libjava/
[official-gcc.git] / libjava / classpath / gnu / CORBA / CDR / LittleEndianInputStream.java
bloba439b658b440692f0aa65176f3979cf4af20c233
1 /* LittleEndianInputStream.java --
2 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.CORBA.CDR;
41 import java.io.EOFException;
42 import java.io.FilterInputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.PushbackInputStream;
47 /**
48 * This class reads data in the Little Endian format. It reuses
49 * code from GNU Classpath DataInputStream.
51 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
52 * @author Warren Levy (warrenl@cygnus.com)
53 * @author Aaron M. Renn (arenn@urbanophile.com)
55 public class LittleEndianInputStream
56 extends FilterInputStream
57 implements AbstractDataInput
59 // Byte buffer, used to make primitive read calls more efficient.
60 byte[] buf = new byte[ 8 ];
62 /**
63 * This constructor initializes a new <code>DataInputStream</code>
64 * to read from the specified subordinate stream.
66 * @param in The subordinate <code>InputStream</code> to read from
68 public LittleEndianInputStream(InputStream in)
70 super(in);
73 /**
74 * This method reads bytes from the underlying stream into the specified
75 * byte array buffer. It will attempt to fill the buffer completely, but
76 * may return a short count if there is insufficient data remaining to be
77 * read to fill the buffer.
79 * @param b The buffer into which bytes will be read.
81 * @return The actual number of bytes read, or -1 if end of stream reached
82 * before reading any bytes.
84 * @exception IOException If an error occurs.
86 public int read(byte[] b)
87 throws IOException
89 return in.read(b, 0, b.length);
92 /**
93 * This method reads bytes from the underlying stream into the specified
94 * byte array buffer. It will attempt to read <code>len</code> bytes and
95 * will start storing them at position <code>off</code> into the buffer.
96 * This method can return a short count if there is insufficient data
97 * remaining to be read to complete the desired read length.
99 * @param b The buffer into which bytes will be read.
100 * @param off The offset into the buffer to start storing bytes.
101 * @param len The requested number of bytes to read.
103 * @return The actual number of bytes read, or -1 if end of stream reached
104 * before reading any bytes.
106 * @exception IOException If an error occurs.
108 public int read(byte[] b, int off, int len)
109 throws IOException
111 return in.read(b, off, len);
115 * This method reads a Java boolean value from an input stream. It does
116 * so by reading a single byte of data. If that byte is zero, then the
117 * value returned is <code>false</code>. If the byte is non-zero, then
118 * the value returned is <code>true</code>.
119 * <p>
120 * This method can read a <code>boolean</code> written by an object
121 * implementing the <code>writeBoolean()</code> method in the
122 * <code>DataOutput</code> interface.
124 * @return The <code>boolean</code> value read
126 * @exception EOFException If end of file is reached before reading
127 * the boolean
128 * @exception IOException If any other error occurs
130 * @see DataOutput#writeBoolean
132 public boolean readBoolean()
133 throws IOException
135 return convertToBoolean(in.read());
139 * This method reads a Java byte value from an input stream. The value
140 * is in the range of -128 to 127.
141 * <p>
142 * This method can read a <code>byte</code> written by an object
143 * implementing the <code>writeByte()</code> method in the
144 * <code>DataOutput</code> interface.
146 * @return The <code>byte</code> value read
148 * @exception EOFException If end of file is reached before reading the byte
149 * @exception IOException If any other error occurs
151 * @see DataOutput#writeByte
153 public byte readByte()
154 throws IOException
156 return convertToByte(in.read());
160 * This method reads a Java <code>char</code> value from an input stream.
161 * It operates by reading two bytes from the stream and converting them to
162 * a single 16-bit Java <code>char</code>. The two bytes are stored most
163 * significant byte first (i.e., "big endian") regardless of the native
164 * host byte ordering.
165 * <p>
166 * As an example, if <code>byte1</code> and <code>byte2</code>
167 * represent the first and second byte read from the stream
168 * respectively, they will be transformed to a <code>char</code> in
169 * the following manner:
170 * <p>
171 * <code>(char)(((byte1 &amp; 0xFF) &lt;&lt; 8) | (byte2 &amp; 0xFF)</code>
172 * <p>
173 * This method can read a <code>char</code> written by an object
174 * implementing the <code>writeChar()</code> method in the
175 * <code>DataOutput</code> interface.
177 * @return The <code>char</code> value read
179 * @exception EOFException If end of file is reached before reading the char
180 * @exception IOException If any other error occurs
182 * @see DataOutput#writeChar
184 public char readChar()
185 throws IOException
187 readFully(buf, 0, 2);
188 return convertToChar(buf);
192 * This method reads a Java double value from an input stream. It operates
193 * by first reading a <code>long</code> value from the stream by calling the
194 * <code>readLong()</code> method in this interface, then converts
195 * that <code>long</code> to a <code>double</code> using the
196 * <code>longBitsToDouble</code> method in the class
197 * <code>java.lang.Double</code>
198 * <p>
199 * This method can read a <code>double</code> written by an object
200 * implementing the <code>writeDouble()</code> method in the
201 * <code>DataOutput</code> interface.
203 * @return The <code>double</code> value read
205 * @exception EOFException If end of file is reached before reading
206 * the double
207 * @exception IOException If any other error occurs
209 * @see DataOutput#writeDouble
210 * @see java.lang.Double#longBitsToDouble
212 public double readDouble()
213 throws IOException
215 return Double.longBitsToDouble(readLong());
219 * This method reads a Java float value from an input stream. It
220 * operates by first reading an <code>int</code> value from the
221 * stream by calling the <code>readInt()</code> method in this
222 * interface, then converts that <code>int</code> to a
223 * <code>float</code> using the <code>intBitsToFloat</code> method
224 * in the class <code>java.lang.Float</code>
225 * <p>
226 * This method can read a <code>float</code> written by an object
227 * implementing the <code>writeFloat()</code> method in the
228 * <code>DataOutput</code> interface.
230 * @return The <code>float</code> value read
232 * @exception EOFException If end of file is reached before reading the float
233 * @exception IOException If any other error occurs
235 * @see DataOutput#writeFloat
236 * @see java.lang.Float#intBitsToFloat
238 public float readFloat()
239 throws IOException
241 return Float.intBitsToFloat(readInt());
245 * This method reads raw bytes into the passed array until the array is
246 * full. Note that this method blocks until the data is available and
247 * throws an exception if there is not enough data left in the stream to
248 * fill the buffer. Note also that zero length buffers are permitted.
249 * In this case, the method will return immediately without reading any
250 * bytes from the stream.
252 * @param b The buffer into which to read the data
254 * @exception EOFException If end of file is reached before filling the
255 * buffer
256 * @exception IOException If any other error occurs
258 public void readFully(byte[] b)
259 throws IOException
261 readFully(b, 0, b.length);
265 * This method reads raw bytes into the passed array <code>buf</code>
266 * starting
267 * <code>offset</code> bytes into the buffer. The number of bytes read
268 * will be
269 * exactly <code>len</code>. Note that this method blocks until the data is
270 * available and throws an exception if there is not enough data left in
271 * the stream to read <code>len</code> bytes. Note also that zero length
272 * buffers are permitted. In this case, the method will return immediately
273 * without reading any bytes from the stream.
275 * @param buf The buffer into which to read the data
276 * @param offset The offset into the buffer to start storing data
277 * @param len The number of bytes to read into the buffer
279 * @exception EOFException If end of file is reached before filling the
280 * buffer
281 * @exception IOException If any other error occurs
283 public void readFully(byte[] buf, int offset, int len)
284 throws IOException
286 if (len < 0)
287 throw new IndexOutOfBoundsException("Negative length: " + len);
289 while (len > 0)
291 // in.read will block until some data is available.
292 int numread = in.read(buf, offset, len);
293 if (numread < 0)
294 throw new EOFException();
295 len -= numread;
296 offset += numread;
301 * This method reads a Java <code>int</code> value from an input stream
302 * It operates by reading four bytes from the stream and converting them to
303 * a single Java <code>int</code>. The bytes are stored most
304 * significant byte first (i.e., "big endian") regardless of the native
305 * host byte ordering.
306 * <p>
307 * As an example, if <code>byte1</code> through <code>byte4</code> represent
308 * the first four bytes read from the stream, they will be
309 * transformed to an <code>int</code> in the following manner:
310 * <p>
311 * <code>(int)(((byte1 &amp; 0xFF) &lt;&lt; 24) + ((byte2 &amp; 0xFF) &lt;&lt; 16) +
312 * ((byte3 &amp; 0xFF)&lt;&lt; 8) + (byte4 &amp; 0xFF)))</code>
313 * <p>
314 * The value returned is in the range of -2147483648 to 2147483647.
315 * <p>
316 * This method can read an <code>int</code> written by an object
317 * implementing the <code>writeInt()</code> method in the
318 * <code>DataOutput</code> interface.
320 * @return The <code>int</code> value read
322 * @exception EOFException If end of file is reached before reading the int
323 * @exception IOException If any other error occurs
325 * @see DataOutput#writeInt
327 public int readInt()
328 throws IOException
330 readFully(buf, 0, 4);
331 return convertToInt(buf);
335 * This method reads the next line of text data from an input
336 * stream. It operates by reading bytes and converting those bytes
337 * to <code>char</code> values by treating the byte read as the low
338 * eight bits of the <code>char</code> and using 0 as the high eight
339 * bits. Because of this, it does not support the full 16-bit
340 * Unicode character set.
341 * <p>
342 * The reading of bytes ends when either the end of file or a line
343 * terminator is encountered. The bytes read are then returned as a
344 * <code>String</code> A line terminator is a byte sequence
345 * consisting of either <code>\r</code>, <code>\n</code> or
346 * <code>\r\n</code>. These termination charaters are discarded and
347 * are not returned as part of the string.
348 * <p>
349 * This method can read data that was written by an object implementing the
350 * <code>writeLine()</code> method in <code>DataOutput</code>.
352 * @return The line read as a <code>String</code>
354 * @exception IOException If an error occurs
356 * @see DataOutput
358 * @deprecated
360 public String readLine()
361 throws IOException
363 StringBuffer strb = new StringBuffer();
365 while (true)
367 int c = in.read();
368 if (c == -1) // got an EOF
369 return strb.length() > 0 ? strb.toString() : null;
370 if (c == '\r')
372 int next_c = in.read();
373 if (next_c != '\n' && next_c != -1)
375 if (!(in instanceof PushbackInputStream))
376 in = new PushbackInputStream(in);
377 ((PushbackInputStream) in).unread(next_c);
379 break;
381 if (c == '\n')
382 break;
383 strb.append((char) c);
386 return strb.length() > 0 ? strb.toString() : "";
390 * This method reads a Java <code>long</code> value from an input stream
391 * It operates by reading eight bytes from the stream and converting them to
392 * a single Java <code>long</code>. The bytes are stored most
393 * significant byte first (i.e., "big endian") regardless of the native
394 * host byte ordering.
395 * <p>
396 * As an example, if <code>byte1</code> through <code>byte8</code> represent
397 * the first eight bytes read from the stream, they will be
398 * transformed to an <code>long</code> in the following manner:
399 * <p>
400 * <code>(long)(((byte1 &amp; 0xFF) &lt;&lt; 56) + ((byte2 &amp; 0xFF) &lt;&lt; 48) +
401 * ((byte3 &amp; 0xFF) &lt;&lt; 40) + ((byte4 &amp; 0xFF) &lt;&lt; 32) +
402 * ((byte5 &amp; 0xFF) &lt;&lt; 24) + ((byte6 &amp; 0xFF) &lt;&lt; 16) +
403 * ((byte7 &amp; 0xFF) &lt;&lt; 8) + (byte8 &amp; 0xFF)))
404 * </code>
405 * <p>
406 * The value returned is in the range of -9223372036854775808 to
407 * 9223372036854775807.
408 * <p>
409 * This method can read an <code>long</code> written by an object
410 * implementing the <code>writeLong()</code> method in the
411 * <code>DataOutput</code> interface.
413 * @return The <code>long</code> value read
415 * @exception EOFException If end of file is reached before reading the long
416 * @exception IOException If any other error occurs
418 * @see DataOutput#writeLong
420 public long readLong()
421 throws IOException
423 readFully(buf, 0, 8);
424 return convertToLong(buf);
428 * This method reads a signed 16-bit value into a Java in from the
429 * stream. It operates by reading two bytes from the stream and
430 * converting them to a single 16-bit Java <code>short</code>. The
431 * two bytes are stored most significant byte first (i.e., "big
432 * endian") regardless of the native host byte ordering.
433 * <p>
434 * As an example, if <code>byte1</code> and <code>byte2</code>
435 * represent the first and second byte read from the stream
436 * respectively, they will be transformed to a <code>short</code>. in
437 * the following manner:
438 * <p>
439 * <code>(short)(((byte1 &amp; 0xFF) &lt;&lt; 8) | (byte2 &amp; 0xFF))</code>
440 * <p>
441 * The value returned is in the range of -32768 to 32767.
442 * <p>
443 * This method can read a <code>short</code> written by an object
444 * implementing the <code>writeShort()</code> method in the
445 * <code>DataOutput</code> interface.
447 * @return The <code>short</code> value read
449 * @exception EOFException If end of file is reached before reading the value
450 * @exception IOException If any other error occurs
452 * @see DataOutput#writeShort
454 public short readShort()
455 throws IOException
457 readFully(buf, 0, 2);
458 return convertToShort(buf);
462 * This method reads 8 unsigned bits into a Java <code>int</code>
463 * value from the stream. The value returned is in the range of 0 to
464 * 255.
465 * <p>
466 * This method can read an unsigned byte written by an object
467 * implementing the <code>writeUnsignedByte()</code> method in the
468 * <code>DataOutput</code> interface.
470 * @return The unsigned bytes value read as a Java <code>int</code>.
472 * @exception EOFException If end of file is reached before reading the value
473 * @exception IOException If any other error occurs
475 * @see DataOutput#writeByte
477 public int readUnsignedByte()
478 throws IOException
480 return convertToUnsignedByte(in.read());
484 * This method reads 16 unsigned bits into a Java int value from the stream.
485 * It operates by reading two bytes from the stream and converting them to
486 * a single Java <code>int</code> The two bytes are stored most
487 * significant byte first (i.e., "big endian") regardless of the native
488 * host byte ordering.
489 * <p>
490 * As an example, if <code>byte1</code> and <code>byte2</code>
491 * represent the first and second byte read from the stream
492 * respectively, they will be transformed to an <code>int</code> in
493 * the following manner:
494 * <p>
495 * <code>(int)(((byte1 &amp; 0xFF) &lt;&lt; 8) + (byte2 &amp; 0xFF))</code>
496 * <p>
497 * The value returned is in the range of 0 to 65535.
498 * <p>
499 * This method can read an unsigned short written by an object
500 * implementing the <code>writeUnsignedShort()</code> method in the
501 * <code>DataOutput</code> interface.
503 * @return The unsigned short value read as a Java <code>int</code>
505 * @exception EOFException If end of file is reached before reading the value
506 * @exception IOException If any other error occurs
508 * @see DataOutput#writeShort
510 public int readUnsignedShort()
511 throws IOException
513 readFully(buf, 0, 2);
514 return convertToUnsignedShort(buf);
518 * This method attempts to skip and discard the specified number of bytes
519 * in the input stream. It may actually skip fewer bytes than requested.
520 * This method will not skip any bytes if passed a negative number of bytes
521 * to skip.
523 * @param n The requested number of bytes to skip.
525 * @return The requested number of bytes to skip.
527 * @exception IOException If an error occurs.
528 * @specnote The JDK docs claim that this returns the number of bytes
529 * actually skipped. The JCL claims that this method can throw an
530 * EOFException. Neither of these appear to be true in the JDK 1.3's
531 * implementation. This tries to implement the actual JDK behaviour.
533 public int skipBytes(int n)
534 throws IOException
536 if (n <= 0)
537 return 0;
540 return (int) in.skip(n);
542 catch (EOFException x)
544 // do nothing.
546 return n;
549 protected boolean convertToBoolean(int b)
550 throws EOFException
552 if (b < 0)
553 throw new EOFException();
555 return (b != 0);
558 protected byte convertToByte(int i)
559 throws EOFException
561 if (i < 0)
562 throw new EOFException();
564 return (byte) i;
567 protected int convertToUnsignedByte(int i)
568 throws EOFException
570 if (i < 0)
571 throw new EOFException();
573 return (i & 0xFF);
577 * Less significant byte first.
579 protected char convertToChar(byte[] buf)
581 return (char) ((buf [ 1 ] << 8) | (buf [ 0 ] & 0xff));
585 * Less significant byte first.
587 protected short convertToShort(byte[] buf)
589 return (short) ((buf [ 1 ] << 8) | (buf [ 0 ] & 0xff));
593 * Less significant byte first.
595 protected int convertToUnsignedShort(byte[] buf)
597 return (((buf [ 1 ] & 0xff) << 8) | (buf [ 0 ] & 0xff));
601 * Less significant byte first.
603 protected int convertToInt(byte[] buf)
605 return (((buf [ 3 ] & 0xff) << 24) | ((buf [ 2 ] & 0xff) << 16) |
606 ((buf [ 1 ] & 0xff) << 8) | (buf [ 0 ] & 0xff));
610 * Less significant byte first.
612 protected long convertToLong(byte[] buf)
614 return (((long) (buf [ 7 ] & 0xff) << 56) |
615 ((long) (buf [ 6 ] & 0xff) << 48) |
616 ((long) (buf [ 5 ] & 0xff) << 40) |
617 ((long) (buf [ 4 ] & 0xff) << 32) |
618 ((long) (buf [ 3 ] & 0xff) << 24) |
619 ((long) (buf [ 2 ] & 0xff) << 16) |
620 ((long) (buf [ 1 ] & 0xff) << 8) | ((long) (buf [ 0 ] & 0xff)));
624 * This should never be called.
626 * @throws InternalError, always.
628 public String readUTF()
630 throw new InternalError();