randomaccessfile.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,012 行 · 第 1/3 页

JAVA
1,012
字号
	  * @exception IOException If any other error occurs
	  *
	  * @see DataOutput
	  */
	public final int readUnsignedByte() throws EOFException, IOException {
		int byte_read = read();

		if (byte_read == -1)
			throw new EOFException("Unexpected end of stream");

		return (DataInputStream.convertToUnsignedByte(byte_read));
	}

	/**
	  * This method reads a Java <code>char</code> value from an input stream.  
	  * It operates by reading two bytes from the stream and converting them to 
	  * a single 16-bit Java <code>char</code>  The two bytes are stored most
	  * significant byte first (i.e., "big endian") regardless of the native
	  * host byte ordering. 
	  * <p>
	  * As an example, if <code>byte1</code> and code{byte2</code> represent the first
	  * and second byte read from the stream respectively, they will be
	  * transformed to a <code>char</code> in the following manner:
	  * <p>
	  * <code>(char)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code>
	  * <p>
	  * This method can read a <code>char</code> written by an object implementing the
	  * <code>writeChar()</code> method in the <code>DataOutput</code> interface.
	  *
	  * @return The <code>char</code> value read 
	  *
	  * @exception EOFException If end of file is reached before reading the char
	  * @exception IOException If any other error occurs
	  *
	  * @see DataOutput
	  */
	public final char readChar() throws EOFException, IOException {
		byte[] buf = new byte[2];

		readFully(buf);

		return (DataInputStream.convertToChar(buf));
	}

	/**
	  * This method reads a signed 16-bit value into a Java in from the stream.
	  * It operates by reading two bytes from the stream and converting them to 
	  * a single 16-bit Java <code>short</code>  The two bytes are stored most
	  * significant byte first (i.e., "big endian") regardless of the native
	  * host byte ordering. 
	  * <p>
	  * As an example, if <code>byte1</code> and code{byte2</code> represent the first
	  * and second byte read from the stream respectively, they will be
	  * transformed to a <code>short</code> in the following manner:
	  * <p>
	  * <code>(short)(((byte1 & 0xFF) << 8) | (byte2 & 0xFF)</code>
	  * <p>
	  * The value returned is in the range of -32768 to 32767.
	  * <p>
	  * This method can read a <code>short</code> written by an object implementing the
	  * <code>writeShort()</code> method in the <code>DataOutput</code> interface.
	  *
	  * @return The <code>short</code> value read
	  *
	  * @exception EOFException If end of file is reached before reading the value
	  * @exception IOException If any other error occurs
	  *
	  * @see DataOutput
	  */
	public final short readShort() throws EOFException, IOException {
		byte[] buf = new byte[2];

		readFully(buf);

		return (DataInputStream.convertToShort(buf));
	}

	/*************************************************************************/

	/**
	  * This method reads 16 unsigned bits into a Java int value from the stream.
	  * It operates by reading two bytes from the stream and converting them to 
	  * a single Java <code>int</code>  The two bytes are stored most
	  * significant byte first (i.e., "big endian") regardless of the native
	  * host byte ordering. 
	  * <p>
	  * As an example, if <code>byte1</code> and code{byte2</code> represent the first
	  * and second byte read from the stream respectively, they will be
	  * transformed to an <code>int</code> in the following manner:
	  * <p>
	  * <code>(int)(((byte1 & 0xFF) << 8) + (byte2 & 0xFF))</code>
	  * <p>
	  * The value returned is in the range of 0 to 65535.
	  * <p>
	  * This method can read an unsigned short written by an object implementing
	  * the <code>writeUnsignedShort()</code> method in the <code>DataOutput</code> interface.
	  *
	  * @return The unsigned short value read as a Java <code>int</code>
	  *
	  * @exception EOFException If end of file is reached before reading the value
	  * @exception IOException If any other error occurs
	  */
	public final int readUnsignedShort() throws EOFException, IOException {
		byte[] buf = new byte[2];

		readFully(buf);

		return (DataInputStream.convertToUnsignedShort(buf));
	}

	/**
	  * This method reads a Java <code>int</code> value from an input stream
	  * It operates by reading four bytes from the stream and converting them to 
	  * a single Java <code>int</code>  The bytes are stored most
	  * significant byte first (i.e., "big endian") regardless of the native
	  * host byte ordering. 
	  * <p>
	  * As an example, if <code>byte1</code> through <code>byte4</code> represent the first
	  * four bytes read from the stream, they will be
	  * transformed to an <code>int</code> in the following manner:
	  * <p>
	  * <code>(int)(((byte1 & 0xFF) << 24) + ((byte2 & 0xFF) << 16) + 
	  * ((byte3 & 0xFF) << 8) + (byte4 & 0xFF)))</code>
	  * <p>
	  * The value returned is in the range of 0 to 65535.
	  * <p>
	  * This method can read an <code>int</code> written by an object implementing the
	  * <code>writeInt()</code> method in the <code>DataOutput</code> interface.
	  *
	  * @return The <code>int</code> value read
	  *
	  * @exception EOFException If end of file is reached before reading the int
	  * @exception IOException If any other error occurs
	  *
	  * @see DataOutput
	  */
	public final int readInt() throws EOFException, IOException {
		byte[] buf = new byte[4];

		readFully(buf);

		return (DataInputStream.convertToInt(buf));
	}

	/**
	  * This method reads a Java long value from an input stream
	  * It operates by reading eight bytes from the stream and converting them to 
	  * a single Java <code>long</code>  The bytes are stored most
	  * significant byte first (i.e., "big endian") regardless of the native
	  * host byte ordering. 
	  * <p>
	  * As an example, if <code>byte1</code> through <code>byte8</code> represent the first
	  * eight bytes read from the stream, they will be
	  * transformed to an <code>long</code> in the following manner:
	  * <p>
	  * <code>(long)((((long)byte1 & 0xFF) << 56) + (((long)byte2 & 0xFF) << 48) + 
	  * (((long)byte3 & 0xFF) << 40) + (((long)byte4 & 0xFF) << 32) + 
	  * (((long)byte5 & 0xFF) << 24) + (((long)byte6 & 0xFF) << 16) + 
	  * (((long)byte7 & 0xFF) << 8) + ((long)byte9 & 0xFF)))</code>
	  * <p>
	  * The value returned is in the range of 0 to 65535.
	  * <p>
	  * This method can read an <code>long</code> written by an object implementing the
	  * <code>writeLong()</code> method in the <code>DataOutput</code> interface.
	  *
	  * @return The <code>long</code> value read
	  *
	  * @exception EOFException If end of file is reached before reading the long
	  * @exception IOException If any other error occurs
	  *
	  * @see DataOutput
	  */
	public final long readLong() throws EOFException, IOException {
		byte[] buf = new byte[8];

		readFully(buf);

		return (DataInputStream.convertToLong(buf));
	}

	/**
	  * This method reads a Java float value from an input stream.  It operates
	  * by first reading an <code>int</code> value from the stream by calling the
	  * <code>readInt()</code> method in this interface, then converts that <code>int</code>
	  * to a <code>float</code> using the <code>intBitsToFloat</code> method in 
	  * the class <code>java.lang.Float</code>
	  * <p>
	  * This method can read a <code>float</code> written by an object implementing the
	  * <code>writeFloat()</code> method in the <code>DataOutput</code> interface.
	  *
	  * @return The <code>float</code> value read
	  *
	  * @exception EOFException If end of file is reached before reading the float
	  * @exception IOException If any other error occurs
	  *
	  * @see java.lang.Float
	  * @see DataOutput
	  */
	public final float readFloat() throws EOFException, IOException {
		int val = readInt();

		return (Float.intBitsToFloat(val));
	}

	/**
	  * This method reads a Java double value from an input stream.  It operates
	  * by first reading a <code>logn</code> value from the stream by calling the
	  * <code>readLong()</code> method in this interface, then converts that <code>long</code>
	  * to a <code>double</code> using the <code>longBitsToDouble</code> method in 
	  * the class <code>java.lang.Double</code>
	  * <p>
	  * This method can read a <code>double</code> written by an object implementing the
	  * <code>writeDouble()</code> method in the <code>DataOutput</code> interface.
	  *
	  * @return The <code>double</code> value read
	  *
	  * @exception EOFException If end of file is reached before reading the double
	  * @exception IOException If any other error occurs
	  *
	  * @see java.lang.Double
	  * @see DataOutput
	  */
	public final double readDouble() throws EOFException, IOException {
		long val = readLong();

		return (Double.longBitsToDouble(val));
	}

	/**
	  * This method reads the next line of text data from an input stream.
	  * It operates by reading bytes and converting those bytes to <code>char</code>
	  * values by treating the byte read as the low eight bits of the <code>char</code>
	  * and using <code>0</code> as the high eight bits.  Because of this, it does
	  * not support the full 16-bit Unicode character set.
	  * <p>
	  * The reading of bytes ends when either the end of file or a line terminator
	  * is encountered.  The bytes read are then returned as a <code>String</code>
	  * A line terminator is a byte sequence consisting of either 
	  * <code>\r</code> <code>\n</code> or <code>\r\n</code>  These termination charaters are
	  * discarded and are not returned as part of the string.
	  * <p>
	  * This method can read data that was written by an object implementing the
	  * <code>writeLine()</code> method in <code>DataOutput</code>
	  *
	  * @return The line read as a <code>String</code>
	  *
	  * @exception IOException If an error occurs
	  *
	  * @see DataOutput
	  *
	  * @deprecated
	  */
	public synchronized final String readLine() throws IOException {
		StringBuffer sb = new StringBuffer("");

		for (;;) {
			int byte_read = read();

			if (byte_read == -1)
				return (sb.toString());

			char c = (char)byte_read;

			if (c == '\r') {
				byte_read = read();
				if (((char)byte_read) != '\n')
					seek(getFilePointer() - 1);

				return (sb.toString());
			}

			if (c == '\n')
				return (sb.toString());

			sb.append(c);
		}
	}

	/**
	  * This method reads a <code>String</code> from an input stream that is encoded in
	  * a modified UTF-8 format.  This format has a leading two byte sequence
	  * that contains the remaining number of bytes to read.  This two byte
	  * sequence is read using the <code>readUnsignedShort()</code> method of this
	  * interface.
	  * <p>
	  * After the number of remaining bytes have been determined, these bytes
	  * are read an transformed into <code>char</code> values.  These <code>char</code> values
	  * are encoded in the stream using either a one, two, or three byte format.
	  * The particular format in use can be determined by examining the first
	  * byte read.  
	  * <p>
	  * If the first byte has a high order bit of 0 then
	  * that character consists on only one byte.  This character value consists
	  * of seven bits that are at positions 0 through 6 of the byte.  As an
	  * example, if <code>byte1</code> is the byte read from the stream, it would
	  * be converted to a <code>char</code> like so:
	  * <p>
	  * <code>(char)byte1</code>
	  * <p>
	  * If the first byte has <code>110</code> as its high order bits, then the 
	  * character consists of two bytes.  The bits that make up the character
	  * value are in positions 0 through 4 of the first byte and bit positions
	  * 0 through 5 of the second byte.  (The second byte should have 
	  * 10 as its high order bits).  These values are in most significant
	  * byte first (i.e., "big endian") order.
	  * <p>
	  * As an example, if <code>byte1</code> and <code>byte2</code> are the first two bytes
	  * read respectively, and the high order bits of them match the patterns
	  * which indicate a two byte character encoding, then they would be
	  * converted to a Java <code>char</code> like so:
	  * <p>
	  * <code>(char)(((byte1 & 0x1F) << 6) | (byte2 & 0x3F))</code>
	  * <p>
	  * If the first byte has a <code>1110</code> as its high order bits, then the
	  * character consists of three bytes.  The bits that make up the character
	  * value are in positions 0 through 3 of the first byte and bit positions
	  * 0 through 5 of the other two bytes.  (The second and third bytes should
	  * have <code>10</code> as their high order bits).  These values are in most
	  * significant byte first (i.e., "big endian") order.
	  * <p>
	  * As an example, if <code>byte1</code> <code>byte2</code> and <code>byte3</code> are the
	  * three bytes read, and the high order bits of them match the patterns
	  * which indicate a three byte character encoding, then they would be
	  * converted to a Java <code>char</code> like so:
	  * <p>
	  * <code>(char)(((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F))</code>
	  * <p>
	  * Note that all characters are encoded in the method that requires the
	  * fewest number of bytes with the exception of the character with the
	  * value of <code>&#92;u0000</code> which is encoded as two bytes.  This is a 
	  * modification of the UTF standard used to prevent C language style
	  * <code>NUL</code> values from appearing in the byte stream.
	  * <p>
	  * This method can read data that was written by an object implementing the
	  * <code>writeUTF()</code> method in <code>DataOutput</code>
	  * 
	  * @return The <code>String</code> read
	  *

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?