X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Futil%2Fbinary%2FBinaryMemory.java;h=33ac55c5e91fd91c5945434fb52fbcd2bfae4063;hb=a8d030f7db1f59a5b51cdc34f18de7c0a0ee8549;hp=087a6a9da6e931f6c5627d3a317f34286db3f704;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/BinaryMemory.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/BinaryMemory.java index 087a6a9da..33ac55c5e 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/BinaryMemory.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/BinaryMemory.java @@ -1,20 +1,20 @@ -/******************************************************************************* - * 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.IOException; -import java.io.InputStream; -import java.nio.ByteBuffer; +import java.io.DataInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; /** * Rancom access memory blob @@ -35,20 +35,20 @@ public class BinaryMemory implements RandomAccessBinary { buf = ByteBuffer.allocate(16); increment = 16; } - - /** - * New memory blob - */ - public BinaryMemory(byte[] data) { - this.buf = ByteBuffer.wrap(data); - } - - /** - * New memory blob - */ - public BinaryMemory(byte[] data, int offset, int length) { - this.buf = ByteBuffer.wrap(data, offset, length); - } + + /** + * New memory blob + */ + public BinaryMemory(byte[] data) { + this.buf = ByteBuffer.wrap(data); + } + + /** + * New memory blob + */ + public BinaryMemory(byte[] data, int offset, int length) { + this.buf = ByteBuffer.wrap(data, offset, length); + } /** * New memory blob @@ -88,18 +88,18 @@ public class BinaryMemory implements RandomAccessBinary { @Override public void close() throws IOException { } - - @Override - public boolean isOpen() { - return true; - } + + @Override + public boolean isOpen() { + return true; + } @Override public void flush() throws IOException { - } - - @Override - public void reset() throws IOException { + } + + @Override + public void reset() throws IOException { } @Override @@ -109,63 +109,63 @@ public class BinaryMemory implements RandomAccessBinary { byte result = buf.get(); pointer += 1; return result; - } - - int _read() { - if (pointer >= buf.limit()) return -1; - buf.position( (int) pointer ); - byte result = buf.get(); - pointer += 1; - return result & 0xff; - } - - 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 int readUnsignedByte() throws IOException { - return readByte() & 0x000000ff; + } + + int _read() { + if (pointer >= buf.limit()) return -1; + buf.position( (int) pointer ); + byte result = buf.get(); + pointer += 1; + return result & 0xff; + } + + 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 int readUnsignedByte() throws IOException { + return readByte() & 0x000000ff; } - - @Override - public boolean readBoolean() throws IOException { - assertHasReadableBytes(1); - buf.position( (int) pointer ); - byte result = buf.get(); - pointer += 1; - return result!=0; - } + + @Override + public boolean readBoolean() throws IOException { + assertHasReadableBytes(1); + buf.position( (int) pointer ); + byte result = buf.get(); + pointer += 1; + return result!=0; + } @Override public void readFully(byte[] dst, int offset, int length) { @@ -261,24 +261,24 @@ public class BinaryMemory implements RandomAccessBinary { short result = buf.getShort(); pointer += 2; return result; - } - - @Override - public char readChar() { - assertHasReadableBytes(2); - buf.position( (int) pointer ); - char result = buf.getChar(); - pointer += 2; - return result; - } - - @Override - public int readUnsignedShort() { - assertHasReadableBytes(2); - buf.position( (int) pointer ); - int result = buf.getShort() & 0xffff; - pointer += 2; - return result; + } + + @Override + public char readChar() { + assertHasReadableBytes(2); + buf.position( (int) pointer ); + char result = buf.getChar(); + pointer += 2; + return result; + } + + @Override + public int readUnsignedShort() { + assertHasReadableBytes(2); + buf.position( (int) pointer ); + int result = buf.getShort() & 0xffff; + pointer += 2; + return result; } @Override @@ -302,22 +302,22 @@ public class BinaryMemory implements RandomAccessBinary { buf.position( (int) pointer ); buf.put((byte) b); pointer += 1; - } - - @Override - public void writeByte(int b) throws IOException { - assertHasWritableBytes(1); - buf.position( (int) pointer ); - buf.put((byte) b); - pointer += 1; - } - - @Override - public void writeBoolean(boolean v) throws IOException { - assertHasWritableBytes(1); - buf.position( (int) pointer ); - buf.put( (byte) (v ? 1 : 0) ); - pointer += 1; + } + + @Override + public void writeByte(int b) throws IOException { + assertHasWritableBytes(1); + buf.position( (int) pointer ); + buf.put((byte) b); + pointer += 1; + } + + @Override + public void writeBoolean(boolean v) throws IOException { + assertHasWritableBytes(1); + buf.position( (int) pointer ); + buf.put( (byte) (v ? 1 : 0) ); + pointer += 1; } @Override @@ -411,44 +411,44 @@ public class BinaryMemory implements RandomAccessBinary { buf.position( (int) pointer ); buf.putShort( (short) value); pointer += 2; - } - - @Override - public void writeChar(int value) throws IOException { - assertHasWritableBytes(2); - buf.position( (int) pointer ); - buf.putShort( (short) value); - pointer += 2; - } - - @Override - public void writeBytes(String s) throws IOException { - int len = s.length(); - assertHasWritableBytes(len); - buf.position( (int) pointer ); - for (int i = 0 ; i < len ; i++) { - buf.put((byte)s.charAt(i)); - } - pointer += len; - } - - @Override - public void writeChars(String s) throws IOException { - int len = s.length(); - assertHasWritableBytes(len*2); - for (int i = 0 ; i < len ; i++) { - int v = s.charAt(i); - buf.put( (byte) ((v >>> 8) & 0xFF) ); - buf.put( (byte) ((v >>> 0) & 0xFF) ); - } - pointer += len*2; - } - - @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 { + assertHasWritableBytes(2); + buf.position( (int) pointer ); + buf.putShort( (short) value); + pointer += 2; + } + + @Override + public void writeBytes(String s) throws IOException { + int len = s.length(); + assertHasWritableBytes(len); + buf.position( (int) pointer ); + for (int i = 0 ; i < len ; i++) { + buf.put((byte)s.charAt(i)); + } + pointer += len; + } + + @Override + public void writeChars(String s) throws IOException { + int len = s.length(); + assertHasWritableBytes(len*2); + for (int i = 0 ; i < len ; i++) { + int v = s.charAt(i); + buf.put( (byte) ((v >>> 8) & 0xFF) ); + buf.put( (byte) ((v >>> 0) & 0xFF) ); + } + pointer += len*2; + } + + @Override + public void writeUTF(String s) throws IOException { + int len = UTF8.getModifiedUTF8EncodingByteLength(s); + writeShort(len); + UTF8.writeModifiedUTF(this, s); } void assertHasReadableBytes(long count) { @@ -554,15 +554,15 @@ public class BinaryMemory implements RandomAccessBinary { @Override public long skipBytes(long bytes) throws IOException { - pointer += bytes; + pointer += bytes; + return bytes; + } + + @Override + public int skipBytes(int bytes) throws IOException { + pointer += bytes; return bytes; } - - @Override - public int skipBytes(int bytes) throws IOException { - pointer += bytes; - return bytes; - } @Override public String toString() {