home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / io / RandomAccessFile.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  6.6 KB  |  285 lines

  1. package java.io;
  2.  
  3. public class RandomAccessFile implements DataOutput, DataInput {
  4.    // $FF: renamed from: fd java.io.FileDescriptor
  5.    private FileDescriptor field_0;
  6.  
  7.    public RandomAccessFile(String name, String mode) throws IOException {
  8.       boolean rw = mode.equals("rw");
  9.       if (!rw && !mode.equals("r")) {
  10.          throw new IllegalArgumentException("mode must be r or rw");
  11.       } else {
  12.          SecurityManager security = System.getSecurityManager();
  13.          if (security != null) {
  14.             security.checkRead(name);
  15.             if (rw) {
  16.                security.checkWrite(name);
  17.             }
  18.          }
  19.  
  20.          this.field_0 = new FileDescriptor();
  21.          this.open(name, rw);
  22.       }
  23.    }
  24.  
  25.    public RandomAccessFile(File file, String mode) throws IOException {
  26.       this(file.getPath(), mode);
  27.    }
  28.  
  29.    public final FileDescriptor getFD() throws IOException {
  30.       if (this.field_0 != null) {
  31.          return this.field_0;
  32.       } else {
  33.          throw new IOException();
  34.       }
  35.    }
  36.  
  37.    private native void open(String var1, boolean var2) throws IOException;
  38.  
  39.    public native int read() throws IOException;
  40.  
  41.    private native int readBytes(byte[] var1, int var2, int var3) throws IOException;
  42.  
  43.    public int read(byte[] b, int off, int len) throws IOException {
  44.       return this.readBytes(b, off, len);
  45.    }
  46.  
  47.    public int read(byte[] b) throws IOException {
  48.       return this.readBytes(b, 0, b.length);
  49.    }
  50.  
  51.    public final void readFully(byte[] b) throws IOException {
  52.       this.readFully(b, 0, b.length);
  53.    }
  54.  
  55.    public final void readFully(byte[] b, int off, int len) throws IOException {
  56.       int count;
  57.       for(int n = 0; n < len; n += count) {
  58.          count = this.read(b, off + n, len - n);
  59.          if (count < 0) {
  60.             throw new EOFException();
  61.          }
  62.       }
  63.  
  64.    }
  65.  
  66.    public int skipBytes(int n) throws IOException {
  67.       this.seek(this.getFilePointer() + (long)n);
  68.       return n;
  69.    }
  70.  
  71.    public native void write(int var1) throws IOException;
  72.  
  73.    private native void writeBytes(byte[] var1, int var2, int var3) throws IOException;
  74.  
  75.    public void write(byte[] b) throws IOException {
  76.       this.writeBytes(b, 0, b.length);
  77.    }
  78.  
  79.    public void write(byte[] b, int off, int len) throws IOException {
  80.       this.writeBytes(b, off, len);
  81.    }
  82.  
  83.    public native long getFilePointer() throws IOException;
  84.  
  85.    public native void seek(long var1) throws IOException;
  86.  
  87.    public native long length() throws IOException;
  88.  
  89.    public native void close() throws IOException;
  90.  
  91.    public final boolean readBoolean() throws IOException {
  92.       int ch = this.read();
  93.       if (ch < 0) {
  94.          throw new EOFException();
  95.       } else {
  96.          return ch != 0;
  97.       }
  98.    }
  99.  
  100.    public final byte readByte() throws IOException {
  101.       int ch = this.read();
  102.       if (ch < 0) {
  103.          throw new EOFException();
  104.       } else {
  105.          return (byte)ch;
  106.       }
  107.    }
  108.  
  109.    public final int readUnsignedByte() throws IOException {
  110.       int ch = this.read();
  111.       if (ch < 0) {
  112.          throw new EOFException();
  113.       } else {
  114.          return ch;
  115.       }
  116.    }
  117.  
  118.    public final short readShort() throws IOException {
  119.       int ch1 = this.read();
  120.       int ch2 = this.read();
  121.       if ((ch1 | ch2) < 0) {
  122.          throw new EOFException();
  123.       } else {
  124.          return (short)((ch1 << 8) + ch2);
  125.       }
  126.    }
  127.  
  128.    public final int readUnsignedShort() throws IOException {
  129.       int ch1 = this.read();
  130.       int ch2 = this.read();
  131.       if ((ch1 | ch2) < 0) {
  132.          throw new EOFException();
  133.       } else {
  134.          return (ch1 << 8) + ch2;
  135.       }
  136.    }
  137.  
  138.    public final char readChar() throws IOException {
  139.       int ch1 = this.read();
  140.       int ch2 = this.read();
  141.       if ((ch1 | ch2) < 0) {
  142.          throw new EOFException();
  143.       } else {
  144.          return (char)((ch1 << 8) + ch2);
  145.       }
  146.    }
  147.  
  148.    public final int readInt() throws IOException {
  149.       int ch1 = this.read();
  150.       int ch2 = this.read();
  151.       int ch3 = this.read();
  152.       int ch4 = this.read();
  153.       if ((ch1 | ch2 | ch3 | ch4) < 0) {
  154.          throw new EOFException();
  155.       } else {
  156.          return (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + ch4;
  157.       }
  158.    }
  159.  
  160.    public final long readLong() throws IOException {
  161.       return ((long)this.readInt() << 32) + ((long)this.readInt() & 4294967295L);
  162.    }
  163.  
  164.    public final float readFloat() throws IOException {
  165.       return Float.intBitsToFloat(this.readInt());
  166.    }
  167.  
  168.    public final double readDouble() throws IOException {
  169.       return Double.longBitsToDouble(this.readLong());
  170.    }
  171.  
  172.    public final String readLine() throws IOException {
  173.       StringBuffer input = new StringBuffer();
  174.  
  175.       int c;
  176.       while((c = this.read()) != -1 && c != 10) {
  177.          input.append((char)c);
  178.       }
  179.  
  180.       return c == -1 && input.length() == 0 ? null : input.toString();
  181.    }
  182.  
  183.    public final String readUTF() throws IOException {
  184.       return DataInputStream.readUTF(this);
  185.    }
  186.  
  187.    public final void writeBoolean(boolean v) throws IOException {
  188.       this.write(v ? 1 : 0);
  189.    }
  190.  
  191.    public final void writeByte(int v) throws IOException {
  192.       this.write(v);
  193.    }
  194.  
  195.    public final void writeShort(int v) throws IOException {
  196.       this.write(v >>> 8 & 255);
  197.       this.write(v & 255);
  198.    }
  199.  
  200.    public final void writeChar(int v) throws IOException {
  201.       this.write(v >>> 8 & 255);
  202.       this.write(v & 255);
  203.    }
  204.  
  205.    public final void writeInt(int v) throws IOException {
  206.       this.write(v >>> 24 & 255);
  207.       this.write(v >>> 16 & 255);
  208.       this.write(v >>> 8 & 255);
  209.       this.write(v & 255);
  210.    }
  211.  
  212.    public final void writeLong(long v) throws IOException {
  213.       this.write((int)(v >>> 56) & 255);
  214.       this.write((int)(v >>> 48) & 255);
  215.       this.write((int)(v >>> 40) & 255);
  216.       this.write((int)(v >>> 32) & 255);
  217.       this.write((int)(v >>> 24) & 255);
  218.       this.write((int)(v >>> 16) & 255);
  219.       this.write((int)(v >>> 8) & 255);
  220.       this.write((int)v & 255);
  221.    }
  222.  
  223.    public final void writeFloat(float v) throws IOException {
  224.       this.writeInt(Float.floatToIntBits(v));
  225.    }
  226.  
  227.    public final void writeDouble(double v) throws IOException {
  228.       this.writeLong(Double.doubleToLongBits(v));
  229.    }
  230.  
  231.    public final void writeBytes(String s) throws IOException {
  232.       int len = s.length();
  233.  
  234.       for(int i = 0; i < len; ++i) {
  235.          this.write((byte)s.charAt(i));
  236.       }
  237.  
  238.    }
  239.  
  240.    public final void writeChars(String s) throws IOException {
  241.       int len = s.length();
  242.  
  243.       for(int i = 0; i < len; ++i) {
  244.          int v = s.charAt(i);
  245.          this.write(v >>> 8 & 255);
  246.          this.write(v & 255);
  247.       }
  248.  
  249.    }
  250.  
  251.    public final void writeUTF(String str) throws IOException {
  252.       int strlen = str.length();
  253.       int utflen = 0;
  254.  
  255.       for(int i = 0; i < strlen; ++i) {
  256.          int c = str.charAt(i);
  257.          if (c >= 1 && c <= 127) {
  258.             ++utflen;
  259.          } else if (c > 2047) {
  260.             utflen += 3;
  261.          } else {
  262.             utflen += 2;
  263.          }
  264.       }
  265.  
  266.       this.write(utflen >>> 8 & 255);
  267.       this.write(utflen & 255);
  268.  
  269.       for(int i = 0; i < strlen; ++i) {
  270.          int c = str.charAt(i);
  271.          if (c >= 1 && c <= 127) {
  272.             this.write(c);
  273.          } else if (c > 2047) {
  274.             this.write(224 | c >> 12 & 15);
  275.             this.write(128 | c >> 6 & 63);
  276.             this.write(128 | c & 63);
  277.          } else {
  278.             this.write(192 | c >> 6 & 31);
  279.             this.write(128 | c & 63);
  280.          }
  281.       }
  282.  
  283.    }
  284. }
  285.