X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Futil%2Fbinary%2FBinaryFile.java;h=4a9871f14bafe4fd5204838dfd4bbbec818da097;hb=refs%2Fchanges%2F38%2F238%2F2;hp=1e9dfa3fbe71ac5ac3789e4be6f3cbf94c9a0597;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/BinaryFile.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/BinaryFile.java index 1e9dfa3fb..4a9871f14 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/BinaryFile.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/BinaryFile.java @@ -1,22 +1,22 @@ -/******************************************************************************* - * 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 - *******************************************************************************/ +/******************************************************************************* + * 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; -import java.io.EOFException; -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.ByteBuffer; +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. *

* There is internal pointer variable. The actual file pointer is moved on - * disc read and write operations. - *

- * Primitive number writes (int, short, char, double, float and long) and - * written in big endian (network) byte order. Use {@link Endian} + * disc read and write operations. + *

+ * 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 @@ -62,49 +62,49 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri long virtualLength; long diskLength; - 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 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. + * 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 { + 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; } - - public synchronized boolean isOpen() { - return buf!=null; - } + + 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 - { + { assertReadable(1); int posInBuf = (int) (pointer - buf_pos); int result = buf[posInBuf] & 0xFF; pointer++; return result; - } - - /** - * 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; + } + + /** + * 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 ); } - - /** - * 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()0) { + 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; - } - if (length>0) { - fill(); - if (readableBytesInBuffer()==0) { - throw new EOFException(); - } - } + } + 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() ); +// ( _get() ); _getInt(); } @@ -490,52 +490,52 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri // ( ((long)_get()) << 16) | // ( ((long)_get()) << 8) | // ( ((long)_get()) ); - _getLong(); + _getLong(); } @Override public short readShort() throws IOException { return (short) ( (_get() << 8) | _get() ) ; - } - - @Override - public int readUnsignedShort() throws IOException { - return (int) ( (_get() << 8) | _get() ) ; + } + + @Override + public int readUnsignedShort() throws IOException { + return (int) ( (_get() << 8) | _get() ) ; } - - 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); - } + + 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,25 +560,59 @@ public class BinaryFile implements RandomAccessBinary, BinaryReadable, BinaryWri */ public void reset() throws IOException { writeFlush(); - 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; - } - - + 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 - + + 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>> 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>> 24); - _put(value >>> 16); - _put(value >>> 8); - _put(value); + _putInt(value); } @Override public void writeLong(long value) throws IOException { - _put((int) (value >>> 56)); - _put((int) (value >>> 48)); - _put((int) (value >>> 40)); - _put((int) (value >>> 32)); - _put((int) (value >>> 24)); - _put((int) (value >>> 16)); - _put((int) (value >>> 8)); - _put((int) (value)); + _putLong(value); } @Override public void writeShort(int value) throws IOException { _put(value >> 8); _put(value); - } - - @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 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 {