OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [java/] [io/] [DataInputStream.java] - Blame information for rev 777

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 771 jeremybenn
/* DataInputStream.java -- FilteredInputStream that implements DataInput
2
   Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005, 2008
3
   Free Software Foundation
4
 
5
This file is part of GNU Classpath.
6
 
7
GNU Classpath is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2, or (at your option)
10
any later version.
11
 
12
GNU Classpath is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with GNU Classpath; see the file COPYING.  If not, write to the
19
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
02110-1301 USA.
21
 
22
Linking this library statically or dynamically with other modules is
23
making a combined work based on this library.  Thus, the terms and
24
conditions of the GNU General Public License cover the whole
25
combination.
26
 
27
As a special exception, the copyright holders of this library give you
28
permission to link this library with independent modules to produce an
29
executable, regardless of the license terms of these independent
30
modules, and to copy and distribute the resulting executable under
31
terms of your choice, provided that you also meet, for each linked
32
independent module, the terms and conditions of the license of that
33
module.  An independent module is a module which is not derived from
34
or based on this library.  If you modify this library, you may extend
35
this exception to your version of the library, but you are not
36
obligated to do so.  If you do not wish to do so, delete this
37
exception statement from your version. */
38
 
39
package java.io;
40
 
41
import gnu.java.lang.CPStringBuilder;
42
 
43
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
44
 * "The Java Language Specification", ISBN 0-201-63451-1
45
 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
46
 * Status:  Believed complete and correct.
47
 */
48
 
49
/**
50
 * This subclass of <code>FilteredInputStream</code> implements the
51
 * <code>DataInput</code> interface that provides method for reading primitive
52
 * Java data types from a stream.
53
 *
54
 * @see DataInput
55
 *
56
 * @author Warren Levy (warrenl@cygnus.com)
57
 * @author Aaron M. Renn (arenn@urbanophile.com)
58
 * @date October 20, 1998.
59
 */
