]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/ByteBufferReadable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / binary / ByteBufferReadable.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/ByteBufferReadable.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/ByteBufferReadable.java
new file mode 100644 (file)
index 0000000..455d337
--- /dev/null
@@ -0,0 +1,205 @@
+/*******************************************************************************\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
+package org.simantics.databoard.util.binary;
+
+import java.io.DataInputStream;\r
+import java.io.IOException;\r
+import java.nio.ByteBuffer;\r
+import java.nio.ByteOrder;\r
+
+public class ByteBufferReadable implements BinaryReadable {
+       
+       ByteBuffer buf;
+       
+       public ByteBufferReadable(ByteBuffer buf) {             
+               if (buf == null)
+                       throw new IllegalArgumentException("null");
+               this.buf = buf;
+       }
+
+       public ByteBufferReadable(byte[] buf) {         
+               if (buf == null)
+                       throw new IllegalArgumentException("null");
+               this.buf = ByteBuffer.wrap(buf);                
+       }
+
+       
+       @Override
+       public byte readByte() {
+               return buf.get();
+       }\r
+               \r
+       int _read() {\r
+               if (buf.position()>=buf.limit()) return -1;\r
+               return buf.get() & 0xff;\r
+       }\r
+       \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
+       \r
+       @Override\r
+       public int readUnsignedByte() throws IOException {\r
+               return buf.get() & 0x000000ff;\r
+       }               \r
+       
+       @Override\r
+       public boolean readBoolean() throws IOException {\r
+               return buf.get()!=0;\r
+       }\r
+
+       @Override
+       public void readFully(byte[] dst, int offset, int length) {
+               buf.get(dst, offset, length);
+       }
+
+       @Override
+       public void readFully(byte[] dst) {
+               buf.get(dst);
+       }
+
+       @Override
+       public void readFully(ByteBuffer buf) {         
+               if (buf.hasArray()) {
+                       this.buf.get(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining());
+                       buf.position(buf.capacity());
+               } else {
+                       buf.put(buf);
+               }
+       }
+
+       @Override
+       public void readFully(ByteBuffer buf, int length) {
+               if (buf.hasArray()) {
+                       this.buf.get(buf.array(), buf.arrayOffset() + buf.position(), length);
+                       buf.position(buf.position() + length);
+               } else {
+//                     int len = Math.min( Math.min( buf.remaining(), this.buf.remaining() ), length);
+                       int len = length;
+                       int origLimit = this.buf.limit();
+                       try {
+                               this.buf.limit(this.buf.position()+len);
+                               buf.put(this.buf);
+                       } finally {
+                               this.buf.limit(origLimit);
+                       }
+               }
+       }
+
+       @Override
+       public double readDouble() {
+               return buf.getDouble();
+       }
+
+       @Override
+       public float readFloat() {
+               return buf.getFloat();
+       }
+
+       @Override
+       public int readInt() {
+               return buf.getInt();
+       }
+
+       @Override
+       public long readLong() {
+               return buf.getLong();
+       }
+
+       @Override
+       public short readShort() {
+               return buf.getShort();
+       }\r
+       \r
+    public final String readUTF() throws IOException {\r
+       return DataInputStream.readUTF(this);\r
+    } \r
+    \r
+       @Override\r
+       public char readChar() throws IOException {\r
+               return buf.getChar();\r
+       }
+\r
+       @Override\r
+       public int readUnsignedShort() {\r
+               return buf.getShort() & 0xffff;\r
+       }\r
+       
+       @Override
+       public long length() {
+               return buf.limit();
+       }
+       
+       @Override
+       public long position() {
+               return buf.position();
+       }
+       
+       public ByteOrder order() {
+               return buf.order();
+       }
+
+       public void order(ByteOrder order) {
+               buf.order(order);
+       }
+
+       public void position(int newPosition) throws IOException {
+               buf.position(newPosition);
+       }
+
+       public void position(long newPosition) throws IOException {
+               if (newPosition>=Integer.MAX_VALUE || newPosition<0) throw new IllegalArgumentException("index out of range");
+               buf.position((int) newPosition);
+       }
+
+       @Override
+       public long skipBytes(long bytes) throws IOException {
+               long newPosition = bytes + position();
+               position( newPosition );\r
+               return bytes;
+       }
+\r
+       @Override\r
+       public int skipBytes(int bytes) throws IOException {\r
+               long newPosition = bytes + position();\r
+               position( newPosition );\r
+               return bytes;\r
+       }\r
+       
+}