Source Code Cross Referenced for DataInputStream.java in  » 6.0-JDK-Core » io-nio » java » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Core » io nio » java.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 1994-2006 Sun Microsystems, Inc.  All Rights Reserved.
003:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004:         *
005:         * This code is free software; you can redistribute it and/or modify it
006:         * under the terms of the GNU General Public License version 2 only, as
007:         * published by the Free Software Foundation.  Sun designates this
008:         * particular file as subject to the "Classpath" exception as provided
009:         * by Sun in the LICENSE file that accompanied this code.
010:         *
011:         * This code is distributed in the hope that it will be useful, but WITHOUT
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013:         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014:         * version 2 for more details (a copy is included in the LICENSE file that
015:         * accompanied this code).
016:         *
017:         * You should have received a copy of the GNU General Public License version
018:         * 2 along with this work; if not, write to the Free Software Foundation,
019:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020:         *
021:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022:         * CA 95054 USA or visit www.sun.com if you need additional information or
023:         * have any questions.
024:         */
025:
026:        package java.io;
027:
028:        /**
029:         * A data input stream lets an application read primitive Java data
030:         * types from an underlying input stream in a machine-independent
031:         * way. An application uses a data output stream to write data that
032:         * can later be read by a data input stream.
033:         * <p>
034:         * DataInputStream is not necessarily safe for multithreaded access.
035:         * Thread safety is optional and is the responsibility of users of
036:         * methods in this class.
037:         *
038:         * @author  Arthur van Hoff
039:         * @version 1.83, 05/05/07
040:         * @see     java.io.DataOutputStream
041:         * @since   JDK1.0
042:         */
043:        public class DataInputStream extends FilterInputStream implements 
044:                DataInput {
045:
046:            /**
047:             * Creates a DataInputStream that uses the specified
048:             * underlying InputStream.
049:             *
050:             * @param  in   the specified input stream
051:             */
052:            public DataInputStream(InputStream in) {
053:                super (in);
054:            }
055:
056:            /**
057:             * working arrays initialized on demand by readUTF
058:             */
059:            private byte bytearr[] = new byte[80];
060:            private char chararr[] = new char[80];
061:
062:            /**
063:             * Reads some number of bytes from the contained input stream and 
064:             * stores them into the buffer array <code>b</code>. The number of 
065:             * bytes actually read is returned as an integer. This method blocks 
066:             * until input data is available, end of file is detected, or an 
067:             * exception is thrown. 
068:             * 
069:             * <p>If <code>b</code> is null, a <code>NullPointerException</code> is 
070:             * thrown. If the length of <code>b</code> is zero, then no bytes are 
071:             * read and <code>0</code> is returned; otherwise, there is an attempt 
072:             * to read at least one byte. If no byte is available because the 
073:             * stream is at end of file, the value <code>-1</code> is returned;
074:             * otherwise, at least one byte is read and stored into <code>b</code>. 
075:             * 
076:             * <p>The first byte read is stored into element <code>b[0]</code>, the 
077:             * next one into <code>b[1]</code>, and so on. The number of bytes read 
078:             * is, at most, equal to the length of <code>b</code>. Let <code>k</code> 
079:             * be the number of bytes actually read; these bytes will be stored in 
080:             * elements <code>b[0]</code> through <code>b[k-1]</code>, leaving 
081:             * elements <code>b[k]</code> through <code>b[b.length-1]</code> 
082:             * unaffected. 
083:             * 
084:             * <p>The <code>read(b)</code> method has the same effect as: 
085:             * <blockquote><pre>
086:             * read(b, 0, b.length) 
087:             * </pre></blockquote>
088:             *
089:             * @param      b   the buffer into which the data is read.
090:             * @return     the total number of bytes read into the buffer, or
091:             *             <code>-1</code> if there is no more data because the end
092:             *             of the stream has been reached.
093:             * @exception  IOException if the first byte cannot be read for any reason
094:             * other than end of file, the stream has been closed and the underlying
095:             * input stream does not support reading after close, or another I/O
096:             * error occurs.
097:             * @see        java.io.FilterInputStream#in
098:             * @see        java.io.InputStream#read(byte[], int, int)
099:             */
100:            public final int read(byte b[]) throws IOException {
101:                return in.read(b, 0, b.length);
102:            }
103:
104:            /**
105:             * Reads up to <code>len</code> bytes of data from the contained 
106:             * input stream into an array of bytes.  An attempt is made to read 
107:             * as many as <code>len</code> bytes, but a smaller number may be read, 
108:             * possibly zero. The number of bytes actually read is returned as an 
109:             * integer.
110:             *
111:             * <p> This method blocks until input data is available, end of file is
112:             * detected, or an exception is thrown.
113:             *
114:             * <p> If <code>len</code> is zero, then no bytes are read and
115:             * <code>0</code> is returned; otherwise, there is an attempt to read at
116:             * least one byte. If no byte is available because the stream is at end of
117:             * file, the value <code>-1</code> is returned; otherwise, at least one
118:             * byte is read and stored into <code>b</code>.
119:             *
120:             * <p> The first byte read is stored into element <code>b[off]</code>, the
121:             * next one into <code>b[off+1]</code>, and so on. The number of bytes read
122:             * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
123:             * bytes actually read; these bytes will be stored in elements
124:             * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
125:             * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
126:             * <code>b[off+len-1]</code> unaffected.
127:             *
128:             * <p> In every case, elements <code>b[0]</code> through
129:             * <code>b[off]</code> and elements <code>b[off+len]</code> through
130:             * <code>b[b.length-1]</code> are unaffected.
131:             *
132:             * @param      b     the buffer into which the data is read.
133:             * @param off the start offset in the destination array <code>b</code>
134:             * @param      len   the maximum number of bytes read.
135:             * @return     the total number of bytes read into the buffer, or
136:             *             <code>-1</code> if there is no more data because the end
137:             *             of the stream has been reached.
138:             * @exception  NullPointerException If <code>b</code> is <code>null</code>.
139:             * @exception  IndexOutOfBoundsException If <code>off</code> is negative, 
140:             * <code>len</code> is negative, or <code>len</code> is greater than 
141:             * <code>b.length - off</code>
142:             * @exception  IOException if the first byte cannot be read for any reason
143:             * other than end of file, the stream has been closed and the underlying
144:             * input stream does not support reading after close, or another I/O
145:             * error occurs.
146:             * @see        java.io.FilterInputStream#in
147:             * @see        java.io.InputStream#read(byte[], int, int)
148:             */
149:            public final int read(byte b[], int off, int len)
150:                    throws IOException {
151:                return in.read(b, off, len);
152:            }
153:
154:            /**
155:             * See the general contract of the <code>readFully</code>
156:             * method of <code>DataInput</code>.
157:             * <p>
158:             * Bytes
159:             * for this operation are read from the contained
160:             * input stream.
161:             *
162:             * @param      b   the buffer into which the data is read.
163:             * @exception  EOFException  if this input stream reaches the end before
164:             *             reading all the bytes.
165:             * @exception  IOException   the stream has been closed and the contained
166:             * 		   input stream does not support reading after close, or
167:             * 		   another I/O error occurs.
168:             * @see        java.io.FilterInputStream#in
169:             */
170:            public final void readFully(byte b[]) throws IOException {
171:                readFully(b, 0, b.length);
172:            }
173:
174:            /**
175:             * See the general contract of the <code>readFully</code>
176:             * method of <code>DataInput</code>.
177:             * <p>
178:             * Bytes
179:             * for this operation are read from the contained
180:             * input stream.
181:             *
182:             * @param      b     the buffer into which the data is read.
183:             * @param      off   the start offset of the data.
184:             * @param      len   the number of bytes to read.
185:             * @exception  EOFException  if this input stream reaches the end before
186:             *               reading all the bytes.
187:             * @exception  IOException   the stream has been closed and the contained
188:             * 		   input stream does not support reading after close, or
189:             * 		   another I/O error occurs.
190:             * @see        java.io.FilterInputStream#in
191:             */
192:            public final void readFully(byte b[], int off, int len)
193:                    throws IOException {
194:                if (len < 0)
195:                    throw new IndexOutOfBoundsException();
196:                int n = 0;
197:                while (n < len) {
198:                    int count = in.read(b, off + n, len - n);
199:                    if (count < 0)
200:                        throw new EOFException();
201:                    n += count;
202:                }
203:            }
204:
205:            /**
206:             * See the general contract of the <code>skipBytes</code>
207:             * method of <code>DataInput</code>.
208:             * <p>
209:             * Bytes for this operation are read from the contained
210:             * input stream.
211:             *
212:             * @param      n   the number of bytes to be skipped.
213:             * @return     the actual number of bytes skipped.
214:             * @exception  IOException  if the contained input stream does not support
215:             *		   seek, or the stream has been closed and
216:             *		   the contained input stream does not support 
217:             *		   reading after close, or another I/O error occurs.
218:             */
219:            public final int skipBytes(int n) throws IOException {
220:                int total = 0;
221:                int cur = 0;
222:
223:                while ((total < n) && ((cur = (int) in.skip(n - total)) > 0)) {
224:                    total += cur;
225:                }
226:
227:                return total;
228:            }
229:
230:            /**
231:             * See the general contract of the <code>readBoolean</code>
232:             * method of <code>DataInput</code>.
233:             * <p>
234:             * Bytes for this operation are read from the contained
235:             * input stream.
236:             *
237:             * @return     the <code>boolean</code> value read.
238:             * @exception  EOFException  if this input stream has reached the end.
239:             * @exception  IOException   the stream has been closed and the contained
240:             * 		   input stream does not support reading after close, or
241:             * 		   another I/O error occurs.
242:             * @see        java.io.FilterInputStream#in
243:             */
244:            public final boolean readBoolean() throws IOException {
245:                int ch = in.read();
246:                if (ch < 0)
247:                    throw new EOFException();
248:                return (ch != 0);
249:            }
250:
251:            /**
252:             * See the general contract of the <code>readByte</code>
253:             * method of <code>DataInput</code>.
254:             * <p>
255:             * Bytes
256:             * for this operation are read from the contained
257:             * input stream.
258:             *
259:             * @return     the next byte of this input stream as a signed 8-bit
260:             *             <code>byte</code>.
261:             * @exception  EOFException  if this input stream has reached the end.
262:             * @exception  IOException   the stream has been closed and the contained
263:             * 		   input stream does not support reading after close, or
264:             * 		   another I/O error occurs.
265:             * @see        java.io.FilterInputStream#in
266:             */
267:            public final byte readByte() throws IOException {
268:                int ch = in.read();
269:                if (ch < 0)
270:                    throw new EOFException();
271:                return (byte) (ch);
272:            }
273:
274:            /**
275:             * See the general contract of the <code>readUnsignedByte</code>
276:             * method of <code>DataInput</code>.
277:             * <p>
278:             * Bytes
279:             * for this operation are read from the contained
280:             * input stream.
281:             *
282:             * @return     the next byte of this input stream, interpreted as an
283:             *             unsigned 8-bit number.
284:             * @exception  EOFException  if this input stream has reached the end.
285:             * @exception  IOException   the stream has been closed and the contained
286:             * 		   input stream does not support reading after close, or
287:             * 		   another I/O error occurs.
288:             * @see         java.io.FilterInputStream#in
289:             */
290:            public final int readUnsignedByte() throws IOException {
291:                int ch = in.read();
292:                if (ch < 0)
293:                    throw new EOFException();
294:                return ch;
295:            }
296:
297:            /**
298:             * See the general contract of the <code>readShort</code>
299:             * method of <code>DataInput</code>.
300:             * <p>
301:             * Bytes
302:             * for this operation are read from the contained
303:             * input stream.
304:             *
305:             * @return     the next two bytes of this input stream, interpreted as a
306:             *             signed 16-bit number.
307:             * @exception  EOFException  if this input stream reaches the end before
308:             *               reading two bytes.
309:             * @exception  IOException   the stream has been closed and the contained
310:             * 		   input stream does not support reading after close, or
311:             * 		   another I/O error occurs.
312:             * @see        java.io.FilterInputStream#in
313:             */
314:            public final short readShort() throws IOException {
315:                int ch1 = in.read();
316:                int ch2 = in.read();
317:                if ((ch1 | ch2) < 0)
318:                    throw new EOFException();
319:                return (short) ((ch1 << 8) + (ch2 << 0));
320:            }
321:
322:            /**
323:             * See the general contract of the <code>readUnsignedShort</code>
324:             * method of <code>DataInput</code>.
325:             * <p>
326:             * Bytes
327:             * for this operation are read from the contained
328:             * input stream.
329:             *
330:             * @return     the next two bytes of this input stream, interpreted as an
331:             *             unsigned 16-bit integer.
332:             * @exception  EOFException  if this input stream reaches the end before
333:             *             reading two bytes.
334:             * @exception  IOException   the stream has been closed and the contained
335:             * 		   input stream does not support reading after close, or
336:             * 		   another I/O error occurs.
337:             * @see        java.io.FilterInputStream#in
338:             */
339:            public final int readUnsignedShort() throws IOException {
340:                int ch1 = in.read();
341:                int ch2 = in.read();
342:                if ((ch1 | ch2) < 0)
343:                    throw new EOFException();
344:                return (ch1 << 8) + (ch2 << 0);
345:            }
346:
347:            /**
348:             * See the general contract of the <code>readChar</code>
349:             * method of <code>DataInput</code>.
350:             * <p>
351:             * Bytes
352:             * for this operation are read from the contained
353:             * input stream.
354:             *
355:             * @return     the next two bytes of this input stream, interpreted as a
356:             *		   <code>char</code>.
357:             * @exception  EOFException  if this input stream reaches the end before
358:             *               reading two bytes.
359:             * @exception  IOException   the stream has been closed and the contained
360:             * 		   input stream does not support reading after close, or
361:             * 		   another I/O error occurs.
362:             * @see        java.io.FilterInputStream#in
363:             */
364:            public final char readChar() throws IOException {
365:                int ch1 = in.read();
366:                int ch2 = in.read();
367:                if ((ch1 | ch2) < 0)
368:                    throw new EOFException();
369:                return (char) ((ch1 << 8) + (ch2 << 0));
370:            }
371:
372:            /**
373:             * See the general contract of the <code>readInt</code>
374:             * method of <code>DataInput</code>.
375:             * <p>
376:             * Bytes
377:             * for this operation are read from the contained
378:             * input stream.
379:             *
380:             * @return     the next four bytes of this input stream, interpreted as an
381:             *             <code>int</code>.
382:             * @exception  EOFException  if this input stream reaches the end before
383:             *               reading four bytes.
384:             * @exception  IOException   the stream has been closed and the contained
385:             * 		   input stream does not support reading after close, or
386:             * 		   another I/O error occurs.
387:             * @see        java.io.FilterInputStream#in
388:             */
389:            public final int readInt() throws IOException {
390:                int ch1 = in.read();
391:                int ch2 = in.read();
392:                int ch3 = in.read();
393:                int ch4 = in.read();
394:                if ((ch1 | ch2 | ch3 | ch4) < 0)
395:                    throw new EOFException();
396:                return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
397:            }
398:
399:            private byte readBuffer[] = new byte[8];
400:
401:            /**
402:             * See the general contract of the <code>readLong</code>
403:             * method of <code>DataInput</code>.
404:             * <p>
405:             * Bytes
406:             * for this operation are read from the contained
407:             * input stream.
408:             *
409:             * @return     the next eight bytes of this input stream, interpreted as a
410:             *             <code>long</code>.
411:             * @exception  EOFException  if this input stream reaches the end before
412:             *               reading eight bytes.
413:             * @exception  IOException   the stream has been closed and the contained
414:             * 		   input stream does not support reading after close, or
415:             * 		   another I/O error occurs.
416:             * @see        java.io.FilterInputStream#in
417:             */
418:            public final long readLong() throws IOException {
419:                readFully(readBuffer, 0, 8);
420:                return (((long) readBuffer[0] << 56)
421:                        + ((long) (readBuffer[1] & 255) << 48)
422:                        + ((long) (readBuffer[2] & 255) << 40)
423:                        + ((long) (readBuffer[3] & 255) << 32)
424:                        + ((long) (readBuffer[4] & 255) << 24)
425:                        + ((readBuffer[5] & 255) << 16)
426:                        + ((readBuffer[6] & 255) << 8) + ((readBuffer[7] & 255) << 0));
427:            }
428:
429:            /**
430:             * See the general contract of the <code>readFloat</code>
431:             * method of <code>DataInput</code>.
432:             * <p>
433:             * Bytes
434:             * for this operation are read from the contained
435:             * input stream.
436:             *
437:             * @return     the next four bytes of this input stream, interpreted as a
438:             *             <code>float</code>.
439:             * @exception  EOFException  if this input stream reaches the end before
440:             *               reading four bytes.
441:             * @exception  IOException   the stream has been closed and the contained
442:             * 		   input stream does not support reading after close, or
443:             * 		   another I/O error occurs.
444:             * @see        java.io.DataInputStream#readInt()
445:             * @see        java.lang.Float#intBitsToFloat(int)
446:             */
447:            public final float readFloat() throws IOException {
448:                return Float.intBitsToFloat(readInt());
449:            }
450:
451:            /**
452:             * See the general contract of the <code>readDouble</code>
453:             * method of <code>DataInput</code>.
454:             * <p>
455:             * Bytes
456:             * for this operation are read from the contained
457:             * input stream.
458:             *
459:             * @return     the next eight bytes of this input stream, interpreted as a
460:             *             <code>double</code>.
461:             * @exception  EOFException  if this input stream reaches the end before
462:             *               reading eight bytes.
463:             * @exception  IOException   the stream has been closed and the contained
464:             * 		   input stream does not support reading after close, or
465:             * 		   another I/O error occurs.
466:             * @see        java.io.DataInputStream#readLong()
467:             * @see        java.lang.Double#longBitsToDouble(long)
468:             */
469:            public final double readDouble() throws IOException {
470:                return Double.longBitsToDouble(readLong());
471:            }
472:
473:            private char lineBuffer[];
474:
475:            /**
476:             * See the general contract of the <code>readLine</code>
477:             * method of <code>DataInput</code>.
478:             * <p>
479:             * Bytes
480:             * for this operation are read from the contained
481:             * input stream.
482:             *
483:             * @deprecated This method does not properly convert bytes to characters.
484:             * As of JDK&nbsp;1.1, the preferred way to read lines of text is via the
485:             * <code>BufferedReader.readLine()</code> method.  Programs that use the
486:             * <code>DataInputStream</code> class to read lines can be converted to use
487:             * the <code>BufferedReader</code> class by replacing code of the form:
488:             * <blockquote><pre>
489:             *     DataInputStream d =&nbsp;new&nbsp;DataInputStream(in);
490:             * </pre></blockquote>
491:             * with:
492:             * <blockquote><pre>
493:             *     BufferedReader d
494:             *          =&nbsp;new&nbsp;BufferedReader(new&nbsp;InputStreamReader(in));
495:             * </pre></blockquote>
496:             *
497:             * @return     the next line of text from this input stream.
498:             * @exception  IOException  if an I/O error occurs.
499:             * @see        java.io.BufferedReader#readLine()
500:             * @see        java.io.FilterInputStream#in
501:             */
502:            @Deprecated
503:            public final String readLine() throws IOException {
504:                char buf[] = lineBuffer;
505:
506:                if (buf == null) {
507:                    buf = lineBuffer = new char[128];
508:                }
509:
510:                int room = buf.length;
511:                int offset = 0;
512:                int c;
513:
514:                loop: while (true) {
515:                    switch (c = in.read()) {
516:                    case -1:
517:                    case '\n':
518:                        break loop;
519:
520:                    case '\r':
521:                        int c2 = in.read();
522:                        if ((c2 != '\n') && (c2 != -1)) {
523:                            if (!(in instanceof  PushbackInputStream)) {
524:                                this .in = new PushbackInputStream(in);
525:                            }
526:                            ((PushbackInputStream) in).unread(c2);
527:                        }
528:                        break loop;
529:
530:                    default:
531:                        if (--room < 0) {
532:                            buf = new char[offset + 128];
533:                            room = buf.length - offset - 1;
534:                            System.arraycopy(lineBuffer, 0, buf, 0, offset);
535:                            lineBuffer = buf;
536:                        }
537:                        buf[offset++] = (char) c;
538:                        break;
539:                    }
540:                }
541:                if ((c == -1) && (offset == 0)) {
542:                    return null;
543:                }
544:                return String.copyValueOf(buf, 0, offset);
545:            }
546:
547:            /**
548:             * See the general contract of the <code>readUTF</code>
549:             * method of <code>DataInput</code>.
550:             * <p>
551:             * Bytes
552:             * for this operation are read from the contained
553:             * input stream.
554:             *
555:             * @return     a Unicode string.
556:             * @exception  EOFException  if this input stream reaches the end before
557:             *               reading all the bytes.
558:             * @exception  IOException   the stream has been closed and the contained
559:             * 		   input stream does not support reading after close, or
560:             * 		   another I/O error occurs.
561:             * @exception  UTFDataFormatException if the bytes do not represent a valid
562:             *             modified UTF-8 encoding of a string.
563:             * @see        java.io.DataInputStream#readUTF(java.io.DataInput)
564:             */
565:            public final String readUTF() throws IOException {
566:                return readUTF(this );
567:            }
568:
569:            /**
570:             * Reads from the
571:             * stream <code>in</code> a representation
572:             * of a Unicode  character string encoded in
573:             * <a href="DataInput.html#modified-utf-8">modified UTF-8</a> format;
574:             * this string of characters is then returned as a <code>String</code>.
575:             * The details of the modified UTF-8 representation
576:             * are  exactly the same as for the <code>readUTF</code>
577:             * method of <code>DataInput</code>.
578:             *
579:             * @param      in   a data input stream.
580:             * @return     a Unicode string.
581:             * @exception  EOFException            if the input stream reaches the end
582:             *               before all the bytes.
583:             * @exception  IOException   the stream has been closed and the contained
584:             * 		   input stream does not support reading after close, or
585:             * 		   another I/O error occurs.
586:             * @exception  UTFDataFormatException  if the bytes do not represent a
587:             *               valid modified UTF-8 encoding of a Unicode string.
588:             * @see        java.io.DataInputStream#readUnsignedShort()
589:             */
590:            public final static String readUTF(DataInput in) throws IOException {
591:                int utflen = in.readUnsignedShort();
592:                byte[] bytearr = null;
593:                char[] chararr = null;
594:                if (in instanceof  DataInputStream) {
595:                    DataInputStream dis = (DataInputStream) in;
596:                    if (dis.bytearr.length < utflen) {
597:                        dis.bytearr = new byte[utflen * 2];
598:                        dis.chararr = new char[utflen * 2];
599:                    }
600:                    chararr = dis.chararr;
601:                    bytearr = dis.bytearr;
602:                } else {
603:                    bytearr = new byte[utflen];
604:                    chararr = new char[utflen];
605:                }
606:
607:                int c, char2, char3;
608:                int count = 0;
609:                int chararr_count = 0;
610:
611:                in.readFully(bytearr, 0, utflen);
612:
613:                while (count < utflen) {
614:                    c = (int) bytearr[count] & 0xff;
615:                    if (c > 127)
616:                        break;
617:                    count++;
618:                    chararr[chararr_count++] = (char) c;
619:                }
620:
621:                while (count < utflen) {
622:                    c = (int) bytearr[count] & 0xff;
623:                    switch (c >> 4) {
624:                    case 0:
625:                    case 1:
626:                    case 2:
627:                    case 3:
628:                    case 4:
629:                    case 5:
630:                    case 6:
631:                    case 7:
632:                        /* 0xxxxxxx*/
633:                        count++;
634:                        chararr[chararr_count++] = (char) c;
635:                        break;
636:                    case 12:
637:                    case 13:
638:                        /* 110x xxxx   10xx xxxx*/
639:                        count += 2;
640:                        if (count > utflen)
641:                            throw new UTFDataFormatException(
642:                                    "malformed input: partial character at end");
643:                        char2 = (int) bytearr[count - 1];
644:                        if ((char2 & 0xC0) != 0x80)
645:                            throw new UTFDataFormatException(
646:                                    "malformed input around byte " + count);
647:                        chararr[chararr_count++] = (char) (((c & 0x1F) << 6) | (char2 & 0x3F));
648:                        break;
649:                    case 14:
650:                        /* 1110 xxxx  10xx xxxx  10xx xxxx */
651:                        count += 3;
652:                        if (count > utflen)
653:                            throw new UTFDataFormatException(
654:                                    "malformed input: partial character at end");
655:                        char2 = (int) bytearr[count - 2];
656:                        char3 = (int) bytearr[count - 1];
657:                        if (((char2 & 0xC0) != 0x80)
658:                                || ((char3 & 0xC0) != 0x80))
659:                            throw new UTFDataFormatException(
660:                                    "malformed input around byte "
661:                                            + (count - 1));
662:                        chararr[chararr_count++] = (char) (((c & 0x0F) << 12)
663:                                | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
664:                        break;
665:                    default:
666:                        /* 10xx xxxx,  1111 xxxx */
667:                        throw new UTFDataFormatException(
668:                                "malformed input around byte " + count);
669:                    }
670:                }
671:                // The number of chars produced may be less than utflen
672:                return new String(chararr, 0, chararr_count);
673:            }
674:        }
ww___w.j_a__va_2s__._c__o_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.