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.IOException;
16 import java.nio.ByteBuffer;
17 import java.nio.ByteOrder;
19 public class ByteBufferReadable implements BinaryReadable {
23 public ByteBufferReadable(ByteBuffer buf) {
25 throw new IllegalArgumentException("null");
29 public ByteBufferReadable(byte[] buf) {
31 throw new IllegalArgumentException("null");
32 this.buf = ByteBuffer.wrap(buf);
37 public byte readByte() {
42 if (buf.position()>=buf.limit()) return -1;
43 return buf.get() & 0xff;
46 public final String readLine() throws IOException {
47 StringBuffer input = new StringBuffer();
52 switch (c = _read()) {
59 long cur = position();
60 if ((_read()) != '\n') {
65 input.append((char)c);
70 if ((c == -1) && (input.length() == 0)) {
73 return input.toString();
78 public int readUnsignedByte() throws IOException {
79 return buf.get() & 0x000000ff;
83 public boolean readBoolean() throws IOException {
88 public void readFully(byte[] dst, int offset, int length) {
89 buf.get(dst, offset, length);
93 public void readFully(byte[] dst) {
98 public void readFully(ByteBuffer buf) {
100 this.buf.get(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining());
101 buf.position(buf.capacity());
108 public void readFully(ByteBuffer buf, int length) {
109 if (buf.hasArray()) {
110 this.buf.get(buf.array(), buf.arrayOffset() + buf.position(), length);
111 buf.position(buf.position() + length);
113 // int len = Math.min( Math.min( buf.remaining(), this.buf.remaining() ), length);
115 int origLimit = this.buf.limit();
117 this.buf.limit(this.buf.position()+len);
120 this.buf.limit(origLimit);
126 public double readDouble() {
127 return buf.getDouble();
131 public float readFloat() {
132 return buf.getFloat();
136 public int readInt() {
141 public long readLong() {
142 return buf.getLong();
146 public short readShort() {
147 return buf.getShort();
150 public final String readUTF() throws IOException {
151 return DataInputStream.readUTF(this);
155 public char readChar() throws IOException {
156 return buf.getChar();
160 public int readUnsignedShort() {
161 return buf.getShort() & 0xffff;
165 public long length() {
170 public long position() {
171 return buf.position();
174 public ByteOrder order() {
178 public void order(ByteOrder order) {
182 public void position(int newPosition) throws IOException {
183 buf.position(newPosition);
186 public void position(long newPosition) throws IOException {
187 if (newPosition>=Integer.MAX_VALUE || newPosition<0) throw new IllegalArgumentException("index out of range");
188 buf.position((int) newPosition);
192 public long skipBytes(long bytes) throws IOException {
193 long newPosition = bytes + position();
194 position( newPosition );
199 public int skipBytes(int bytes) throws IOException {
200 long newPosition = bytes + position();
201 position( newPosition );