]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.util.binary;
13
14 import java.io.DataInputStream;\r
15 import java.io.IOException;\r
16 import java.nio.ByteBuffer;\r
17 import java.nio.ByteOrder;\r
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         }\r
40                 \r
41         int _read() {\r
42                 if (buf.position()>=buf.limit()) return -1;\r
43                 return buf.get() & 0xff;\r
44         }\r
45         \r
46     public final String readLine() throws IOException {\r
47         StringBuffer input = new StringBuffer();\r
48         int c = -1;\r
49         boolean eol = false;\r
50 \r
51         while (!eol) {\r
52             switch (c = _read()) {\r
53             case -1:\r
54             case '\n':\r
55                 eol = true;\r
56                 break;\r
57             case '\r':\r
58                 eol = true;\r
59                 long cur = position();\r
60                 if ((_read()) != '\n') {\r
61                     position(cur);\r
62                 }\r
63                 break;\r
64             default:\r
65                 input.append((char)c);\r
66                 break;\r
67             }\r
68         }\r
69 \r
70         if ((c == -1) && (input.length() == 0)) {\r
71             return null;\r
72         }\r
73         return input.toString();\r
74     }   \r
75         \r
76         \r
77         @Override\r
78         public int readUnsignedByte() throws IOException {\r
79                 return buf.get() & 0x000000ff;\r
80         }               \r
81         
82         @Override\r
83         public boolean readBoolean() throws IOException {\r
84                 return buf.get()!=0;\r
85         }\r
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         }\r
149         \r
150     public final String readUTF() throws IOException {\r
151         return DataInputStream.readUTF(this);\r
152     } \r
153     \r
154         @Override\r
155         public char readChar() throws IOException {\r
156                 return buf.getChar();\r
157         }
158 \r
159         @Override\r
160         public int readUnsignedShort() {\r
161                 return buf.getShort() & 0xffff;\r
162         }\r
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 );\r
195                 return bytes;
196         }
197 \r
198         @Override\r
199         public int skipBytes(int bytes) throws IOException {\r
200                 long newPosition = bytes + position();\r
201                 position( newPosition );\r
202                 return bytes;\r
203         }\r
204         
205 }