-/*******************************************************************************\r
- * Copyright (c) 2010 Association for Decentralized Information Management in\r
- * 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 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.IOException;\r
-import java.nio.ByteBuffer;\r
-import java.util.WeakHashMap;\r
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.WeakHashMap;
/**
* Blob is recursive random access binary. Blob is isolated
* @throws IOException
*/
public Blob(RandomAccessBinary parent) throws IOException {
- this(parent, 0, parent.length());\r
+ this(parent, 0, parent.length());
}
/**
* @param start
* @param length
*/
- public Blob(RandomAccessBinary parent, long start, long length) {\r
+ public Blob(RandomAccessBinary parent, long start, long length) {
this.parent = parent;
this.start = start;
this.length = length;
if (children == null) children = new WeakHashMap<Blob, Object>(1);
children.put(result, Blob.class);
return result;
- }\r
- \r
- public RandomAccessBinary getParent() {\r
- return parent;\r
+ }
+
+ public RandomAccessBinary getParent() {
+ return parent;
+ }
+
+ @Override
+ public void close() throws IOException {
}
@Override
- public void close() throws IOException {\r
- }\r
- \r
- @Override\r
- public boolean isOpen() {\r
- return parent.isOpen();\r
+ public boolean isOpen() {
+ return parent.isOpen();
}
@Override
*/
void childGrewBackend(Blob child, long pos, long bytes)
{
- length += bytes;\r
- assert(bytes>=0);\r
+ length += bytes;
+ assert(bytes>=0);
if (children != null) {
for (Blob c : children.keySet()) {
}
/**
- * Parent of this blob grew itself in the back-end.\r
+ * Parent of this blob grew itself in the back-end.
*
* @param parent
* @param pos position in this blob
*/
void parentGrew(Blob parent, long pos, long bytes, ByteSide side)
{
- if (pos<0 || (pos==0 && side!=ByteSide.Right) ) {\r
- start += bytes;\r
- return;\r
- }\r
- if (pos>=length) {\r
- return;\r
- }\r
- // Grow applies this blob\r
- if (\r
- ( pos==0 && side==ByteSide.Right ) ||\r
- ( pos==length && side==ByteSide.Left ) ||\r
- ( pos>0 && pos<length ) ) \r
- length += bytes;\r
+ if (pos<0 || (pos==0 && side!=ByteSide.Right) ) {
+ start += bytes;
+ return;
+ }
+ if (pos>=length) {
+ return;
+ }
+ // Grow applies this blob
+ if (
+ ( pos==0 && side==ByteSide.Right ) ||
+ ( pos==length && side==ByteSide.Left ) ||
+ ( pos>0 && pos<length ) )
+ length += bytes;
// Notify children
if (children!=null) {
@Override
public void removeBytes(long bytes, ByteSide side) throws IOException {
if (pointer<0 || pointer+bytes>length) throw new IndexOutOfBoundsException();
- if (bytes==0) return;\r
+ if (bytes==0) return;
if (bytes<0) throw new IllegalArgumentException("bytes must be positive value");
// Go to backend
* @param bytes
*/
void childShrankBackend(Blob child, long pos, long bytes)
- {\r
- length -= bytes;\r
- assert(bytes>=0);\r
+ {
+ length -= bytes;
+ assert(bytes>=0);
// update siblings
if (children != null) {
}
/**
- * Parent of this blob shrank itself in the back-end.\r
- * \r
+ * Parent of this blob shrank itself in the back-end.
+ *
* @param parent
* @param pos position in this blob
* @param bytes
*/
void parentShrunk(Blob parent, long pos, long bytes)
- {\r
- if (pos<0) {\r
- start -= bytes;\r
- return;\r
- }\r
- if (pos>=length) {\r
- return;\r
- }\r
- // Change applies this blob\r
- length -= bytes; \r
+ {
+ if (pos<0) {
+ start -= bytes;
+ return;
+ }
+ if (pos>=length) {
+ return;
+ }
+ // Change applies this blob
+ length -= bytes;
// Notify children
if (children != null)
@Override
public void flush() throws IOException {
parent.flush();
- }\r
- \r
- @Override\r
- public void reset() throws IOException {\r
- parent.reset();\r
- length = parent.length();\r
+ }
+
+ @Override
+ public void reset() throws IOException {
+ parent.reset();
+ length = parent.length();
}
@Override
parent.position(start + pointer);
parent.write(b);
pointer += 1;
- }\r
- \r
- @Override\r
- public void writeByte(int b) throws IOException {\r
- assertHasWritableBytes(1); \r
- parent.position(start + pointer);\r
- parent.write(b);\r
- pointer += 1;\r
- } \r
- \r
- @Override\r
- public void writeBoolean(boolean v) throws IOException {\r
- assertHasWritableBytes(1); \r
- parent.position(start + pointer);\r
- parent.write( (byte) (v ? 1 : 0));\r
- pointer += 1;\r
+ }
+
+ @Override
+ public void writeByte(int b) throws IOException {
+ assertHasWritableBytes(1);
+ parent.position(start + pointer);
+ parent.write(b);
+ pointer += 1;
+ }
+
+ @Override
+ public void writeBoolean(boolean v) throws IOException {
+ assertHasWritableBytes(1);
+ parent.position(start + pointer);
+ parent.write( (byte) (v ? 1 : 0));
+ pointer += 1;
}
@Override
parent.position(start + pointer);
parent.writeShort(value);
pointer += 2;
- }\r
- \r
- @Override\r
- public void writeChar(int value) throws IOException {\r
- assertHasWritableBytes(2); \r
- parent.position(start + pointer);\r
- parent.writeChar(value);\r
- pointer += 2;\r
- } \r
- \r
- @Override\r
- public void writeBytes(String s) throws IOException {\r
- int len = s.length();\r
- assertHasWritableBytes(len); \r
- parent.position(start + pointer);\r
- parent.writeBytes(s);\r
- pointer += len;\r
- }\r
- \r
- @Override\r
- public void writeChars(String s) throws IOException {\r
- int len = s.length();\r
- assertHasWritableBytes(len*2); \r
- parent.position(start + pointer);\r
- parent.writeChars(s);\r
- pointer += len*2;\r
- } \r
- \r
- @Override\r
- public void writeUTF(String s) throws IOException {\r
- int len = UTF8.getModifiedUTF8EncodingByteLength(s);\r
- assertHasWritableBytes(len+2); \r
- parent.position(start + pointer);\r
-// parent.writeUTF(s);\r
- parent.writeShort(len);\r
- UTF8.writeUTF(this, s);\r
- pointer += len+2; \r
- }\r
+ }
+
+ @Override
+ public void writeChar(int value) throws IOException {
+ assertHasWritableBytes(2);
+ parent.position(start + pointer);
+ parent.writeChar(value);
+ pointer += 2;
+ }
+
+ @Override
+ public void writeBytes(String s) throws IOException {
+ int len = s.length();
+ assertHasWritableBytes(len);
+ parent.position(start + pointer);
+ parent.writeBytes(s);
+ pointer += len;
+ }
+
+ @Override
+ public void writeChars(String s) throws IOException {
+ int len = s.length();
+ assertHasWritableBytes(len*2);
+ parent.position(start + pointer);
+ parent.writeChars(s);
+ pointer += len*2;
+ }
+
+ @Override
+ public void writeUTF(String s) throws IOException {
+ int len = UTF8.getModifiedUTF8EncodingByteLength(s);
+ assertHasWritableBytes(len+2);
+ parent.position(start + pointer);
+// parent.writeUTF(s);
+ parent.writeShort(len);
+ UTF8.writeUTF(this, s);
+ pointer += len+2;
+ }
@Override
public byte readByte() throws IOException {
byte result = parent.readByte();
pointer += 1;
return result;
- }\r
- \r
- @Override\r
- public int readUnsignedByte() throws IOException {\r
- assertHasReadableBytes(1);\r
- parent.position(start + pointer);\r
- int result = parent.readUnsignedByte();\r
- pointer += 1;\r
- return result;\r
- } \r
- \r
- @Override\r
- public boolean readBoolean() throws IOException {\r
- assertHasReadableBytes(1);\r
- parent.position(start + pointer);\r
- boolean result = parent.readBoolean();\r
- pointer += 1;\r
- return result;\r
+ }
+
+ @Override
+ public int readUnsignedByte() throws IOException {
+ assertHasReadableBytes(1);
+ parent.position(start + pointer);
+ int result = parent.readUnsignedByte();
+ pointer += 1;
+ return result;
+ }
+
+ @Override
+ public boolean readBoolean() throws IOException {
+ assertHasReadableBytes(1);
+ parent.position(start + pointer);
+ boolean result = parent.readBoolean();
+ pointer += 1;
+ return result;
}
@Override
short result = parent.readShort();
pointer += 2;
return result;
- }\r
- \r
- @Override\r
- public String readLine() throws IOException {\r
- assertHasReadableBytes(2);\r
- parent.position(start + pointer);\r
- String result = parent.readLine();\r
- pointer += ( parent.position() - start - pointer );\r
- return result;\r
- } \r
- \r
- public final String readUTF() throws IOException {\r
- return DataInputStream.readUTF(this);\r
- } \r
- \r
- @Override\r
- public char readChar() throws IOException {\r
- assertHasReadableBytes(2);\r
- parent.position(start + pointer);\r
- char result = parent.readChar();\r
- pointer += 2;\r
- return result;\r
- } \r
- \r
- @Override\r
- public int readUnsignedShort() throws IOException {\r
- assertHasReadableBytes(2);\r
- parent.position(start + pointer);\r
- int result = parent.readShort() & 0xffff;\r
- pointer += 2;\r
- return result;\r
+ }
+
+ @Override
+ public String readLine() throws IOException {
+ assertHasReadableBytes(2);
+ parent.position(start + pointer);
+ String result = parent.readLine();
+ pointer += ( parent.position() - start - pointer );
+ return result;
+ }
+
+ public final String readUTF() throws IOException {
+ return DataInputStream.readUTF(this);
+ }
+
+ @Override
+ public char readChar() throws IOException {
+ assertHasReadableBytes(2);
+ parent.position(start + pointer);
+ char result = parent.readChar();
+ pointer += 2;
+ return result;
+ }
+
+ @Override
+ public int readUnsignedShort() throws IOException {
+ assertHasReadableBytes(2);
+ parent.position(start + pointer);
+ int result = parent.readShort() & 0xffff;
+ pointer += 2;
+ return result;
}
void assertHasReadableBytes(long count) {
@Override
public long skipBytes(long bytes) throws IOException {
- pointer += bytes;\r
+ pointer += bytes;
return bytes;
- }\r
+ }
- @Override\r
- public int skipBytes(int bytes) throws IOException {\r
- pointer += bytes;\r
- return bytes;\r
- }\r
+ @Override
+ public int skipBytes(int bytes) throws IOException {
+ pointer += bytes;
+ return bytes;
+ }
public long getStartPositionInSourceBinary() {
return start;
}
- public void setPositionInSource(long start, long length) {\r
+ public void setPositionInSource(long start, long length) {
this.start = start;
this.length = length;
}