]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/InputStreamReadable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / binary / InputStreamReadable.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/InputStreamReadable.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/InputStreamReadable.java
new file mode 100644 (file)
index 0000000..17a34ac
--- /dev/null
@@ -0,0 +1,252 @@
+/*******************************************************************************\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.EOFException;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.nio.ByteBuffer;\r
+\r
+import org.simantics.databoard.util.StreamUtil;\r
+
+/**
+ * Input stream reader
+ * 
+ * @author Toni Kalajainen (toni.kalajainen@vtt.fi)
+ */
+public class InputStreamReadable implements BinaryReadable {
+
+       InputStream is;
+       long limit, position;\r
+       \r
+       public static BinaryReadable readFully(InputStream is) throws IOException {\r
+               byte[] data = StreamUtil.readFully(is);\r
+               return new BinaryMemory( data );\r
+       }
+       
+       public InputStreamReadable(InputStream is, long limit)
+       {
+               this.is = is;
+               this.limit = limit;
+       }
+
+       /**
+        * Get next byte
+        * @return 0..255
+        * @throws IOException
+        */
+       int _get()
+       throws IOException
+       {
+               int value = is.read();
+               if (value==-1)
+                       throw new EOFException();
+               position++;
+               return value & 0xff;
+       }\r
+       \r
+       /**\r
+        * Get next byte\r
+        * @return 0..255 or -1 on end of file\r
+        * @throws IOException\r
+        */\r
+       int _read()\r
+       throws IOException\r
+       {\r
+               int value = is.read();\r
+               if (value==-1) return -1;\r
+               position++;             \r
+               return value & 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
+       
+       
+       @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
+       }       \r
+\r
+       @Override\r
+       public boolean readBoolean() \r
+    throws IOException \r
+       {\r
+               return _get()!=0;\r
+       }       
+
+       @Override
+       public void readFully(byte[] dst, int offset, int length) 
+    throws IOException 
+       {
+               while (length>0) {
+                       int bytesRead = is.read(dst, offset, length);
+                       if (bytesRead==-1) throw new EOFException();
+                       position+=bytesRead;                    
+                       offset += bytesRead;
+                       length -= bytesRead;
+               }
+       }
+
+       @Override
+       public void readFully(byte[] dst) 
+    throws IOException 
+       {
+               readFully(dst, 0, dst.length);
+       }
+
+       @Override
+       public void readFully(ByteBuffer buf) 
+    throws IOException 
+       {               
+               for (;buf.hasRemaining();)
+                       buf.put((byte)_get());          
+       }
+
+       @Override
+       public void readFully(ByteBuffer buf, int length) 
+    throws IOException 
+       {
+               if (length<256) {
+                       for (int i=0; i<length; i++)
+                               buf.put((byte)_get());
+               } else {
+                       byte[] b = new byte[length];
+                       readFully(b, 0, length);
+                       buf.put(b);
+               }
+       }
+
+       @Override
+       public double readDouble() 
+    throws IOException 
+       {
+               return Double.longBitsToDouble(readLong());
+       }
+
+       @Override
+       public float readFloat() 
+    throws IOException 
+       {
+               return Float.intBitsToFloat(readInt());
+       }\r
+       \r
+    public final String readUTF() throws IOException {\r
+       return DataInputStream.readUTF(this);\r
+    }  
+
+       @Override
+       public int readInt() 
+    throws IOException 
+       {
+               return 
+                       ( _get() << 24) |
+                       ( _get() << 16) | 
+                       ( _get() << 8) |
+                       ( _get() );
+       }
+
+       @Override
+       public long readLong() 
+    throws IOException 
+       {
+               return
+               ( ((long)_get()) << 56) |
+               ( ((long)_get()) << 48) | 
+               ( ((long)_get()) << 40) |
+               ( ((long)_get()) << 32) |               
+               ( ((long)_get()) << 24) |
+               ( ((long)_get()) << 16) | 
+               ( ((long)_get()) << 8) |
+               ( ((long)_get()) );             
+       }
+
+       @Override
+       public short readShort() 
+    throws IOException 
+       {
+               return (short) ( (_get() << 8) |  _get() ) ;
+       }
+\r
+       @Override\r
+       public int readUnsignedShort() \r
+    throws IOException \r
+       {\r
+               return ( (_get() << 8) |  _get() ) ;\r
+       }       \r
+       
+       @Override
+       public long length() 
+       {
+               return limit;
+       }
+       
+       @Override
+       public long position() {
+               return position;
+       }
+
+
+       @Override
+       public long skipBytes(long bytes) throws IOException {
+               is.skip(bytes); \r
+               return bytes;
+       }
+\r
+       @Override\r
+       public int skipBytes(int bytes) throws IOException {\r
+               is.skip(bytes); \r
+               return bytes;\r
+       }\r
+       
+}
+