1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.util.binary;
14 import java.io.DataInputStream;
15 import java.io.EOFException;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.nio.ByteBuffer;
20 import org.simantics.databoard.util.StreamUtil;
25 * @author Toni Kalajainen (toni.kalajainen@vtt.fi)
27 public class InputStreamReadable implements BinaryReadable {
32 public static BinaryReadable readFully(InputStream is) throws IOException {
33 byte[] data = StreamUtil.readFully(is);
34 return new BinaryMemory( data );
37 public InputStreamReadable(InputStream is, long limit)
51 int value = is.read();
53 throw new EOFException();
60 * @return 0..255 or -1 on end of file
66 int value = is.read();
67 if (value==-1) return -1;
72 public final String readLine() throws IOException {
73 StringBuffer input = new StringBuffer();
78 switch (c = _read()) {
85 // long cur = position();
86 // if ((_read()) != '\n') {
91 input.append((char)c);
96 if ((c == -1) && (input.length() == 0)) {
99 return input.toString();
104 public byte readByte()
107 return (byte) _get();
111 public char readChar() throws IOException {
112 return (char) ( (_get() << 8) | _get() ) ;
116 public int readUnsignedByte() throws IOException {
117 return _get() & 0x000000ff;
121 public boolean readBoolean()
128 public void readFully(byte[] dst, int offset, int length)
132 int bytesRead = is.read(dst, offset, length);
133 if (bytesRead==-1) throw new EOFException();
141 public void readFully(byte[] dst)
144 readFully(dst, 0, dst.length);
148 public void readFully(ByteBuffer buf)
151 for (;buf.hasRemaining();)
152 buf.put((byte)_get());
156 public void readFully(ByteBuffer buf, int length)
160 for (int i=0; i<length; i++)
161 buf.put((byte)_get());
163 byte[] b = new byte[length];
164 readFully(b, 0, length);
170 public double readDouble()
173 return Double.longBitsToDouble(readLong());
177 public float readFloat()
180 return Float.intBitsToFloat(readInt());
183 public final String readUTF() throws IOException {
184 return DataInputStream.readUTF(this);
199 public long readLong()
203 ( ((long)_get()) << 56) |
204 ( ((long)_get()) << 48) |
205 ( ((long)_get()) << 40) |
206 ( ((long)_get()) << 32) |
207 ( ((long)_get()) << 24) |
208 ( ((long)_get()) << 16) |
209 ( ((long)_get()) << 8) |
214 public short readShort()
217 return (short) ( (_get() << 8) | _get() ) ;
221 public int readUnsignedShort()
224 return ( (_get() << 8) | _get() ) ;
234 public long position() {
240 public long skipBytes(long bytes) throws IOException {
246 public int skipBytes(int bytes) throws IOException {