datainputstream.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 700 行 · 第 1/2 页
JAVA
700 行
/* * @(#)DataInputStream.java 1.59 06/10/10 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package java.io;/** * A data input stream lets an application read primitive Java data * types from an underlying input stream in a machine-independent * way. An application uses a data output stream to write data that * can later be read by a data input stream. * <p> * Data input streams and data output streams represent Unicode * strings in a format that is a slight modification of UTF-8. (For * more information, see X/Open Company Ltd., "File System Safe * UCS Transformation Format (FSS_UTF)", X/Open Preliminary * Specification, Document Number: P316. This information also * appears in ISO/IEC 10646, Annex P.) Note that in the * following tables, the most significant bit appears in the * far left-hand column. * <p> * All characters in the range <code>'\u0001'</code> to * <code>'\u007F'</code> are represented by a single byte: * * <center> * <table border="3" summary="Bit values and bytes"> * <tr> * <td></td> * <th id="bit" colspan=2><P ALIGN="LEFT">Bit Values</P></th> * </tr> * <tr> * <th id="byte1">Byte 1 </th> * <td headers="bit byte1"><i>0</i></td> * <td>bits 6-0</td> * </tr> * </table> * </center> * * <p> * The null character <code>'\u0000'</code> and characters in the * range <code>'\u0080'</code> to <code>'\u07FF'</code> are * represented by a pair of bytes: * * <center> * <table border="3" summary="Bit values and bytes"> * <tr> * <td></td> * <th id="bit" colspan=4><P ALIGN="LEFT">Bit Values</P></th> * </tr> * <tr> * <th id="byte1">Byte 1 </th> * <td headers="bit byte1">1</td> * <td headers="bit byte1">1</td> * <td headers="bit byte1">0</td> * <td headers="bit byte1">bits 10-6</td> * </tr> * <tr> * <th id="byte2">Byte 2 </th> * <td headers="bit byte2">1</td> * <td headers="bit byte2">0</td> * <td headers="bit byte2" colspan=2>bits 5-0</td> * </tr> * </table> * </center> * * <br> * Characters in the range <code>'\u0800'</code> to * <code>'\uFFFF'</code> are represented by three bytes: * * <center> * <table border="3" summary="Bit values and bytes"> * <tr> * <td></td> * <th id="bit" colspan=5><P ALIGN="LEFT">Bit Values</P></th> * </tr> * * <tr> * <th id="byte1">Byte 1 </th> * <td headers="bit byte1">1</td> * <td headers="bit byte1">1</td> * <td headers="bit byte1">1</td> * <td headers="bit byte1">0</td> * <td headers="bit byte1">bits 15-12</td> * </tr> * <tr> * <th id="byte2">Byte 2 </th> * <td headers="bit byte2">1</td> * <td headers="bit byte2">0</td> * <td headers="bit byte2" colspan=3>bits 11-6</td> * </tr> * <tr> * <th id="byte3">Byte 3 </th> * <td headers="bit byte3">1</td> * <td headers="bit byte3">0</td> * <td headers="bit byte3" colspan=3>bits 5-0</td> * </tr> * </table> * </center> * <p> * The two differences between this format and the * "standard" UTF-8 format are the following: * <ul> * <li>The null byte <code>'\u0000'</code> is encoded in 2-byte format * rather than 1-byte, so that the encoded strings never have * embedded nulls. * <li>Only the 1-byte, 2-byte, and 3-byte formats are used. * </ul> * * @version 1.52 10/17/00 * @see java.io.DataOutputStream * @since JDK1.0 */publicclass DataInputStream extends FilterInputStream implements DataInput { /** * Creates a DataInputStream that uses the specified * underlying InputStream. * * @param in the specified input stream */ public DataInputStream(InputStream in) { super(in); } /** * Reads some number of bytes from the contained input stream and * stores them into the buffer array <code>b</code>. The number of * bytes actually read is returned as an integer. This method blocks * until input data is available, end of file is detected, or an * exception is thrown. * * <p>If <code>b</code> is null, a <code>NullPointerException</code> is * thrown. If the length of <code>b</code> is zero, then no bytes are * read and <code>0</code> is returned; otherwise, there is an attempt * to read at least one byte. If no byte is available because the * stream is at end of file, the value <code>-1</code> is returned; * otherwise, at least one byte is read and stored into <code>b</code>. * * <p>The first byte read is stored into element <code>b[0]</code>, the * next one into <code>b[1]</code>, and so on. The number of bytes read * is, at most, equal to the length of <code>b</code>. Let <code>k</code> * be the number of bytes actually read; these bytes will be stored in * elements <code>b[0]</code> through <code>b[k-1]</code>, leaving * elements <code>b[k]</code> through <code>b[b.length-1]</code> * unaffected. * * <p>If the first byte cannot be read for any reason other than end of * file, then an <code>IOException</code> is thrown. In particular, an * <code>IOException</code> is thrown if the input stream has been closed. * * <p>The <code>read(b)</code> method has the same effect as: * <blockquote><pre> * read(b, 0, b.length) * </pre></blockquote> * * @param b the buffer into which the data is read. * @return the total number of bytes read into the buffer, or * <code>-1</code> if there is no more data because the end * of the stream has been reached. * @exception IOException if an I/O error occurs. * @see java.io.FilterInputStream#in * @see java.io.InputStream#read(byte[], int, int) */ public final int read(byte b[]) throws IOException { return in.read(b, 0, b.length); } /** * Reads up to <code>len</code> bytes of data from the contained * input stream into an array of bytes. An attempt is made to read * as many as <code>len</code> bytes, but a smaller number may be read, * possibly zero. The number of bytes actually read is returned as an * integer. * * <p> This method blocks until input data is available, end of file is * detected, or an exception is thrown. * * <p> If <code>b</code> is <code>null</code>, a * <code>NullPointerException</code> is thrown. * * <p> If <code>off</code> is negative, or <code>len</code> is negative, or * <code>off+len</code> is greater than the length of the array * <code>b</code>, then an <code>IndexOutOfBoundsException</code> is * thrown. * * <p> If <code>len</code> is zero, then no bytes are read and * <code>0</code> is returned; otherwise, there is an attempt to read at * least one byte. If no byte is available because the stream is at end of * file, the value <code>-1</code> is returned; otherwise, at least one * byte is read and stored into <code>b</code>. * * <p> The first byte read is stored into element <code>b[off]</code>, the * next one into <code>b[off+1]</code>, and so on. The number of bytes read * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of * bytes actually read; these bytes will be stored in elements * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>, * leaving elements <code>b[off+</code><i>k</i><code>]</code> through * <code>b[off+len-1]</code> unaffected. * * <p> In every case, elements <code>b[0]</code> through * <code>b[off]</code> and elements <code>b[off+len]</code> through * <code>b[b.length-1]</code> are unaffected. * * <p> If the first byte cannot be read for any reason other than end of * file, then an <code>IOException</code> is thrown. In particular, an * <code>IOException</code> is thrown if the input stream has been closed. * * @param b the buffer into which the data is read. * @param off the start offset of the data. * @param len the maximum number of bytes read. * @return the total number of bytes read into the buffer, or * <code>-1</code> if there is no more data because the end * of the stream has been reached. * @exception IOException if an I/O error occurs. * @see java.io.FilterInputStream#in * @see java.io.InputStream#read(byte[], int, int) */ public final int read(byte b[], int off, int len) throws IOException { return in.read(b, off, len); } /** * See the general contract of the <code>readFully</code> * method of <code>DataInput</code>. * <p> * Bytes * for this operation are read from the contained * input stream. * * @param b the buffer into which the data is read. * @exception EOFException if this input stream reaches the end before * reading all the bytes. * @exception IOException if an I/O error occurs. * @see java.io.FilterInputStream#in */ public final void readFully(byte b[]) throws IOException { readFully(b, 0, b.length); } /** * See the general contract of the <code>readFully</code> * method of <code>DataInput</code>. * <p> * Bytes * for this operation are read from the contained * input stream. * * @param b the buffer into which the data is read. * @param off the start offset of the data. * @param len the number of bytes to read. * @exception EOFException if this input stream reaches the end before * reading all the bytes. * @exception IOException if an I/O error occurs. * @see java.io.FilterInputStream#in */ public final void readFully(byte b[], int off, int len) throws IOException { if (len < 0) throw new IndexOutOfBoundsException(); int n = 0; while (n < len) { int count = in.read(b, off + n, len - n); if (count < 0) throw new EOFException(); n += count; } } /** * See the general contract of the <code>skipBytes</code> * method of <code>DataInput</code>. * <p> * Bytes * for this operation are read from the contained * input stream. * * @param n the number of bytes to be skipped. * @return the actual number of bytes skipped. * @exception IOException if an I/O error occurs. */ public final int skipBytes(int n) throws IOException { int total = 0; int cur = 0; while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) { total += cur; } return total; } /** * See the general contract of the <code>readBoolean</code> * method of <code>DataInput</code>. * <p> * Bytes * for this operation are read from the contained * input stream. * * @return the <code>boolean</code> value read. * @exception EOFException if this input stream has reached the end. * @exception IOException if an I/O error occurs. * @see java.io.FilterInputStream#in */ public final boolean readBoolean() throws IOException { int ch = in.read(); if (ch < 0) throw new EOFException(); return (ch != 0); } /** * See the general contract of the <code>readByte</code> * method of <code>DataInput</code>. * <p> * Bytes * for this operation are read from the contained * input stream. * * @return the next byte of this input stream as a signed 8-bit * <code>byte</code>. * @exception EOFException if this input stream has reached the end. * @exception IOException if an I/O error occurs. * @see java.io.FilterInputStream#in */ public final byte readByte() throws IOException { int ch = in.read();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?