]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/ByteBufferReadable.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / binary / ByteBufferReadable.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
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
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.util.binary;
13
14 import java.io.DataInputStream;
15 import java.io.IOException;
16 import java.nio.ByteBuffer;
17 import java.nio.ByteOrder;
18
19 public class ByteBufferReadable implements BinaryReadable {
20         
21         ByteBuffer buf;
22         
23         public ByteBufferReadable(ByteBuffer buf) {             
24                 if (buf == null)
25                         throw new IllegalArgumentException("null");
26                 this.buf = buf;
27         }
28
29         public ByteBufferReadable(byte[] buf) {         
30                 if (buf == null)
31                         throw new IllegalArgumentException("null");
32                 this.buf = ByteBuffer.wrap(buf);                
33         }
34
35         
36         @Override
37         public byte readByte() {
38                 return buf.get();
39         }
40                 
41         int _read() {
42                 if (buf.position()>=buf.limit()) return -1;
43                 return buf.get() & 0xff;
44         }
45         
46     public final String readLine() throws IOException {
47         StringBuffer input = new StringBuffer();
48         int c = -1;
49         boolean eol = false;
50
51         while (!eol) {
52             switch (c = _read()) {
53             case -1:
54             case '\n':
55                 eol = true;
56                 break;
57             case '\r':
58                 eol = true;
59                 long cur = position();
60                 if ((_read()) != '\n') {
61                     position(cur);
62                 }
63                 break;
64             default:
65                 input.append((char)c);
66                 break;
67             }
68         }
69
70         if ((c == -1) && (input.length() == 0)) {
71             return null;
72         }
73         return input.toString();
74     }   
75         
76         
77         @Override
78         public int readUnsignedByte() throws IOException {
79                 return buf.get() & 0x000000ff;
80         }               
81         
82         @Override
83         public boolean readBoolean() throws IOException {
84                 return buf.get()!=0;
85         }
86
87         @Override
88         public void readFully(byte[] dst, int offset, int length) {
89                 buf.get(dst, offset, length);
90         }
91
92         @Override
93         public void readFully(byte[] dst) {
94                 buf.get(dst);
95         }
96
97         @Override
98         public void readFully(ByteBuffer buf) {         
99                 if (buf.hasArray()) {
100                         this.buf.get(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining());
101                         buf.position(buf.capacity());
102                 } else {
103                         buf.put(buf);
104                 }
105         }
106
107         @Override
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);
112                 } else {
113 //                      int len = Math.min( Math.min( buf.remaining(), this.buf.remaining() ), length);
114                         int len = length;
115                         int origLimit = this.buf.limit();
116                         try {
117                                 this.buf.limit(this.buf.position()+len);
118                                 buf.put(this.buf);
119                         } finally {
120                                 this.buf.limit(origLimit);
121                         }
122                 }
123         }
124
125         @Override
126         public double readDouble() {
127                 return buf.getDouble();
128         }
129
130         @Override
131         public float readFloat() {
132                 return buf.getFloat();
133         }
134
135         @Override
136         public int readInt() {
137                 return buf.getInt();
138         }
139
140         @Override
141         public long readLong() {
142                 return buf.getLong();
143         }
144
145         @Override
146         public short readShort() {
147                 return buf.getShort();
148         }
149         
150     public final String readUTF() throws IOException {
151         return DataInputStream.readUTF(this);
152     } 
153     
154         @Override
155         public char readChar() throws IOException {
156                 return buf.getChar();
157         }
158
159         @Override
160         public int readUnsignedShort() {
161                 return buf.getShort() & 0xffff;
162         }
163         
164         @Override
165         public long length() {
166                 return buf.limit();
167         }
168         
169         @Override
170         public long position() {
171                 return buf.position();
172         }
173         
174         public ByteOrder order() {
175                 return buf.order();
176         }
177
178         public void order(ByteOrder order) {
179                 buf.order(order);
180         }
181
182         public void position(int newPosition) throws IOException {
183                 buf.position(newPosition);
184         }
185
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);
189         }
190
191         @Override
192         public long skipBytes(long bytes) throws IOException {
193                 long newPosition = bytes + position();
194                 position( newPosition );
195                 return bytes;
196         }
197
198         @Override
199         public int skipBytes(int bytes) throws IOException {
200                 long newPosition = bytes + position();
201                 position( newPosition );
202                 return bytes;
203         }
204         
205 }