]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/BinaryFile.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / binary / BinaryFile.java
index e7d8d386eb14c79ec6b295b215c3e3626384d642..4a9871f14bafe4fd5204838dfd4bbbec818da097 100644 (file)
@@ -1,22 +1,22 @@
-/*******************************************************************************\r
- * Copyright (c) 2010, 2016 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
+/*******************************************************************************
+ * Copyright (c) 2010, 2016 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
 package org.simantics.databoard.util.binary;
 
-import java.io.DataInputStream;\r
-import java.io.EOFException;\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.io.RandomAccessFile;\r
-import java.nio.ByteBuffer;\r
+import java.io.DataInputStream;
+import java.io.EOFException;
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
 
 /**
  * This class is a Random Access File implementation to RandomAccessBinary.
@@ -30,10 +30,10 @@ import java.nio.ByteBuffer;
  * to disc.
  * <p>
  * There is internal pointer variable. The actual file pointer is moved on
- * disc read and write operations. \r
- * <p>\r
- * Primitive number writes (int, short, char, double, float and long) and\r
- * written in big endian (network) byte order. Use {@link Endian}\r
+ * disc read and write operations. 
+ * <p>
+ * Primitive number writes (int, short, char, double, float and long) and
+ * written in big endian (network) byte order. Use {@link Endian}
  * to make little endian operations.
  *
  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
@@ -62,49 +62,49 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri
     long virtualLength;
     long diskLength;
     
-       public BinaryFile(RandomAccessFile file) throws IOException\r
-       {\r
-               this.raf = file;\r
-               virtualLength = diskLength = raf.length();\r
-               pointer = 0;\r
-               buf = new byte[4096];\r
-       }\r
-\r
-       public BinaryFile(File file) throws IOException\r
-       {\r
-               this.raf = new RandomAccessFile(file, "rw");\r
-               virtualLength = diskLength = raf.length();              \r
-               this.file = file;\r
-               pointer = 0;\r
-               buf = new byte[4096];\r
-       }\r
-       \r
-       public BinaryFile(File file, String mode) throws IOException\r
-       {\r
-               this.raf = new RandomAccessFile(file, mode);\r
-               virtualLength = diskLength = raf.length();              \r
-               this.file = file;\r
-               pointer = 0;\r
-               buf = new byte[4096];\r
-       }\r
-       \r
-       public BinaryFile(RandomAccessFile file, int bufSize) throws IOException\r
-       {\r
-               this.raf = file;\r
-               virtualLength = diskLength = raf.length();\r
-               pointer = 0;\r
-               buf = new byte[bufSize];\r
-       }\r
-\r
-       public BinaryFile(File file, int bufSize) throws IOException\r
-       {\r
-               this.raf = new RandomAccessFile(file, "rw");\r
-               virtualLength = diskLength = raf.length();              \r
-               this.file = file;\r
-               pointer = 0;\r
-               buf = new byte[bufSize];\r
-       }\r
-       \r
+       public BinaryFile(RandomAccessFile file) throws IOException
+       {
+               this.raf = file;
+               virtualLength = diskLength = raf.length();
+               pointer = 0;
+               buf = new byte[4096];
+       }
+
+       public BinaryFile(File file) throws IOException
+       {
+               this.raf = new RandomAccessFile(file, "rw");
+               virtualLength = diskLength = raf.length();              
+               this.file = file;
+               pointer = 0;
+               buf = new byte[4096];
+       }
+       
+       public BinaryFile(File file, String mode) throws IOException
+       {
+               this.raf = new RandomAccessFile(file, mode);
+               virtualLength = diskLength = raf.length();              
+               this.file = file;
+               pointer = 0;
+               buf = new byte[4096];
+       }
+       
+       public BinaryFile(RandomAccessFile file, int bufSize) throws IOException
+       {
+               this.raf = file;
+               virtualLength = diskLength = raf.length();
+               pointer = 0;
+               buf = new byte[bufSize];
+       }
+
+       public BinaryFile(File file, int bufSize) throws IOException
+       {
+               this.raf = new RandomAccessFile(file, "rw");
+               virtualLength = diskLength = raf.length();              
+               this.file = file;
+               pointer = 0;
+               buf = new byte[bufSize];
+       }
+       
        public static BinaryFile tempFile(long size) throws IOException
        {
                File tmpFile = File.createTempFile("Temp", ".file");
@@ -115,12 +115,12 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri
        }
        
        /**
-        * Closes the object. Note, this will close the input random access file.\r
+        * Closes the object. Note, this will close the input random access file.
         * This method may be called several times.
         *  
         * @throws IOException
         */
