1 7 8 package java.io; 9 10 import java.nio.channels.FileChannel ; 11 import sun.nio.ch.FileChannelImpl; 12 13 14 41 42 public class RandomAccessFile implements DataOutput , DataInput , Closeable { 43 44 private FileDescriptor fd; 45 private FileChannel channel = null; 46 private boolean rw; 47 48 private static final int O_RDONLY = 1; 49 private static final int O_RDWR = 2; 50 private static final int O_SYNC = 4; 51 private static final int O_DSYNC = 8; 52 53 95 public RandomAccessFile(String name, String mode) 96 throws FileNotFoundException 97 { 98 this(name != null ? new File (name) : null, mode); 99 } 100 101 177 public RandomAccessFile(File file, String mode) 178 throws FileNotFoundException 179 { 180 String name = (file != null ? file.getPath() : null); 181 int imode = -1; 182 if (mode.equals("r")) 183 imode = O_RDONLY; 184 else if (mode.startsWith("rw")) { 185 imode = O_RDWR; 186 rw = true; 187 if (mode.length() > 2) { 188 if (mode.equals("rws")) 189 imode |= O_SYNC; 190 else if (mode.equals("rwd")) 191 imode |= O_DSYNC; 192 else 193 imode = -1; 194 } 195 } 196 if (imode < 0) 197 throw new IllegalArgumentException ("Illegal mode \"" + mode 198 + "\" must be one of " 199 + "\"r\", \"rw\", \"rws\"," 200 + " or \"rwd\""); 201 SecurityManager security = System.getSecurityManager(); 202 if (security != null) { 203 security.checkRead(name); 204 if (rw) { 205 security.checkWrite(name); 206 } 207 } 208 if (name == null) { 209 throw new NullPointerException (); 210 } 211 fd = new FileDescriptor (); 212 open(name, imode); 213 } 214 215 223 public final FileDescriptor getFD() throws IOException { 224 if (fd != null) return fd; 225 throw new IOException (); 226 } 227 228 246 public final FileChannel getChannel() { 247 synchronized (this) { 248 if (channel == null) 249 channel = FileChannelImpl.open(fd, true, rw, this); 250 return channel; 251 } 252 } 253 254 265 private native void open(String name, int mode) 266 throws FileNotFoundException ; 267 268 270 285 public native int read() throws IOException ; 286 287 294 private native int readBytes(byte b[], int off, int len) throws IOException ; 295 296 314 public int read(byte b[], int off, int len) throws IOException { 315 return readBytes(b, off, len); 316 } 317 318 334 public int read(byte b[]) throws IOException { 335 return readBytes(b, 0, b.length); 336 } 337 338 350 public final void readFully(byte b[]) throws IOException { 351 readFully(b, 0, b.length); 352 } 353 354 368 public final void readFully(byte b[], int off, int len) throws IOException { 369 int n = 0; 370 do { 371 int count = this.read(b, off + n, len - n); 372 if (count < 0) 373 throw new EOFException (); 374 n += count; 375 } while (n < len); 376 } 377 378 394 public int skipBytes(int n) throws IOException { 395 long pos; 396 long len; 397 long newpos; 398 399 if (n <= 0) { 400 return 0; 401 } 402 pos = getFilePointer(); 403 len = length(); 404 newpos = pos + n; 405 if (newpos > len) { 406 newpos = len; 407 } 408 seek(newpos); 409 410 411 return (int) (newpos - pos); 412 } 413 414 416 423 public native void write(int b) throws IOException ; 424 425 433 private native void writeBytes(byte b[], int off, int len) throws IOException ; 434 435 442 public void write(byte b[]) throws IOException { 443 writeBytes(b, 0, b.length); 444 } 445 446 455 public void write(byte b[], int off, int len) throws IOException { 456 writeBytes(b, off, len); 457 } 458 459 461 468 public native long getFilePointer() throws IOException ; 469 470 484 public native void seek(long pos) throws IOException ; 485 486 492 public native long length() throws IOException ; 493 494 513 public native void setLength(long newLength) throws IOException ; 514 515 529 public void close() throws IOException { 530 if (channel != null) 531 channel.close(); 532 close0(); 533 } 534 535 540 552 public final boolean readBoolean() throws IOException { 553 int ch = this.read(); 554 if (ch < 0) 555 throw new EOFException (); 556 return (ch != 0); 557 } 558 559 577 public final byte readByte() throws IOException { 578 int ch = this.read(); 579 if (ch < 0) 580 throw new EOFException (); 581 return (byte)(ch); 582 } 583 584 597 public final int readUnsignedByte() throws IOException { 598 int ch = this.read(); 599 if (ch < 0) 600 throw new EOFException (); 601 return ch; 602 } 603 604 624 public final short readShort() throws IOException { 625 int ch1 = this.read(); 626 int ch2 = this.read(); 627 if ((ch1 | ch2) < 0) 628 throw new EOFException (); 629 return (short)((ch1 << 8) + (ch2 << 0)); 630 } 631 632 652 public final int readUnsignedShort() throws IOException { 653 int ch1 = this.read(); 654 int ch2 = this.read(); 655 if ((ch1 | ch2) < 0) 656 throw new EOFException (); 657 return (ch1 << 8) + (ch2 << 0); 658 } 659 660 679 public final char readChar() throws IOException { 680 int ch1 = this.read(); 681 int ch2 = this.read(); 682 if ((ch1 | ch2) < 0) 683 throw new EOFException (); 684 return (char)((ch1 << 8) + (ch2 << 0)); 685 } 686 687 707 public final int readInt() throws IOException { 708 int ch1 = this.read(); 709 int ch2 = this.read(); 710 int ch3 = this.read(); 711 int ch4 = this.read(); 712 if ((ch1 | ch2 | ch3 | ch4) < 0) 713 throw new EOFException (); 714 return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); 715 } 716 717 745 public final long readLong() throws IOException { 746 return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL); 747 } 748 749 768 public final float readFloat() throws IOException { 769 return Float.intBitsToFloat(readInt()); 770 } 771 772 791 public final double readDouble() throws IOException { 792 return Double.longBitsToDouble(readLong()); 793 } 794 795 818 819 public final String readLine() throws IOException { 820 StringBuffer input = new StringBuffer (); 821 int c = -1; 822 boolean eol = false; 823 824 while (!eol) { 825 switch (c = read()) { 826 case -1: 827 case '\n': 828 eol = true; 829 break; 830 case '\r': 831 eol = true; 832 long cur = getFilePointer(); 833 if ((read()) != '\n') { 834 seek(cur); 835 } 836 break; 837 default: 838 input.append((char)c); 839 break; 840 } 841 } 842 843 if ((c == -1) && (input.length() == 0)) { 844 return null; 845 } 846 return input.toString(); 847 } 848 849 874 public final String readUTF() throws IOException { 875 return DataInputStream.readUTF(this); 876 } 877 878 888 public final void writeBoolean(boolean v) throws IOException { 889 write(v ? 1 : 0); 890 } 892 893 900 public final void writeByte(int v) throws IOException { 901 write(v); 902 } 904 905 912 public final void writeShort(int v) throws IOException { 913 write((v >>> 8) & 0xFF); 914 write((v >>> 0) & 0xFF); 915 } 917 918 926 public final void writeChar(int v) throws IOException { 927 write((v >>> 8) & 0xFF); 928 write((v >>> 0) & 0xFF); 929 } 931 932 939 public final void writeInt(int v) throws IOException { 940 write((v >>> 24) & 0xFF); 941 write((v >>> 16) & 0xFF); 942 write((v >>> 8) & 0xFF); 943 write((v >>> 0) & 0xFF); 944 } 946 947 954 public final void writeLong(long v) throws IOException { 955 write((int)(v >>> 56) & 0xFF); 956 write((int)(v >>> 48) & 0xFF); 957 write((int)(v >>> 40) & 0xFF); 958 write((int)(v >>> 32) & 0xFF); 959 write((int)(v >>> 24) & 0xFF); 960 write((int)(v >>> 16) & 0xFF); 961 write((int)(v >>> 8) & 0xFF); 962 write((int)(v >>> 0) & 0xFF); 963 } 965 966 977 public final void writeFloat(float v) throws IOException { 978 writeInt(Float.floatToIntBits(v)); 979 } 980 981 992 public final void writeDouble(double v) throws IOException { 993 writeLong(Double.doubleToLongBits(v)); 994 } 995 996 1005 public final void writeBytes(String s) throws IOException { 1006 int len = s.length(); 1007 byte[] b = new byte[len]; 1008 s.getBytes(0, len, b, 0); 1009 writeBytes(b, 0, len); 1010 } 1011 1012 1022 public final void writeChars(String s) throws IOException { 1023 int clen = s.length(); 1024 int blen = 2*clen; 1025 byte[] b = new byte[blen]; 1026 char[] c = new char[clen]; 1027 s.getChars(0, clen, c, 0); 1028 for (int i = 0, j = 0; i < clen; i++) { 1029 b[j++] = (byte)(c[i] >>> 8); 1030 b[j++] = (byte)(c[i] >>> 0); 1031 } 1032 writeBytes(b, 0, blen); 1033 } 1034 1035 1051 public final void writeUTF(String str) throws IOException { 1052 DataOutputStream.writeUTF(str, this); 1053 } 1054 1055 private static native void initIDs(); 1056 1057 private native void close0() throws IOException ; 1058 1059 static { 1060 initIDs(); 1061 } 1062 1063} 1064 | Popular Tags |