]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/InputStreamReadable.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / binary / InputStreamReadable.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.EOFException;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.nio.ByteBuffer;
19
20 import org.simantics.databoard.util.StreamUtil;
21
22 /**
23  * Input stream reader
24  * 
25  * @author Toni Kalajainen (toni.kalajainen@vtt.fi)
26  */
27 public class InputStreamReadable implements BinaryReadable {
28
29         InputStream is;
30         long limit, position;
31         
32         public static BinaryReadable readFully(InputStream is) throws IOException {
33                 byte[] data = StreamUtil.readFully(is);
34                 return new BinaryMemory( data );
35         }
36         
37         public InputStreamReadable(InputStream is, long limit)
38         {
39                 this.is = is;
40                 this.limit = limit;
41         }
42
43         /**
44          * Get next byte
45          * @return 0..255
46          * @throws IOException
47          */
48         int _get()
49         throws IOException
50         {
51                 int value = is.read();
52                 if (value==-1)
53                         throw new EOFException();
54                 position++;
55                 return value & 0xff;
56         }
57         
58         /**
59          * Get next byte
60          * @return 0..255 or -1 on end of file
61          * @throws IOException
62          */
63         int _read()
64         throws IOException
65         {
66                 int value = is.read();
67                 if (value==-1) return -1;
68                 position++;             
69                 return value & 0xff;
70         }       
71         
72     public final String readLine() throws IOException {
73         StringBuffer input = new StringBuffer();
74         int c = -1;
75         boolean eol = false;
76
77         while (!eol) {
78             switch (c = _read()) {
79             case -1:
80             case '\n':
81                 eol = true;
82                 break;
83             case '\r':
84                 eol = true;
85 //              long cur = position();
86 //              if ((_read()) != '\n') {
87 //                  position=cur;
88 //              }
89                 break;
90             default:
91                 input.append((char)c);
92                 break;
93             }
94         }
95
96         if ((c == -1) && (input.length() == 0)) {
97             return null;
98         }
99         return input.toString();
100     }   
101         
102         
103         @Override
104         public byte readByte() 
105     throws IOException  
106         {
107                 return (byte) _get();
108         }
109         
110         @Override
111         public char readChar() throws IOException {
112                 return (char) ( (_get() << 8) |  _get() ) ;
113         }
114         
115         @Override
116         public int readUnsignedByte() throws IOException {
117                 return _get() & 0x000000ff;
118         }       
119
120         @Override
121         public boolean readBoolean() 
122     throws IOException  
123         {
124                 return _get()!=0;
125         }       
126
127         @Override
128         public void readFully(byte[] dst, int offset, int length) 
129     throws IOException  
130         {
131                 while (length>0) {
132                         int bytesRead = is.read(dst, offset, length);
133                         if (bytesRead==-1) throw new EOFException();
134                         position+=bytesRead;                    
135                         offset += bytesRead;
136                         length -= bytesRead;
137                 }
138         }
139
140         @Override
141         public void readFully(byte[] dst) 
142     throws IOException  
143         {
144                 readFully(dst, 0, dst.length);
145         }
146
147         @Override
148         public void readFully(ByteBuffer buf) 
149     throws IOException  
150         {               
151                 for (;buf.hasRemaining();)
152                         buf.put((byte)_get());          
153         }
154
155         @Override
156         public void readFully(ByteBuffer buf, int length) 
157     throws IOException  
158         {
159                 if (length<256) {
160                         for (int i=0; i<length; i++)
161                                 buf.put((byte)_get());
162                 } else {
163                         byte[] b = new byte[length];
164                         readFully(b, 0, length);
165                         buf.put(b);
166                 }
167         }
168
169         @Override
170         public double readDouble() 
171     throws IOException  
172         {
173                 return Double.longBitsToDouble(readLong());
174         }
175
176         @Override
177         public float readFloat() 
178     throws IOException  
179         {
180                 return Float.intBitsToFloat(readInt());
181         }
182         
183     public final String readUTF() throws IOException {
184         return DataInputStream.readUTF(this);
185     }   
186
187         @Override
188         public int readInt() 
189     throws IOException  
190         {
191                 return 
192                         ( _get() << 24) |
193                         ( _get() << 16) | 
194                         ( _get() << 8) |
195                         ( _get() );
196         }
197
198         @Override
199         public long readLong() 
200     throws IOException  
201         {
202                 return
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) |
210                 ( ((long)_get()) );             
211         }
212
213         @Override
214         public short readShort() 
215     throws IOException  
216         {
217                 return (short) ( (_get() << 8) |  _get() ) ;
218         }
219
220         @Override
221         public int readUnsignedShort() 
222     throws IOException  
223         {
224                 return ( (_get() << 8) |  _get() ) ;
225         }       
226         
227         @Override
228         public long length() 
229         {
230                 return limit;
231         }
232         
233         @Override
234         public long position() {
235                 return position;
236         }
237
238
239         @Override
240         public long skipBytes(long bytes) throws IOException {
241                 is.skip(bytes); 
242                 return bytes;
243         }
244
245         @Override
246         public int skipBytes(int bytes) throws IOException {
247                 is.skip(bytes); 
248                 return bytes;
249         }
250         
251 }
252