home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / RandomAccessFile.java < prev    next >
Text File  |  1996-05-03  |  14KB  |  516 lines

  1. /*
  2.  * @(#)RandomAccessFile.java    1.27 95/11/13 David Brown
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.io;
  21.  
  22. import java.io.File;
  23.  
  24. /**
  25.  * Random access files can be constructed from file descriptors, file 
  26.  * names, or file objects.  This class provides a sense of security
  27.  * by offering methods that allow specified mode accesses of 
  28.  * read-only or read-write to files.
  29.  */
  30. public
  31. class RandomAccessFile implements DataOutput, DataInput {
  32.     private FileDescriptor fd;
  33.  
  34.     /**
  35.      * Creates a RandomAccessFile with the specified system dependent 
  36.      * file name and the specified mode.
  37.      * Mode "r" is for read-only and mode "rw" is for read+write.
  38.      * @param name the system dependent file name
  39.      * @param mode the access mode
  40.      * @exception IOException If an I/O error has occurred.
  41.      */
  42.     public RandomAccessFile(String name, String mode) throws IOException {
  43.     boolean rw = mode.equals("rw");
  44.     if (!rw && !mode.equals("r"))
  45.         throw new IllegalArgumentException("mode must be r or rw");
  46.     SecurityManager security = System.getSecurityManager();
  47.     if (security != null) {
  48.         security.checkRead(name);
  49.         if (rw) {
  50.         security.checkWrite(name);
  51.         }
  52.     }
  53.     fd = new FileDescriptor();
  54.     open(name, rw);
  55.     }
  56.     
  57.     /**
  58.      * Creates a RandomAccessFile from a specified File object
  59.      * and mode ("r" or "rw").     
  60.      * @param file the file object
  61.      * @param mode the access mode
  62.      */
  63.     public RandomAccessFile(File file, String mode) throws IOException {
  64.     this(file.getPath(), mode);
  65.     }
  66.  
  67.     /**
  68.      * Returns the opaque file descriptor object.
  69.      * @return the file descriptor.
  70.      */
  71.     public final FileDescriptor getFD() throws IOException {
  72.     if (fd != null) return fd;
  73.     throw new IOException();
  74.     }
  75.  
  76.     /**
  77.      * Opens a file and returns the file descriptor.  The file is 
  78.      * opened in read-write mode if writeable is true, else 
  79.      * the file is opened as read-only.
  80.      * @param name the name of the file
  81.      * @param writeable the boolean indicating whether file is 
  82.      * writeable or not.
  83.      */
  84.     private native void open(String name, boolean writeable) throws IOException;
  85.  
  86.     // 'Read' primitives
  87.     
  88.     /**
  89.      * Reads a byte of data. This method will block if no input is
  90.      * available.
  91.      * @return the byte read, or -1 if the end of the
  92.      *          stream is reached. 
  93.      * @exception IOException If an I/O error has occurred.
  94.      */
  95.     public native int read() throws IOException;
  96.  
  97.     /**
  98.      * Reads a sub array as a sequence of bytes. 
  99.      * @param b the data to be written
  100.      * @param off the start offset in the data
  101.      * @param len the number of bytes that are written
  102.      * @exception IOException If an I/O error has occurred.
  103.      */
  104.     private native int readBytes(byte b[], int off, int len) throws IOException;
  105.  
  106.     /**
  107.      * Reads a sub array as a sequence of bytes. 
  108.      * @param b the data to be written
  109.      * @param off the start offset in the data
  110.      * @param len the number of bytes that are written
  111.      * @exception IOException If an I/O error has occurred.
  112.      */
  113.     public int read(byte b[], int off, int len) throws IOException {
  114.     return readBytes(b, off, len);
  115.     }
  116.  
  117.     /**
  118.      * Reads data into an array of bytes.  This method blocks
  119.      * until some input is available.
  120.      * @return the actual number of bytes read, -1 is
  121.      *          returned when the end of the stream is reached.
  122.      * @exception IOException If an I/O error has occurred.    
  123.      */
  124.     public int read(byte b[]) throws IOException {
  125.     return readBytes(b, 0, b.length);
  126.     }
  127.    
  128.     /**
  129.      * Reads bytes, blocking until all bytes are read.
  130.      * @param b    the buffer into which the data is read
  131.      * @return  the actual number of bytes read, -1 is
  132.      *         returned when the end of the stream is reached.
  133.      * @exception IOException If an I/O error has occurred.
  134.      */
  135.     public final void readFully(byte b[]) throws IOException {
  136.     readFully(b, 0, b.length);
  137.     }
  138.  
  139.     /**
  140.      * Reads bytes, blocking until all bytes are read.
  141.      * @param b    the buffer into which the data is read
  142.      * @param off the start offset of the data
  143.      * @param len the maximum number of bytes read
  144.      * @return  the actual number of bytes read, -1 is
  145.      *         returned when the end of the stream is reached.
  146.      * @exception IOException If an I/O error has occurred.
  147.      */
  148.     public final void readFully(byte b[], int off, int len) throws IOException {
  149.     int n = 0;
  150.     while (n < len) {
  151.         int count = this.read(b, off + n, len - n);
  152.         if (count < 0)
  153.         throw new EOFException();
  154.         n += count;
  155.     }
  156.     }
  157.  
  158.  
  159.     public int skipBytes(int n) throws IOException {
  160.         seek(getFilePointer() + n);
  161.         return n;
  162.     }
  163.  
  164.     // 'Write' primitives
  165.  
  166.     /**
  167.      * Writes a byte of data. This method will block until the byte
  168.      * is actually written. 
  169.      * @param b the byte to be written
  170.      * @exception IOException If an I/O error has occurred. 
  171.      */
  172.     public native void write(int b) throws IOException;
  173.  
  174.     /**
  175.      * Writes a sub array as a sequence of bytes. 
  176.      * @param b the data to be written
  177.      * @param off the start offset in the data
  178.      * @param len the number of bytes that are written
  179.      * @exception IOException If an I/O error has occurred.
  180.      */
  181.     private native void writeBytes(byte b[], int off, int len) throws IOException;
  182.  
  183.     /**
  184.      * Writes an array of bytes. Will block until the bytes
  185.      * are actually written. 
  186.      * @param b the data to be written
  187.      * @exception IOException If an I/O error has occurred.
  188.      */
  189.     public void write(byte b[]) throws IOException {
  190.     writeBytes(b, 0, b.length); 
  191.     }
  192.  
  193.     /**
  194.      * Wrotes a sub array of bytes.
  195.      * @param b the data to be written
  196.      * @param off the start offset in the data
  197.      * @param len the number of bytes that are written
  198.      * @exception IOException If an I/O error has occurred. 
  199.      */
  200.     public void write(byte b[], int off, int len) throws IOException {
  201.     writeBytes(b, off, len);
  202.     }
  203.  
  204.     // 'Random access' stuff
  205.  
  206.  
  207.     /**
  208.      * Returns the current location of the file pointer.
  209.      */
  210.     public native long getFilePointer() throws IOException;
  211.  
  212.     /**
  213.      * Sets the file pointer to the specified absolute position.
  214.      * @param pos the absolute position
  215.      */
  216.     public native void seek(long pos) throws IOException;
  217.  
  218.     /**
  219.      * Returns the length of the file.
  220.      */
  221.     public native long length() throws IOException;
  222.  
  223.     /**
  224.      * Closes the file. 
  225.      * @exception IOException If an I/O error has occurred.
  226.      */
  227.     public native void close() throws IOException;
  228.  
  229.     //
  230.     //  Some "reading/writing Java data types" methods stolen from
  231.     //  DataInputStream and DataOutputStream.
  232.     //
  233.  
  234.     /**
  235.      * Reads a boolean.
  236.      */
  237.     public final boolean readBoolean() throws IOException {
  238.     int ch = this.read();
  239.     if (ch < 0)
  240.         throw new EOFException();
  241.     return (ch != 0);
  242.     }
  243.  
  244.     /**
  245.      * Reads a byte.
  246.      */
  247.     public final byte readByte() throws IOException {
  248.     int ch = this.read();
  249.     if (ch < 0)
  250.         throw new EOFException();
  251.     return (byte)(ch);
  252.     }
  253.  
  254.  
  255.     /**
  256.      * Reads an unsigned 8 bit byte.
  257.      * @return the 8 bit byte read.
  258.      */
  259.     public final int readUnsignedByte() throws IOException {
  260.     int ch = this.read();
  261.     if (ch < 0)
  262.         throw new EOFException();
  263.     return ch;
  264.     }
  265.  
  266.  
  267.     /**
  268.      * Reads 16 bit short.
  269.      * @return the read 16 bit short.
  270.      */
  271.     public final short readShort() throws IOException {
  272.     int ch1 = this.read();
  273.     int ch2 = this.read();
  274.     if ((ch1 | ch2) < 0)
  275.          throw new EOFException();
  276.     return (short)((ch1 << 8) + (ch2 << 0));
  277.     }
  278.  
  279.  
  280.     /**
  281.      * Reads 16 bit short.
  282.      * @return the read 16 bit short.
  283.      */
  284.     public final int readUnsignedShort() throws IOException {
  285.     int ch1 = this.read();
  286.     int ch2 = this.read();
  287.     if ((ch1 | ch2) < 0)
  288.          throw new EOFException();
  289.     return (ch1 << 8) + (ch2 << 0);
  290.     }
  291.  
  292.  
  293.     /**
  294.      * Reads a 16 bit char.
  295.      * @return the read 16 bit char. 
  296.      */
  297.     public final char readChar() throws IOException {
  298.     int ch1 = this.read();
  299.     int ch2 = this.read();
  300.     if ((ch1 | ch2) < 0)
  301.          throw new EOFException();
  302.     return (char)((ch1 << 8) + (ch2 << 0));
  303.     }
  304.  
  305.     /**
  306.      * Reads a 32 bit int.
  307.      * @return the read 32 bit integer.
  308.      */
  309.     public final int readInt() throws IOException {
  310.     int ch1 = this.read();
  311.     int ch2 = this.read();
  312.     int ch3 = this.read();
  313.     int ch4 = this.read();
  314.     if ((ch1 | ch2 | ch3 | ch4) < 0)
  315.          throw new EOFException();
  316.     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
  317.     }
  318.  
  319.     /**
  320.      * Reads a 64 bit long.
  321.      * @return the read 64 bit long.
  322.      */
  323.     public final long readLong() throws IOException {
  324.     return (this.readInt() << 32L) + (this.readInt() & 0xFFFFFFFFL);
  325.     }
  326.  
  327.     /**
  328.      * Reads a 32 bit float.
  329.      * @return the read 32 bit float.
  330.      */
  331.     public final float readFloat() throws IOException {
  332.     return Float.intBitsToFloat(readInt());
  333.     }
  334.  
  335.     /**
  336.      * Reads a 64 bit double.
  337.      * @return the read 64 bit double.
  338.      */
  339.     public final double readDouble() throws IOException {
  340.     return Double.longBitsToDouble(readLong());
  341.     }
  342.  
  343.     /**
  344.      * Reads a line terminated by a '\n' or EOF.
  345.      */
  346.     public final String readLine() throws IOException {
  347.     StringBuffer input = new StringBuffer();
  348.     int c;
  349.  
  350.     while (((c = read()) != -1) && (c != '\n')) {
  351.         input.append((char)c);
  352.     }
  353.     if ((c == -1) && (input.length() == 0)) {
  354.         return null;
  355.     }
  356.     return input.toString();
  357.     }
  358.  
  359.     /**
  360.      * Reads a UTF formatted String.
  361.      */
  362.     public final String readUTF() throws IOException {
  363.     return DataInputStream.readUTF(this);
  364.     }
  365.  
  366.     /**
  367.      * Writes a boolean.
  368.      * @param v the boolean value
  369.      */
  370.     public final void writeBoolean(boolean v) throws IOException {
  371.     write(v ? 1 : 0);
  372.     //written++;
  373.     }
  374.  
  375.     /**
  376.      * Writes a byte.
  377.      * @param v the byte
  378.      */
  379.     public final void writeByte(int v) throws IOException {
  380.     write(v);
  381.     //written++;
  382.     }
  383.  
  384.     /**
  385.      * Writes a short.
  386.      * @param v the short
  387.      */
  388.     public final void writeShort(int v) throws IOException {
  389.     write((v >>> 8) & 0xFF);
  390.     write((v >>> 0) & 0xFF);
  391.     //written += 2;
  392.     }
  393.  
  394.     /**
  395.      * Writes a character.
  396.      * @param v the char
  397.      */
  398.     public final void writeChar(int v) throws IOException {
  399.     write((v >>> 8) & 0xFF);
  400.     write((v >>> 0) & 0xFF);
  401.     //written += 2;
  402.     }
  403.  
  404.     /**
  405.      * Writes an integer.
  406.      * @param v the integer
  407.      */
  408.     public final void writeInt(int v) throws IOException {
  409.     write((v >>> 24) & 0xFF);
  410.     write((v >>> 16) & 0xFF);
  411.     write((v >>>  8) & 0xFF);
  412.     write((v >>>  0) & 0xFF);
  413.     //written += 4;
  414.     }
  415.  
  416.     /**
  417.      * Writes a long.
  418.      * @param v the long
  419.      */
  420.     public final void writeLong(long v) throws IOException {
  421.     write((int)(v >>> 56) & 0xFF);
  422.     write((int)(v >>> 48) & 0xFF);
  423.     write((int)(v >>> 40) & 0xFF);
  424.     write((int)(v >>> 32) & 0xFF);
  425.     write((int)(v >>> 24) & 0xFF);
  426.     write((int)(v >>> 16) & 0xFF);
  427.     write((int)(v >>>  8) & 0xFF);
  428.     write((int)(v >>>  0) & 0xFF);
  429.     //written += 8;
  430.     }
  431.  
  432.     /*
  433.      * Writes a 32 bit float.
  434.      * @param v the float value to be written
  435.      */
  436.     public final void writeFloat(float v) throws IOException {
  437.     writeInt(Float.floatToIntBits(v));
  438.     }
  439.  
  440.  
  441.     /*
  442.      * Writes a 64 bit double.
  443.      * @param v the double value to be written
  444.      */
  445.     public final void writeDouble(double v) throws IOException {
  446.     writeLong(Double.doubleToLongBits(v));
  447.     }
  448.  
  449.  
  450.     /**
  451.      * Writes a String as a sequence of bytes.
  452.      * @param s the String
  453.      */
  454.     public final void writeBytes(String s) throws IOException {
  455.     int len = s.length();
  456.     for (int i = 0 ; i < len ; i++) {
  457.         write((byte)s.charAt(i));
  458.     }
  459.     //written += len;
  460.     }
  461.  
  462.     /**
  463.      * Writes a String as a sequence of chars.
  464.      * @param s the String
  465.      */
  466.     public final void writeChars(String s) throws IOException {
  467.     int len = s.length();
  468.     for (int i = 0 ; i < len ; i++) {
  469.         int v = s.charAt(i);
  470.         write((v >>> 8) & 0xFF);
  471.         write((v >>> 0) & 0xFF);
  472.     }
  473.     //written += len * 2;
  474.     }
  475.  
  476.     /**
  477.      * Writes a String in UTF format.
  478.      * @param str the String
  479.      */
  480.     public final void writeUTF(String str) throws IOException {
  481.     int strlen = str.length();
  482.     int utflen = 0;
  483.  
  484.     for (int i = 0 ; i < strlen ; i++) {
  485.         int c = str.charAt(i);
  486.         if ((c >= 0x0001) && (c <= 0x007F)) {
  487.         utflen++;
  488.         } else if (c > 0x07FF) {
  489.         utflen += 3;
  490.         } else {
  491.         utflen += 2;
  492.         }
  493.     }
  494.  
  495.     write((utflen >>> 8) & 0xFF);
  496.     write((utflen >>> 0) & 0xFF);
  497.     for (int i = 0 ; i < strlen ; i++) {
  498.         int c = str.charAt(i);
  499.         if ((c >= 0x0001) && (c <= 0x007F)) {
  500.         write(c);
  501.         } else if (c > 0x07FF) {
  502.         write(0xE0 | ((c >> 12) & 0x0F));
  503.         write(0x80 | ((c >>  6) & 0x3F));
  504.         write(0x80 | ((c >>  0) & 0x3F));
  505.         //written += 2;
  506.         } else {
  507.         write(0xC0 | ((c >>  6) & 0x1F));
  508.         write(0x80 | ((c >>  0) & 0x3F));
  509.         //written += 1;
  510.         }
  511.     }
  512.     //written += strlen + 2;
  513.     }
  514.  
  515. }
  516.