Java_io体系之RandomAccessFile简介、走进源码及示例――20(三)

2014-11-24 07:53:39 · 作者: · 浏览: 3
)) imode |= O_SYNC; else if (mode.equals("rwd")) imode |= O_DSYNC; else imode = -1; } } if (imode < 0) throw new IllegalArgumentException("Illegal mode \"" + mode + "\" must be one of " + "\"r\", \"rw\", \"rws\"," + " or \"rwd\""); SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkRead(name); if (rw) { security.checkWrite(name); } } if (name == null) { throw new NullPointerException(); } fd = new FileDescriptor(); open(name, imode); } /** * 返回指定文件的“文件描述符” */ public final FileDescriptor getFD() throws IOException { if (fd != null) return fd; throw new IOException(); } /** * 打开唯一与此流有关的FileChannel */ public final FileChannel getChannel() { synchronized (this) { if (channel == null) channel = FileChannelImpl.open(fd, true, rw, this); return channel; } } /** * 以指定模式打开指定文件 */ private native void open(String name, int mode) throws FileNotFoundException; // 'Read' primitives /** * 读取下一个字节并以整数形式返回 */ public native int read() throws IOException; /** * 将一组字节读取到b中 */ private native int readBytes(byte b[], int off, int len) throws IOException; /** * 将一组字节读取到b中、调用上面的方法 */ public int read(byte b[], int off, int len) throws IOException { return readBytes(b, off, len); } /** * 将一组字节读取到b中 */ public int read(byte b[]) throws IOException { return readBytes(b, 0, b.length); } /** * 读取字节放入到b中、直到存满b或者文件结尾、或者异常才结束 * 与read(byte[] b)的区别是返回值。 */ public final void readFully(byte b[]) throws IOException { readFully(b, 0, b.length); } /** * 读取字节放入到下标从off开始的后len个位置的字节数组b中、直到存满b或者文件结尾、或者异常才结束 * 与read(byte[] b, int off, int len)的区别是返回值。 */ public final void readFully(byte b[], int off, int len) throws IOException { int n = 0; do { int count = this.read(b, off + n, len - n); if (count < 0) throw new EOFException(); n += count; } while (n < len); } /** * 跳过n个字节、返回实际跳过的字节、他是通过从定位“文件指针”来实现跨过要跳过的字节 */ public int skipBytes(int n) throws IOException { long pos; long len; long newpos; if (n <= 0) { return 0; } pos = getFilePointer(); len = length(); newpos = pos + n; if (newpos > len) { newpos = len; } seek(newpos); /* return the actual number of bytes skipped */ return (int) (newpos - pos); } // 'Write' primitives /** * 在当前“文件指针”标示的地方写入一个字节 */ public native void write(int b) throws IOException; /** * 将b的一部分写入到文件中 */ private native void writeBytes(byte b[], int off, int len) throws IOException; /** * 将b写入到文件中 */ public void write(byte b[]) throws IOException { writeBytes(b, 0, b.length); } /** * 将b的一部分写入到文件中 */ public void write(byte b[], int off, int len) throws IOException { writeBytes(b, off, len); } // 'Random access' stuff /** * 返回当前文件的偏移量、即“文件描述符”的位置 */ public native long getFilePointer() throws IOException; /** * 设置“文件指针”的偏移量、从文件的开头开始计数、如果pos大于文件的长度、也不会改变文件的大小、 * 文件的大小只有在当“文件指针”指向文件最后的时候、再向文件中写入字节才会扩展。 */ public native void seek(long pos) throws IOException; /** * 返回文件的字节数 */ public native long length() throws IOException; /** * 设置文件长度: * if(newLength > originalLength) * 扩展文件长度、新增加的长度使用默认值填充; * else * 截取源文件的前originalLength字节、如果源文件的偏移量大于newLength、则将源文件的偏移量设为newLength; */ public native void setLength(long newLength) throws IOException; /** * 关闭此流、释放与此流有关的所有资源 */ public void close() throws IOException { if (channel != null) channel.close(); close0(); } // 一些方法是从DataInputStream/DataOutputStream中剽窃来的。。。 /** * 读取一个boolean型数据 */ public final boolean readBoolean() throws IOException { int ch = this.read(); if (ch < 0) throw new EOFException(); return (ch != 0); } /** * 从此文件读取一个有符号的八位值。 */ public final byte readByte() throws IOException { int ch = this.read(); if (ch < 0) throw new EOFException(); return (byte)(ch); } /** * 从此文件读取一个无符号的八位数。 */ public final int readUnsignedByte() throws IOException { int ch = this.read(); if (ch < 0) throw new EOFException(); return ch; } /** * 从此文件读取一个有符号的 16 位数。 */ public final short readShort() throws IOException { int ch1 = this.read(); int ch2 = this.read(); if ((ch1 | ch2) < 0) throw new EOFException(); return (short)((ch1 << 8) + (ch2 << 0)); } /** * 从此文件读取一个无符号的 16 位数。 */ public final int readUnsignedShort() throws IOException { int ch1 = this.read(); int ch2 = this.read(); if ((ch1 | ch2) < 0) throw new EOFException(); return (ch1 << 8) + (ch2 << 0); } /** * 从此文件读取一个字符。 */ public final char readChar() throws IOException { int ch1 = this.read(); int ch2 = this.read(); if ((ch1 | ch2) < 0) throw new EOFException(); return (char)((ch1 << 8) + (ch2 << 0)); } /** * 从此文件读取一个有符号的 32 位整数。 */ public final int readInt() throws IOException { int ch1 = this.read(); int ch2 = this.read(); int ch3 = this.read(); int ch4 = this.read(); if ((ch1 | ch2 | ch3 | ch4) < 0) throw new EOFException(); return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); } /** * 从此文件读取一个有符号的 64 位整数。 */ public final long readLong() throws IOException { return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL); } /** * 从此文件读取一个 float。 */ public final float readFloat() throws IOException { return Float.intBitsToFloat(readInt()); } /** * 从此文件读取一个 double。 */ public final double readDouble() throws IOException { return Double.longBitsToDouble(readLong()); } /** * 从此文件读取文本的下一行。 */ public final String readLine() throws IOException { StringBuffer input = new StringBuffer(); int c = -1; boolean eol = false; while (!eol) { switch (c = read()) { case -1: case '\n': eol = true; break; case '\r': eol = true; long cur = getFilePointer(); if ((read()) != '\n') { seek(cur); } break; default: input.append((char)c); break; } } if ((c == -1) && (input.length() == 0)) { return null; } return input.toString(); } /** * 从此文件读取一个字符串。 */ public final String readUTF() throws IOException { return DataInputStream.readUTF(this); } /** * 将一boolean型数据写入文件中 */ public final void writeBoolean(boolean v) throws IOException { write(v 1 : 0); //written++; } /** * 将一个字节写入文件中 */ public final void writeByte(int v) throws IOException { write(v); //written++; } /** * 将一个short写入文件 */ public final void writeShort(int v) throws IOException { write((v >
>> 8) & 0xFF); write((v >>> 0) & 0xFF); //written += 2; } /** * 将一个char写入文件 */ public final void writeChar(int v) throws IOException { write((v >>> 8) & 0xFF); write((v >>> 0) & 0xFF); //written += 2; } /** * 将一个int写入文件 */ public final void writeInt(int v) throws IOException { write((v >>> 24) & 0xFF); write((v >>> 16) & 0xFF); write((v >>> 8) & 0xFF); write((v >>> 0) & 0xFF); //written += 4; } /** * 将一个long写入文件 */ public final void writeLong(long v) throws IOException { write((int)(v >>> 56) & 0xFF); write((int)(v >>> 48) & 0xFF); write((int)(v >>> 40) & 0xFF); write((int)(v >>> 32) & 0xFF); write((int)(v >>> 24) & 0xFF); write((int)(v >>> 16) & 0xFF); write((int)(v >>> 8) & 0xFF); write((int)(v >>> 0) & 0xFF); //written += 8; } /** * 将一个long写入文件 */ public final void writeFloat(float v) throws IOException { writeInt(Float.floatToIntBits(v)); } /** * 将一个double写入文件。 */ public final void writeDouble(double v) throws IOException { writeLong(Double.doubleToLongBits(v)); } /** * 将一个字符串转换成一串有序字节写入 */ public final void writeBytes(String s) throws IOException { int len = s.length(); byte[] b = new byte[len]; s.getBytes(0, len, b, 0); writeBytes(b, 0, len); } /** * 将一个字符串以一串有序字符写入文件中 */ public final void writeChars(String s) throws IOException { int clen = s.length(); int blen = 2*clen; byte[] b = new byte[blen]; char[] c = new char[clen]; s.getChars(0, clen, c, 0); for (int i = 0, j = 0; i < clen; i++) { b[j++] = (byte)(c[i] >>> 8); b[j++] = (byte)(c[i] >>> 0); } writeBytes(b, 0, blen); } /** * 调用DataOutputStream的 writeUTF(String str, DataOutput out) 将一个字符串写入文件。 */ public final void writeUTF(String str) throws IOException { DataOutputStream.writeUTF(str, this); } private static native void initIDs(); private native void close0() throws IOException; static { init