-       public synchronized void close() throws IOException {\r
+       public synchronized void close() throws IOException {
                if (raf==null) return;
                flush();
                pointer = -1;
@@ -128,10 +128,10 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri
                raf = null;
                buf = null;
        }
-\r
-       public synchronized boolean isOpen() {\r
-               return buf!=null;\r
-       }\r
+
+       public synchronized boolean isOpen() {
+               return buf!=null;
+       }
        
        public File file() {
                return file;
@@ -170,73 +170,73 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri
         * @throws IOException
         */
        int _get() throws IOException
-       {\r
+       {
                assertReadable(1);
                
                int posInBuf = (int) (pointer - buf_pos);
                int result = buf[posInBuf] & 0xFF;
                pointer++;
                return result;
-       }       \r
-\r
-       /**\r
-        * Get next 4 bytes as an int value. This is an optimization.\r
-        * @return long value\r
-        * @throws IOException\r
-        */\r
-       int _getInt() throws IOException\r
-       {\r
-               assertReadable(4);              \r
-\r
-               int posInBuf = (int) (pointer - buf_pos);\r
-               int result =\r
-                       (((int) (buf[posInBuf + 3] & 0xFF)) |\r
-                       (((int) (buf[posInBuf + 2] & 0xFF)) << 8) |\r
-                       (((int) (buf[posInBuf + 1] & 0xFF)) << 16) |\r
-                       (((int) (buf[posInBuf]     & 0xFF)) << 24));\r
-               pointer+=4;\r
-               return result;\r
-       }\r
-\r
-       /**\r
-        * Get next 8 bytes as a long value. This is an optimization.\r
-        * @return long value\r
-        * @throws IOException\r
-        */\r
-       long _getLong() throws IOException\r
-       {\r
-               assertReadable(8);\r
-\r
-               int posInBuf = (int) (pointer - buf_pos);\r
-               long result =\r
-                       (((long) (buf[posInBuf + 7] & 0xFF)) |\r
-                       (((long) (buf[posInBuf + 6] & 0xFF)) << 8) |\r
-                       (((long) (buf[posInBuf + 5] & 0xFF)) << 16) |\r
-                       (((long) (buf[posInBuf + 4] & 0xFF)) << 24) |\r
-                       (((long) (buf[posInBuf + 3] & 0xFF)) << 32) |\r
-                       (((long) (buf[posInBuf + 2] & 0xFF)) << 40) |\r
-                       (((long) (buf[posInBuf + 1] & 0xFF)) << 48) |\r
-                       (((long) (buf[posInBuf]     & 0xFF)) << 56));\r
-               pointer+=8;\r
-               return result;\r
-       }\r
-\r
-       /**\r
-        * Get next byte\r
-        * @return 0..255 or -1 on end of file\r
-        * @throws IOException\r
-        */\r
-       int _read() throws IOException\r
-       {\r
-               if (readableBytesInBuffer()<1) {\r
-                       fill();\r
-                       if (readableBytesInBuffer()==0) return -1;\r
-               }\r
-               \r
-               int posInBuf = (int) (pointer - buf_pos);\r
-               int result = buf[posInBuf] & 0xFF;\r
-               pointer++;\r
-               return result;\r
+       }       
+
+       /**
+        * Get next 4 bytes as an int value. This is an optimization.
+        * @return long value
+        * @throws IOException
+        */
+       int _getInt() throws IOException
+       {
+               assertReadable(4);              
+
+               int posInBuf = (int) (pointer - buf_pos);
+               int result =
+                       (((int) (buf[posInBuf + 3] & 0xFF)) |
+                       (((int) (buf[posInBuf + 2] & 0xFF)) << 8) |
+                       (((int) (buf[posInBuf + 1] & 0xFF)) << 16) |
+                       (((int) (buf[posInBuf]     & 0xFF)) << 24));
+               pointer+=4;
+               return result;
+       }
+
+       /**
+        * Get next 8 bytes as a long value. This is an optimization.
+        * @return long value
+        * @throws IOException
+        */
+       long _getLong() throws IOException
+       {
+               assertReadable(8);
+
+               int posInBuf = (int) (pointer - buf_pos);
+               long result =
+                       (((long) (buf[posInBuf + 7] & 0xFF)) |
+                       (((long) (buf[posInBuf + 6] & 0xFF)) << 8) |
+                       (((long) (buf[posInBuf + 5] & 0xFF)) << 16) |
+                       (((long) (buf[posInBuf + 4] & 0xFF)) << 24) |
+                       (((long) (buf[posInBuf + 3] & 0xFF)) << 32) |
+                       (((long) (buf[posInBuf + 2] & 0xFF)) << 40) |
+                       (((long) (buf[posInBuf + 1] & 0xFF)) << 48) |
+                       (((long) (buf[posInBuf]     & 0xFF)) << 56));
+               pointer+=8;
+               return result;
+       }
+
+       /**
+        * Get next byte
+        * @return 0..255 or -1 on end of file
+        * @throws IOException
+        */
+       int _read() throws IOException
+       {
+               if (readableBytesInBuffer()<1) {
+                       fill();
+                       if (readableBytesInBuffer()==0) return -1;
+               }
+               
+               int posInBuf = (int) (pointer - buf_pos);
+               int result = buf[posInBuf] & 0xFF;
+               pointer++;
+               return result;
        }               
        
        void _get(byte[] dst, int offset, int length) throws IOException {
@@ -329,21 +329,21 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri
                
                return (int) ( buf.length - posInBuf );
        }
-\r
-       /**\r
-        * Assert there is a set number of bytes in read buffer.\r
-        * bytes should not be larger than read buffer size (buf.length, 4K) \r
-        * \r
-        * @param bytes\r
-        * @throws IOException\r
-        */\r
-       private void assertReadable(int bytes) throws IOException\r
-       {\r
-               if (readableBytesInBuffer()<bytes) {\r
-                       fill();\r
-                       if (readableBytesInBuffer()<bytes) throw new EOFException(); \r
-               }\r
-       }\r
+
+       /**
+        * Assert there is a set number of bytes in read buffer.
+        * bytes should not be larger than read buffer size (buf.length, 4K) 
+        * 
+        * @param bytes
+        * @throws IOException
+        */
+       private void assertReadable(int bytes) throws IOException
+       {
+               if (readableBytesInBuffer()<bytes) {
+                       fill();
+                       if (readableBytesInBuffer()<bytes) throw new EOFException(); 
+               }
+       }
        
        /**
         * Read the buffer at file position pointer.  
@@ -402,26 +402,26 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri
                        readable_bytes_count = new_buf_length;
                }
        }
-\r
+
        @Override
        public byte readByte() throws IOException {
                return (byte) _get();
-       }\r
-       \r
-       @Override\r
-       public char readChar() throws IOException {\r
-               return (char)((_get() << 8) | _get());\r
-       }\r
-       \r
-       @Override\r
-       public int readUnsignedByte() throws IOException {\r
-               return _get() & 0x000000ff;\r
+       }
+       
+       @Override
+       public char readChar() throws IOException {
+               return (char)((_get() << 8) | _get());
+       }
+       
+       @Override
+       public int readUnsignedByte() throws IOException {
+               return _get() & 0x000000ff;
        }       
-\r
-       @Override\r
-       public boolean readBoolean() throws IOException {\r
-               return _get()!=0;\r
-       }\r
+
+       @Override
+       public boolean readBoolean() throws IOException {
+               return _get()!=0;
+       }
        
        @Override
        public void readFully(byte[] dst, int offset, int length) throws IOException {
@@ -440,7 +440,7 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri
 
        @Override
        public void readFully(ByteBuffer buf, int length) throws IOException {
-               while (length>0) {\r
+               while (length>0) {
                        assertReadable( Math.min(this.buf.length, length) );
                        long n = Math.min(readableBytesInBuffer(), length);
                        if (n==0) throw new EOFException();
@@ -449,13 +449,13 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri
                                buf.put(this.buf, (int)posInBuf, (int)n);
                                length -= n;
                                pointer += n;
-                       }\r
-                       if (length>0) {\r
-                               fill();\r
-                               if (readableBytesInBuffer()==0) {\r
-                                       throw new EOFException();\r
-                               }\r
-                       }\r
+                       }
+                       if (length>0) {
+                               fill();
+                               if (readableBytesInBuffer()==0) {
+                                       throw new EOFException();
+                               }
+                       }
                }
        }
 
@@ -475,7 +475,7 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri
 //                     ( _get() << 24) |
 //                     ( _get() << 16) | 
 //                     ( _get() << 8) |
-//                     ( _get() );\r
+//                     ( _get() );
                _getInt();
        }
 
@@ -490,52 +490,52 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri
 //             ( ((long)_get()) << 16) |
 //             ( ((long)_get()) << 8) |
 //             ( ((long)_get()) );
-               _getLong();\r
+               _getLong();
        }
 
        @Override
        public short readShort() throws IOException {
                return (short) ( (_get() << 8) |  _get() ) ;
-       }\r
-       \r
-       @Override\r
-       public int readUnsignedShort() throws IOException {\r
-               return (int) ( (_get() << 8) |  _get() ) ;\r
+       }
+       
+       @Override
+       public int readUnsignedShort() throws IOException {
+               return (int) ( (_get() << 8) |  _get() ) ;
        }       
-       \r
-    public final String readLine() throws IOException {\r
-       StringBuffer input = new StringBuffer();\r
-       int c = -1;\r
-       boolean eol = false;\r
-\r
-       while (!eol) {\r
-           switch (c = _read()) {\r
-           case -1:\r
-           case '\n':\r
-               eol = true;\r
-               break;\r
-           case '\r':\r
-               eol = true;\r
-               long cur = position();\r
-               if ((_read()) != '\n') {\r
-                   position(cur);\r
-               }\r
-               break;\r
-           default:\r
-               input.append((char)c);\r
-               break;\r
-           }\r
-       }\r
-\r
-       if ((c == -1) && (input.length() == 0)) {\r
-           return null;\r
-       }\r
-       return input.toString();\r
-    }  \r
-    \r
-    public final String readUTF() throws IOException {\r
-       return DataInputStream.readUTF(this);\r
-    }    \r
+       
+    public final String readLine() throws IOException {
+       StringBuffer input = new StringBuffer();
+       int c = -1;
+       boolean eol = false;
+
+       while (!eol) {
+           switch (c = _read()) {
+           case -1:
+           case '\n':
+               eol = true;
+               break;
+           case '\r':
+               eol = true;
+               long cur = position();
+               if ((_read()) != '\n') {
+                   position(cur);
+               }
+               break;
+           default:
+               input.append((char)c);
+               break;
+           }
+       }
+
+       if ((c == -1) && (input.length() == 0)) {
+           return null;
+       }
+       return input.toString();
+    }  
+    
+    public final String readUTF() throws IOException {
+       return DataInputStream.readUTF(this);
+    }    
        
        @Override
        public long position() {
@@ -560,58 +560,58 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri
         */
        public void reset() throws IOException {
                writeFlush();
-               readable_bytes_count = 0;\r
-               virtualLength = diskLength = raf.length();              \r
-       }
-\r
-       @Override\r
-       public long skipBytes(long bytes) throws IOException {          \r
-               pointer += bytes;\r
-               return bytes;\r
-       }\r
-       
-       @Override\r
-       public int skipBytes(int bytes) throws IOException {            \r
-               pointer += bytes;\r
-               return bytes;\r
-       }\r
-\r
-\r
+               readable_bytes_count = 0;
+               virtualLength = diskLength = raf.length();              
+       }
+
+       @Override
+       public long skipBytes(long bytes) throws IOException {          
+               pointer += bytes;
+               return bytes;
+       }
+       
+       @Override
+       public int skipBytes(int bytes) throws IOException {            
+               pointer += bytes;
+               return bytes;
+       }
+
+
        // WRITE
-\r
-       void _putLong(long value) throws IOException\r
-       {\r
-               prepareForWrite(8);\r
-               int posInBuf = (int) (pointer - buf_pos);\r
-               buf[posInBuf] = (byte) (value >>> 56);\r
-               buf[posInBuf+1] = (byte) (value >>> 48);\r
-               buf[posInBuf+2] = (byte) (value >>> 40);\r
-               buf[posInBuf+3] = (byte) (value >>> 32);\r
-               buf[posInBuf+4] = (byte) (value >>> 24);\r
-               buf[posInBuf+5] = (byte) (value >>> 16);\r
-               buf[posInBuf+6] = (byte) (value >>> 8);\r
-               buf[posInBuf+7] = (byte) value;\r
-               posInBuf += 8;\r
-               pointer += 8;\r
-               if (write_buf_count<posInBuf) write_buf_count = posInBuf;\r
-               if (readable_bytes_count<write_buf_count) readable_bytes_count=write_buf_count;\r
-               if (virtualLength<pointer) virtualLength=pointer;\r
-       }\r
-\r
-       void _putInt(int value) throws IOException\r
-       {\r
-               prepareForWrite(4);\r
-               int posInBuf = (int) (pointer - buf_pos);\r
-               buf[posInBuf] = (byte) (value >>> 24);\r
-               buf[posInBuf+1] = (byte) (value >>> 16);\r
-               buf[posInBuf+2] = (byte) (value >>> 8);\r
-               buf[posInBuf+3] = (byte) value;\r
-               posInBuf += 4;\r
-               pointer += 4;\r
-               if (write_buf_count<posInBuf) write_buf_count = posInBuf;\r
-               if (readable_bytes_count<write_buf_count) readable_bytes_count=write_buf_count;\r
-               if (virtualLength<pointer) virtualLength=pointer;\r
-       }\r
+
+       void _putLong(long value) throws IOException
+       {
+               prepareForWrite(8);
+               int posInBuf = (int) (pointer - buf_pos);
+               buf[posInBuf] = (byte) (value >>> 56);
+               buf[posInBuf+1] = (byte) (value >>> 48);
+               buf[posInBuf+2] = (byte) (value >>> 40);
+               buf[posInBuf+3] = (byte) (value >>> 32);
+               buf[posInBuf+4] = (byte) (value >>> 24);
+               buf[posInBuf+5] = (byte) (value >>> 16);
+               buf[posInBuf+6] = (byte) (value >>> 8);
+               buf[posInBuf+7] = (byte) value;
+               posInBuf += 8;
+               pointer += 8;
+               if (write_buf_count<posInBuf) write_buf_count = posInBuf;
+               if (readable_bytes_count<write_buf_count) readable_bytes_count=write_buf_count;
+               if (virtualLength<pointer) virtualLength=pointer;
+       }
+
+       void _putInt(int value) throws IOException
+       {
+               prepareForWrite(4);
+               int posInBuf = (int) (pointer - buf_pos);
+               buf[posInBuf] = (byte) (value >>> 24);
+               buf[posInBuf+1] = (byte) (value >>> 16);
+               buf[posInBuf+2] = (byte) (value >>> 8);
+               buf[posInBuf+3] = (byte) value;
+               posInBuf += 4;
+               pointer += 4;
+               if (write_buf_count<posInBuf) write_buf_count = posInBuf;
+               if (readable_bytes_count<write_buf_count) readable_bytes_count=write_buf_count;
+               if (virtualLength<pointer) virtualLength=pointer;
+       }
 
        void _put(int value) throws IOException
        {
@@ -643,17 +643,17 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri
        @Override
        public void write(int b) throws IOException {
                _put(b);
-       }\r
-       \r
-       @Override\r
-       public void writeByte(int b) throws IOException {\r
-               _put(b);\r
+       }
+       
+       @Override
+       public void writeByte(int b) throws IOException {
+               _put(b);
        }       
-\r
-       @Override\r
-       public void writeBoolean(boolean v) throws IOException {\r
-               _put( v ? 1 : 0);\r
-       }\r
+
+       @Override
+       public void writeBoolean(boolean v) throws IOException {
+               _put( v ? 1 : 0);
+       }
        
        @Override
        public void writeFully(ByteBuffer src) throws IOException {
@@ -712,38 +712,38 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri
        public void writeShort(int value) throws IOException {
                _put(value >> 8);
                _put(value);
-       }\r
-       \r
-       @Override\r
-       public void writeChar(int value) throws IOException {\r
-               _put(value >> 8);\r
-               _put(value);\r
-       }\r
-       \r
-       @Override\r
-       public void writeBytes(String s) throws IOException {\r
-               int len = s.length();\r
-               for (int i = 0 ; i < len ; i++) {\r
-                   _put((byte)s.charAt(i));\r
-               }\r
-       }\r
-       \r
-       @Override\r
-       public void writeChars(String s) throws IOException {\r
-        int len = s.length();\r
-        for (int i = 0 ; i < len ; i++) {\r
-            int v = s.charAt(i);\r
-            _put((v >>> 8) & 0xFF); \r
-            _put((v >>> 0) & 0xFF); \r
-        }\r
-       }
-\r
-       @Override\r
-       public void writeUTF(String s) throws IOException {\r
-               int len = UTF8.getModifiedUTF8EncodingByteLength(s);\r
-               writeShort(len);\r
-               UTF8.writeModifiedUTF(this, s);\r
-       }\r
+       }
+       
+       @Override
+       public void writeChar(int value) throws IOException {
+               _put(value >> 8);
+               _put(value);
+       }
+       
+       @Override
+       public void writeBytes(String s) throws IOException {
+               int len = s.length();
+               for (int i = 0 ; i < len ; i++) {
+                   _put((byte)s.charAt(i));
+               }
+       }
+       
+       @Override
+       public void writeChars(String s) throws IOException {
+        int len = s.length();
+        for (int i = 0 ; i < len ; i++) {
+            int v = s.charAt(i);
+            _put((v >>> 8) & 0xFF); 
+            _put((v >>> 0) & 0xFF); 
+        }
+       }
+
+       @Override
+       public void writeUTF(String s) throws IOException {
+               int len = UTF8.getModifiedUTF8EncodingByteLength(s);
+               writeShort(len);
+               UTF8.writeModifiedUTF(this, s);
+       }
        
        @Override
        public void insertBytes(long bytes, ByteSide side) throws IOException {