60
public class DataInputStream extends FilterInputStream implements DataInput
61
{
62
  // Byte buffer, used to make primitive read calls more efficient.
63
  byte[] buf = new byte [8];
64
 
65
  /**
66
   * This constructor initializes a new <code>DataInputStream</code>
67
   * to read from the specified subordinate stream.
68
   *
69
   * @param in The subordinate <code>InputStream</code> to read from
70
   */
71
  public DataInputStream (InputStream in)
72
  {
73
    super (in);
74
  }
75
 
76
  /**
77
   * This method reads bytes from the underlying stream into the specified
78
   * byte array buffer.  It will attempt to fill the buffer completely, but
79
   * may return a short count if there is insufficient data remaining to be
80
   * read to fill the buffer.
81
   *
82
   * @param b The buffer into which bytes will be read.
83
   *
84
   * @return The actual number of bytes read, or -1 if end of stream reached
85
   * before reading any bytes.
86
   *
87
   * @exception IOException If an error occurs.
88
   */
89
  public final int read (byte[] b) throws IOException
90
  {
91
    return in.read (b, 0, b.length);
92
  }
93
 
94
  /**
95
   * This method reads bytes from the underlying stream into the specified
96
   * byte array buffer.  It will attempt to read <code>len</code> bytes and
97
   * will start storing them at position <code>off</code> into the buffer.
98
   * This method can return a short count if there is insufficient data
99
   * remaining to be read to complete the desired read length.
100
   *
101
   * @param b The buffer into which bytes will be read.
102
   * @param off The offset into the buffer to start storing bytes.
103
   * @param len The requested number of bytes to read.
104
   *
105
   * @return The actual number of bytes read, or -1 if end of stream reached
106
   * before reading any bytes.
107
   *
108
   * @exception IOException If an error occurs.
109
   */
110
  public final int read (byte[] b, int off, int len) throws IOException
111
  {
112
    return in.read (b, off, len);
113
  }
114
 
115
  /**
116
   * This method reads a Java boolean value from an input stream.  It does
117
   * so by reading a single byte of data.  If that byte is zero, then the
118
   * value returned is <code>false</code>.  If the byte is non-zero, then
119
   * the value returned is <code>true</code>.
120
   * <p>
121
   * This method can read a <code>boolean</code> written by an object
122
   * implementing the <code>writeBoolean()</code> method in the
123
   * <code>DataOutput</code> interface.
124
   *
125
   * @return The <code>boolean</code> value read
126
   *
127
   * @exception EOFException If end of file is reached before reading
128
   * the boolean
129
   * @exception IOException If any other error occurs
130
   *
131
   * @see DataOutput#writeBoolean
132
   */
133
  public final boolean readBoolean () throws IOException
134
  {
135
    return convertToBoolean (in.read ());
136
  }
137
 
138
  /**
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.
145
   *
146
   * @return The <code>byte</code> value read
147
   *
148
   * @exception EOFException If end of file is reached before reading the byte
149
   * @exception IOException If any other error occurs
150
   *
151
   * @see DataOutput#writeByte
152
   */
153
  public final byte readByte () throws IOException
154
  {
155
    return convertToByte (in.read ());
156
  }
157
 
158
  /**
159
   * This method reads a Java <code>char</code> value from an input stream.
160
   * It operates by reading two bytes from the stream and converting them to
161
   * a single 16-bit Java <code>char</code>.  The two bytes are stored most
162
   * significant byte first (i.e., "big endian") regardless of the native
163
   * host byte ordering.
164
   * <p>
165
   * As an example, if <code>byte1</code> and <code>byte2</code>
166
   * represent the first and second byte read from the stream
167
   * respectively, they will be transformed to a <code>char</code> in
168
   * the following manner:
169
   * <p>
170
   * <code>(char)(((byte1 &amp; 0xFF) &lt;&lt; 8) | (byte2 &amp; 0xFF)</code>
171
   * <p>
172
   * This method can read a <code>char</code> written by an object
173
   * implementing the <code>writeChar()</code> method in the
174
   * <code>DataOutput</code> interface.
175
   *
176
   * @return The <code>char</code> value read
177
   *
178
   * @exception EOFException If end of file is reached before reading the char
179
   * @exception IOException If any other error occurs
180
   *
181
   * @see DataOutput#writeChar
182
   */
183
  public final char readChar () throws IOException
184
  {
185
    readFully (buf, 0, 2);
186
    return convertToChar (buf);
187
  }
188
 
189
  /**
190
   * This method reads a Java double value from an input stream.  It operates
191
   * by first reading a <code>long</code> value from the stream by calling the
192
   * <code>readLong()</code> method in this interface, then converts
193
   * that <code>long</code> to a <code>double</code> using the
194
   * <code>longBitsToDouble</code> method in the class
195
   * <code>java.lang.Double</code>
196
   * <p>
197
   * This method can read a <code>double</code> written by an object
198
   * implementing the <code>writeDouble()</code> method in the
199
   * <code>DataOutput</code> interface.
200
   *
201
   * @return The <code>double</code> value read
202
   *
203
   * @exception EOFException If end of file is reached before reading
204
   * the double
205
   * @exception IOException If any other error occurs
206
   *
207
   * @see DataOutput#writeDouble
208
   * @see java.lang.Double#longBitsToDouble
209
   */
210
  public final double readDouble () throws IOException
211
  {
212
    return Double.longBitsToDouble (readLong ());
213
  }
214
 
215
  /**
216
   * This method reads a Java float value from an input stream.  It
217
   * operates by first reading an <code>int</code> value from the
218
   * stream by calling the <code>readInt()</code> method in this
219
   * interface, then converts that <code>int</code> to a
220
   * <code>float</code> using the <code>intBitsToFloat</code> method
221
   * in the class <code>java.lang.Float</code>
222
   * <p>
223
   * This method can read a <code>float</code> written by an object
224
   * implementing the <code>writeFloat()</code> method in the
225
   * <code>DataOutput</code> interface.
226
   *
227
   * @return The <code>float</code> value read
228
   *
229
   * @exception EOFException If end of file is reached before reading the float
230
   * @exception IOException If any other error occurs
231
   *
232
   * @see DataOutput#writeFloat
233
   * @see java.lang.Float#intBitsToFloat
234
   */
235
  public final float readFloat () throws IOException
236
  {
237
    return Float.intBitsToFloat (readInt ());
238
  }
239
 
240
  /**
241
   * This method reads raw bytes into the passed array until the array is
242
   * full.  Note that this method blocks until the data is available and
243
   * throws an exception if there is not enough data left in the stream to
244
   * fill the buffer.  Note also that zero length buffers are permitted.
245
   * In this case, the method will return immediately without reading any
246
   * bytes from the stream.
247
   *
248
   * @param b The buffer into which to read the data
249
   *
250
   * @exception EOFException If end of file is reached before filling the
251
   * buffer
252
   * @exception IOException If any other error occurs
253
   */
254
  public final void readFully (byte[] b) throws IOException
255
  {
256
    readFully (b, 0, b.length);
257
  }
258
 
259
  /**
260
   * This method reads raw bytes into the passed array <code>buf</code>
261
   * starting
262
   * <code>offset</code> bytes into the buffer.  The number of bytes read
263
   * will be
264
   * exactly <code>len</code>.  Note that this method blocks until the data is
265
   * available and throws an exception if there is not enough data left in
266
   * the stream to read <code>len</code> bytes.  Note also that zero length
267
   * buffers are permitted.  In this case, the method will return immediately
268
   * without reading any bytes from the stream.
269
   *
270
   * @param buf The buffer into which to read the data
271
   * @param offset The offset into the buffer to start storing data
272
   * @param len The number of bytes to read into the buffer
273
   *
274
   * @exception EOFException If end of file is reached before filling the
275
   * buffer
276
   * @exception IOException If any other error occurs
277
   */
278
  public final void readFully (byte[] buf, int offset, int len) throws IOException
279
  {
280
    if (len < 0)
281
      throw new IndexOutOfBoundsException("Negative length: " + len);
282
 
283
    while (len > 0)
284
      {
285
        // in.read will block until some data is available.
286
        int numread = in.read (buf, offset, len);
287
        if (numread < 0)
288
          throw new EOFException ();
289
        len -= numread;
290
        offset += numread;
291
      }
292
  }
293
 
294
  /**
295
   * This method reads a Java <code>int</code> value from an input stream
296
   * It operates by reading four bytes from the stream and converting them to
297
   * a single Java <code>int</code>.  The bytes are stored most
298
   * significant byte first (i.e., "big endian") regardless of the native
299
   * host byte ordering.
300
   * <p>
301
   * As an example, if <code>byte1</code> through <code>byte4</code> represent
302
   * the first four bytes read from the stream, they will be
303
   * transformed to an <code>int</code> in the following manner:
304
   * <p>
305
   * <code>(int)(((byte1 &amp; 0xFF) &lt;&lt; 24) + ((byte2 &amp; 0xFF) &lt;&lt; 16) +
306
   * ((byte3 &amp; 0xFF)&lt;&lt; 8) + (byte4 &amp; 0xFF)))</code>
307
   * <p>
308
   * The value returned is in the range of -2147483648 to 2147483647.
309
   * <p>
310
   * This method can read an <code>int</code> written by an object
311
   * implementing the <code>writeInt()</code> method in the
312
   * <code>DataOutput</code> interface.
313
   *
314
   * @return The <code>int</code> value read
315
   *
316
   * @exception EOFException If end of file is reached before reading the int
317
   * @exception IOException If any other error occurs
318
   *
319
   * @see DataOutput#writeInt
320
   */
321
  public final int readInt () throws IOException
322
  {
323
    readFully (buf, 0, 4);
324
    return convertToInt (buf);
325
  }
326
 
327
  /**
328
   * This method reads the next line of text data from an input
329
   * stream.  It operates by reading bytes and converting those bytes
330
   * to <code>char</code> values by treating the byte read as the low
331
   * eight bits of the <code>char</code> and using 0 as the high eight
332
   * bits.  Because of this, it does not support the full 16-bit
333
   * Unicode character set.
334
   * <p>
335
   * The reading of bytes ends when either the end of file or a line
336
   * terminator is encountered.  The bytes read are then returned as a
337
   * <code>String</code> A line terminator is a byte sequence
338
   * consisting of either <code>\r</code>, <code>\n</code> or
339
   * <code>\r\n</code>.  These termination charaters are discarded and
340
   * are not returned as part of the string.
341
   * <p>
342
   * This method can read data that was written by an object implementing the
343
   * <code>writeLine()</code> method in <code>DataOutput</code>.
344
   *
345
   * @return The line read as a <code>String</code>
346
   *
347
   * @exception IOException If an error occurs
348
   *
349
   * @see DataOutput
350
   *
351
   * @deprecated
352
   */
353
  public final String readLine() throws IOException
354
  {
355
    CPStringBuilder strb = new CPStringBuilder();
356
 
357
    while (true)
358
      {
359
        int c = in.read();
360
        if (c == -1)    // got an EOF
361
            return strb.length() > 0 ? strb.toString() : null;
362
        if (c == '\r')
363
          {
364
            int next_c = in.read();
365
            if (next_c != '\n' && next_c != -1)
366
              {
367
                if (!(in instanceof PushbackInputStream))
368
                  in = new PushbackInputStream(in);
369
                ((PushbackInputStream) in).unread(next_c);
370
              }
371
            break;
372
          }
373
        if (c == '\n')
374
            break;
375
        strb.append((char) c);
376
      }
377
 
378
    return strb.length() > 0 ? strb.toString() : "";
379
  }
380
 
381
  /**
382
   * This method reads a Java <code>long</code> value from an input stream
383
   * It operates by reading eight bytes from the stream and converting them to
384
   * a single Java <code>long</code>.  The bytes are stored most
385
   * significant byte first (i.e., "big endian") regardless of the native
386
   * host byte ordering.
387
   * <p>
388
   * As an example, if <code>byte1</code> through <code>byte8</code> represent
389
   * the first eight bytes read from the stream, they will be
390
   * transformed to an <code>long</code> in the following manner:
391
   * <p>
392
   * <code>(long)(((byte1 &amp; 0xFF) &lt;&lt; 56) + ((byte2 &amp; 0xFF) &lt;&lt; 48) +
393
   * ((byte3 &amp; 0xFF) &lt;&lt; 40) + ((byte4 &amp; 0xFF) &lt;&lt; 32) +
394
   * ((byte5 &amp; 0xFF) &lt;&lt; 24) + ((byte6 &amp; 0xFF) &lt;&lt; 16) +
395
   * ((byte7 &amp; 0xFF) &lt;&lt; 8) + (byte8 &amp; 0xFF)))
396
   * </code>
397
   * <p>
398
   * The value returned is in the range of -9223372036854775808 to
399
   * 9223372036854775807.
400
   * <p>
401
   * This method can read an <code>long</code> written by an object
402
   * implementing the <code>writeLong()</code> method in the
403
   * <code>DataOutput</code> interface.
404
   *
405
   * @return The <code>long</code> value read
406
   *
407
   * @exception EOFException If end of file is reached before reading the long
408
   * @exception IOException If any other error occurs
409
   *
410
   * @see DataOutput#writeLong
411
   */
412
  public final long readLong () throws IOException
413
  {
414
    readFully (buf, 0, 8);
415
    return convertToLong (buf);
416
  }
417
 
418
  /**
419
   * This method reads a signed 16-bit value into a Java in from the
420
   * stream.  It operates by reading two bytes from the stream and
421
   * converting them to a single 16-bit Java <code>short</code>.  The
422
   * two bytes are stored most significant byte first (i.e., "big
423
   * endian") regardless of the native host byte ordering.
424
   * <p>
425
   * As an example, if <code>byte1</code> and <code>byte2</code>
426
   * represent the first and second byte read from the stream
427
   * respectively, they will be transformed to a <code>short</code>. in
428
   * the following manner:
429
   * <p>
430
   * <code>(short)(((byte1 &amp; 0xFF) &lt;&lt; 8) | (byte2 &amp; 0xFF))</code>
431
   * <p>
432
   * The value returned is in the range of -32768 to 32767.
433
   * <p>
434
   * This method can read a <code>short</code> written by an object
435
   * implementing the <code>writeShort()</code> method in the
436
   * <code>DataOutput</code> interface.
437
   *
438
   * @return The <code>short</code> value read
439
   *
440
   * @exception EOFException If end of file is reached before reading the value
441
   * @exception IOException If any other error occurs
442
   *
443
   * @see DataOutput#writeShort
444
   */
445
  public final short readShort () throws IOException
446
  {
447
    readFully (buf, 0, 2);
448
    return convertToShort (buf);
449
  }
450
 
451
  /**
452
   * This method reads 8 unsigned bits into a Java <code>int</code>
453
   * value from the stream. The value returned is in the range of 0 to
454
   * 255.
455
   * <p>
456
   * This method can read an unsigned byte written by an object
457
   * implementing the <code>writeUnsignedByte()</code> method in the
458
   * <code>DataOutput</code> interface.
459
   *
460
   * @return The unsigned bytes value read as a Java <code>int</code>.
461
   *
462
   * @exception EOFException If end of file is reached before reading the value
463
   * @exception IOException If any other error occurs
464
   *
465
   * @see DataOutput#writeByte
466
   */
467
  public final int readUnsignedByte () throws IOException
468
  {
469
    return convertToUnsignedByte (in.read ());
470
  }
471
 
472
  /**
473
   * This method reads 16 unsigned bits into a Java int value from the stream.
474
   * It operates by reading two bytes from the stream and converting them to
475
   * a single Java <code>int</code>  The two bytes are stored most
476
   * significant byte first (i.e., "big endian") regardless of the native
477
   * host byte ordering.
478
   * <p>
479
   * As an example, if <code>byte1</code> and <code>byte2</code>
480
   * represent the first and second byte read from the stream
481
   * respectively, they will be transformed to an <code>int</code> in
482
   * the following manner:
483
   * <p>
484
   * <code>(int)(((byte1 &amp; 0xFF) &lt;&lt; 8) + (byte2 &amp; 0xFF))</code>
485
   * <p>
486
   * The value returned is in the range of 0 to 65535.
487
   * <p>
488
   * This method can read an unsigned short written by an object
489
   * implementing the <code>writeUnsignedShort()</code> method in the
490
   * <code>DataOutput</code> interface.
491
   *
492
   * @return The unsigned short value read as a Java <code>int</code>
493
   *
494
   * @exception EOFException If end of file is reached before reading the value
495
   * @exception IOException If any other error occurs
496
   *
497
   * @see DataOutput#writeShort
498
   */
499
  public final int readUnsignedShort () throws IOException
500
  {
501
    readFully (buf, 0, 2);
502
    return convertToUnsignedShort (buf);
503
  }
504
 
505
  /**
506
   * This method reads a <code>String</code> from an input stream that
507
   * is encoded in a modified UTF-8 format.  This format has a leading
508
   * two byte sequence that contains the remaining number of bytes to
509
   * read.  This two byte sequence is read using the
510
   * <code>readUnsignedShort()</code> method of this interface.
511
   * <p>
512
   * After the number of remaining bytes have been determined, these
513
   * bytes are read an transformed into <code>char</code> values.
514
   * These <code>char</code> values are encoded in the stream using
515
   * either a one, two, or three byte format.  The particular format
516
   * in use can be determined by examining the first byte read.
517
   * <p>
518
   * If the first byte has a high order bit of 0, then that character
519
   * consists on only one byte.  This character value consists of
520
   * seven bits that are at positions 0 through 6 of the byte.  As an
521
   * example, if <code>byte1</code> is the byte read from the stream,
522
   * it would be converted to a <code>char</code> like so:
523
   * <p>
524
   * <code>(char)byte1</code>
525
   * <p>
526
   * If the first byte has 110 as its high order bits, then the
527
   * character consists of two bytes.  The bits that make up the character
528
   * value are in positions 0 through 4 of the first byte and bit positions
529
   * 0 through 5 of the second byte.  (The second byte should have
530
   * 10 as its high order bits).  These values are in most significant
531
   * byte first (i.e., "big endian") order.
532
   * <p>
533
   * As an example, if <code>byte1</code> and <code>byte2</code> are
534
   * the first two bytes read respectively, and the high order bits of
535
   * them match the patterns which indicate a two byte character
536
   * encoding, then they would be converted to a Java
537
   * <code>char</code> like so:
538
   * <p>
539
   * <code>(char)(((byte1 & 0x1F) << 6) | (byte2 & 0x3F))</code>
540
   * <p>
541
   * If the first byte has a 1110 as its high order bits, then the
542
   * character consists of three bytes.  The bits that make up the character
543
   * value are in positions 0 through 3 of the first byte and bit positions
544
   * 0 through 5 of the other two bytes.  (The second and third bytes should
545
   * have 10 as their high order bits).  These values are in most
546
   * significant byte first (i.e., "big endian") order.
547
   * <p>
548
   * As an example, if <code>byte1</code> <code>byte2</code> and
549
   * <code>byte3</code> are the three bytes read, and the high order
550
   * bits of them match the patterns which indicate a three byte
551
   * character encoding, then they would be converted to a Java
552
   * <code>char</code> like so:
553
   * <p>
554
   * <code>(char)(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) |
555
   * (byte3 & 0x3F))</code>
556
   * <p>
557
   * Note that all characters are encoded in the method that requires
558
   * the fewest number of bytes with the exception of the character
559
   * with the value of <code>&#92;u0000</code> which is encoded as two
560
   * bytes.  This is a modification of the UTF standard used to
561
   * prevent C language style <code>NUL</code> values from appearing
562
   * in the byte stream.
563
   * <p>
564
   * This method can read data that was written by an object implementing the
565
   * <code>writeUTF()</code> method in <code>DataOutput</code>
566
   *
567
   * @return The <code>String</code> read
568
   *
569
   * @exception EOFException If end of file is reached before reading
570
   * the String
571
   * @exception UTFDataFormatException If the data is not in UTF-8 format
572
   * @exception IOException If any other error occurs
573
   *
574
   * @see DataOutput#writeUTF
575
   */
576
  public final String readUTF () throws IOException
577
  {
578
    return readUTF (this);
579
  }
580
 
581
  /**
582
   * This method reads a String encoded in UTF-8 format from the
583
   * specified <code>DataInput</code> source.
584
   *
585
   * @param in The <code>DataInput</code> source to read from
586
   *
587
   * @return The String read from the source
588
   *
589
   * @exception IOException If an error occurs
590
   *
591
   * @see DataInput#readUTF
592
   */
593
  public static final String readUTF(DataInput in) throws IOException
594
  {
595
    final int UTFlen = in.readUnsignedShort ();
596
 
597
    return readUTF(in, UTFlen);
598
  }
599
 
600
  /**
601
   * This method is similar to <code>readUTF</code>, but the
602
   * UTF-8 byte length is in 64 bits.
603
   * This method is not public. It is used by <code>ObjectInputStream</code>.
604
   *
605
   * @return The <code>String</code> read
606
   *
607
   * @exception EOFException If end of file is reached before reading
608
   * the String
609
   * @exception UTFDataFormatException If the data is not in UTF-8 format
610
   * @exception IOException If any other error occurs
611
   *
612
   * @see DataOutput#writeUTFLong
613
   */
614
  final String readUTFLong () throws IOException
615
  {
616
    long l = readLong ();
617
    if (l > Integer.MAX_VALUE)
618
      throw new IOException("The string length > Integer.MAX_VALUE");
619
    final int UTFlen = (int)l;
620
    return readUTF (this, UTFlen);
621
  }
622
 
623
  /**
624
   * This method performs the main task of <code>readUTF</code> and
625
   * <code>readUTFLong</code>.
626
   *
627
   * @param in The <code>DataInput</code> source to read from
628
   *
629
   * @param len The UTF-8 byte length of the String to be read
630
   *
631
   * @return The String read from the source
632
   *
633
   * @exception IOException If an error occurs
634
   *
635
   * @see DataInput#readUTF
636
   */
637
  private static final String readUTF(DataInput in, int len) throws IOException
638
  {
639
    byte[] buf = new byte [len];
640
 
641
    // This blocks until the entire string is available rather than
642
    // doing partial processing on the bytes that are available and then
643
    // blocking.  An advantage of the latter is that Exceptions
644
    // could be thrown earlier.  The former is a bit cleaner.
645
    in.readFully (buf, 0, len);
646
 
647
    return convertFromUTF (buf);
648
  }
649
 
650
  /**
651
   * This method attempts to skip and discard the specified number of bytes
652
   * in the input stream.  It may actually skip fewer bytes than requested.
653
   * This method will not skip any bytes if passed a negative number of bytes
654
   * to skip.
655
   *
656
   * @param n The requested number of bytes to skip.
657
   *
658
   * @return The requested number of bytes to skip.
659
   *
660
   * @exception IOException If an error occurs.
661
   * @specnote The JDK docs claim that this returns the number of bytes
662
   *  actually skipped. The JCL claims that this method can throw an
663
   *  EOFException. Neither of these appear to be true in the JDK 1.3's
664
   *  implementation. This tries to implement the actual JDK behaviour.
665
   */
666
  public final int skipBytes (int n) throws IOException
667
  {
668
    if (n <= 0)
669
      return 0;
670
    try
671
      {
672
        return (int) in.skip (n);
673
      }
674
    catch (EOFException x)
675
      {
676
        // do nothing.
677
      }
678
    return n;
679
  }
680
 
681
  static boolean convertToBoolean (int b) throws EOFException
682
  {
683
    if (b < 0)
684
      throw new EOFException ();
685
 
686
    return (b != 0);
687
  }
688
 
689
  static byte convertToByte (int i) throws EOFException
690
  {
691
    if (i < 0)
692
      throw new EOFException ();
693
 
694
    return (byte) i;
695
  }
696
 
697
  static int convertToUnsignedByte (int i) throws EOFException
698
  {
699
    if (i < 0)
700
      throw new EOFException ();
701
 
702
    return (i & 0xFF);
703
  }
704
 
705
  static char convertToChar (byte[] buf)
706
  {
707
    return (char) ((buf [0] << 8)
708
                    | (buf [1] & 0xff));
709
  }
710
 
711
  static short convertToShort (byte[] buf)
712
  {
713
    return (short) ((buf [0] << 8)
714
                    | (buf [1] & 0xff));
715
  }
716
 
717
  static int convertToUnsignedShort (byte[] buf)
718
  {
719
    return (((buf [0] & 0xff) << 8)
720
            | (buf [1] & 0xff));
721
  }
722
 
723
  static int convertToInt (byte[] buf)
724
  {
725
    return (((buf [0] & 0xff) << 24)
726
            | ((buf [1] & 0xff) << 16)
727
            | ((buf [2] & 0xff) << 8)
728
            | (buf [3] & 0xff));
729
  }
730
 
731
  static long convertToLong (byte[] buf)
732
  {
733
    return (((long)(buf [0] & 0xff) << 56) |
734
            ((long)(buf [1] & 0xff) << 48) |
735
            ((long)(buf [2] & 0xff) << 40) |
736
            ((long)(buf [3] & 0xff) << 32) |
737
            ((long)(buf [4] & 0xff) << 24) |
738
            ((long)(buf [5] & 0xff) << 16) |
739
            ((long)(buf [6] & 0xff) <<  8) |
740
            ((long)(buf [7] & 0xff)));
741
  }
742
 
743
  // FIXME: This method should be re-thought.  I suspect we have multiple
744
  // UTF-8 decoders floating around.  We should use the standard charset
745
  // converters, maybe and adding a direct call into one of the new
746
  // NIO converters for a super-fast UTF8 decode.
747
  static String convertFromUTF (byte[] buf)
748
    throws EOFException, UTFDataFormatException
749
  {
750
    // Give StringBuffer an initial estimated size to avoid
751
    // enlarge buffer frequently
752
    CPStringBuilder strbuf = new CPStringBuilder (buf.length / 2 + 2);
753
 
754
    for (int i = 0; i < buf.length; )
755
      {
756
        if ((buf [i] & 0x80) == 0)              // bit pattern 0xxxxxxx
757
          strbuf.append ((char) (buf [i++] & 0xFF));
758
        else if ((buf [i] & 0xE0) == 0xC0)      // bit pattern 110xxxxx
759
          {
760
            if (i + 1 >= buf.length
761
                || (buf [i + 1] & 0xC0) != 0x80)
762
              throw new UTFDataFormatException ();
763
 
764
            strbuf.append((char) (((buf [i++] & 0x1F) << 6)
765
                                  | (buf [i++] & 0x3F)));
766
          }
767
        else if ((buf [i] & 0xF0) == 0xE0)      // bit pattern 1110xxxx
768
          {
769
            if (i + 2 >= buf.length
770
                || (buf [i + 1] & 0xC0) != 0x80
771
                || (buf [i + 2] & 0xC0) != 0x80)
772
              throw new UTFDataFormatException ();
773
 
774
            strbuf.append ((char) (((buf [i++] & 0x0F) << 12)
775
                                   | ((buf [i++] & 0x3F) << 6)
776
                                   | (buf [i++] & 0x3F)));
777
          }
778
        else // must be ((buf [i] & 0xF0) == 0xF0 || (buf [i] & 0xC0) == 0x80)
779
          throw new UTFDataFormatException ();  // bit patterns 1111xxxx or
780
                                                //              10xxxxxx
781
      }
782
 
783
    return strbuf.toString ();
784
  }
785
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.