Annotation of java/classes/org/w3c/util/MSFileInputStream.java, revision 1.3

1.1       bmahe       1: // MSFileInputStream.java
1.3     ! ylafon      2: // $Id: MSFileInputStream.java,v 1.2 2000/08/16 21:37:58 ylafon Exp $
1.1       bmahe       3: // (c) COPYRIGHT MIT, INRIA and Keio, 2000.
                      4: // Please first read the full copyright statement in file COPYRIGHT.html
                      5: package org.w3c.util;
                      6: 
                      7: import java.io.File;
1.3     ! ylafon      8: import java.io.FileInputStream;
1.1       bmahe       9: import java.io.FileNotFoundException;
                     10: import java.io.FilterInputStream;
1.3     ! ylafon     11: import java.io.IOException;
1.1       bmahe      12: 
                     13: /**
1.3     ! ylafon     14:  * @author BenoοΏ½t MahοΏ½ (bmahe@w3.org)
        !            15:  * @version $Revision: 1.2 $
1.1       bmahe      16:  */
                     17: public class MSFileInputStream extends FilterInputStream {
1.2       ylafon     18: 
1.1       bmahe      19:     private File file = null;
                     20: 
                     21:     protected int readlimit = -1;
                     22: 
1.3     ! ylafon     23:     protected int count = 0;
1.1       bmahe      24:     protected int markpos = 0;
                     25: 
                     26:     /**
1.3     ! ylafon     27:      * Tests if this input stream supports the <code>mark</code>
        !            28:      * and <code>reset</code> methods. The <code>markSupported</code>
        !            29:      * method of <code>FilterInputStream</code> calls the
        !            30:      * <code>markSupported</code> method of its underlying input stream
        !            31:      * and returns whatever value that method returns.
1.1       bmahe      32:      *
1.3     ! ylafon     33:      * @return always true.
        !            34:      * @since JDK1.0
1.1       bmahe      35:      */
                     36:     public boolean markSupported() {
1.3     ! ylafon     37:         return true;
1.1       bmahe      38:     }
                     39: 
                     40:     /**
1.3     ! ylafon     41:      * Marks the current position in this input stream. A subsequent
        !            42:      * call to the <code>reset</code> method repositions this stream at
        !            43:      * the last marked position so that subsequent reads re-read the same
        !            44:      * bytes.
        !            45:      * <p/>
        !            46:      * The <code>readlimit</code> arguments tells this input stream to
        !            47:      * allow that many bytes to be read before the mark position gets
        !            48:      * invalidated.
        !            49:      * <p/>
        !            50:      *
        !            51:      * @param readlimit the maximum limit of bytes that can be read before
        !            52:      *                  the mark position becomes invalid.
        !            53:      * @see java.io.InputStream#reset()
        !            54:      * @since JDK1.0
1.1       bmahe      55:      */
                     56:     public synchronized void mark(int readlimit) {
1.3     ! ylafon     57:         this.readlimit = readlimit;
        !            58:         this.markpos = count;
1.1       bmahe      59:     }
                     60: 
                     61:     /**
1.3     ! ylafon     62:      * Repositions this stream to the position at the time the
        !            63:      * <code>mark</code> method was last called on this input stream.
        !            64:      * <p/>
1.1       bmahe      65:      * Stream marks are intended to be used in
                     66:      * situations where you need to read ahead a little to see what's in
                     67:      * the stream. Often this is most easily done by invoking some
                     68:      * general parser. If the stream is of the type handled by the
                     69:      * parser, it just chugs along happily. If the stream is not of
                     70:      * that type, the parser should toss an exception when it fails,
                     71:      * which, if it happens within readlimit bytes, allows the outer
                     72:      * code to reset the stream and try another parser.
                     73:      *
1.3     ! ylafon     74:      * @throws IOException if this stream has not been marked or if the
        !            75:      *                     mark has been invalidated.
        !            76:      * @see java.io.InputStream#mark(int)
        !            77:      * @see java.io.IOException
        !            78:      * @since JDK1.0
        !            79:      */
        !            80:     public synchronized void reset()
        !            81:             throws IOException {
        !            82:         if (markpos < 0) {
        !            83:             throw new IOException("Resetting to invalid mark");
        !            84:         }
        !            85:         if (count - markpos > readlimit) {
        !            86:             throw new IOException("Read limit reached, invalid mark");
        !            87:         }
        !            88:         in.close();
        !            89:         in = new FileInputStream(file);
        !            90:         if (markpos > 0) {
        !            91:             in.skip(markpos);
        !            92:         }
        !            93:         markpos = 0;
        !            94:         count = 0;
        !            95:     }
        !            96: 
        !            97:     /**
        !            98:      * Reads the next byte of data from this input stream. The value
        !            99:      * byte is returned as an <code>int</code> in the range
        !           100:      * <code>0</code> to <code>255</code>. If no byte is available
        !           101:      * because the end of the stream has been reached, the value
        !           102:      * <code>-1</code> is returned. This method blocks until input data
        !           103:      * is available, the end of the stream is detected, or an exception
        !           104:      * is thrown.
        !           105:      *
        !           106:      * @return the next byte of data, or <code>-1</code> if the end of the
        !           107:      *         stream is reached.
        !           108:      * @throws IOException if an I/O error occurs.
        !           109:      * @see java.io.FilterInputStream#in
        !           110:      * @since JDK1.0
        !           111:      */
        !           112:     public int read()
        !           113:             throws IOException {
        !           114:         int read = in.read();
        !           115:         if (read != -1) {
        !           116:             count++;
        !           117:         }
        !           118:         return read;
        !           119:     }
        !           120: 
        !           121:     /**
        !           122:      * Reads up to <code>byte.length</code> bytes of data from this
        !           123:      * input stream into an array of bytes. This method blocks until some
        !           124:      * input is available.
        !           125:      *
        !           126:      * @param b the buffer into which the data is read.
        !           127:      * @return the total number of bytes read into the buffer, or
        !           128:      *         <code>-1</code> if there is no more data because the end of
        !           129:      *         the stream has been reached.
        !           130:      * @throws IOException if an I/O error occurs.
        !           131:      * @see java.io.FilterInputStream#read(byte[], int, int)
        !           132:      * @since JDK1.0
        !           133:      */
        !           134:     public int read(byte b[])
        !           135:             throws IOException {
        !           136:         int read = in.read(b, 0, b.length);
        !           137:         if (read != -1) {
        !           138:             count += read;
        !           139:         }
        !           140:         return read;
        !           141:     }
        !           142: 
        !           143:     /**
        !           144:      * Reads up to <code>len</code> bytes of data from this input stream
        !           145:      * into an array of bytes. This method blocks until some input is
        !           146:      * available.
        !           147:      *
        !           148:      * @param b   the buffer into which the data is read.
        !           149:      * @param off the start offset of the data.
        !           150:      * @param len the maximum number of bytes read.
        !           151:      * @return the total number of bytes read into the buffer, or
        !           152:      *         <code>-1</code> if there is no more data because the end of
        !           153:      *         the stream has been reached.
        !           154:      * @throws IOException if an I/O error occurs.
        !           155:      * @see java.io.FilterInputStream#in
        !           156:      * @since JDK1.0
        !           157:      */
        !           158:     public int read(byte b[], int off, int len)
        !           159:             throws IOException {
        !           160:         int read = in.read(b, off, len);
        !           161:         if (read != -1) {
        !           162:             count += read;
        !           163:         }
        !           164:         return read;
1.1       bmahe     165:     }
                    166: 
                    167: 
                    168:     /**
                    169:      * Creates an input file stream to read from the specified file descriptor.
                    170:      *
1.3     ! ylafon    171:      * @param fdObj the file descriptor to be opened for reading.
        !           172:      * @throws SecurityException if a security manager exists, its
        !           173:      *                           <code>checkRead</code> method is called with the file
        !           174:      *                           descriptor to see if the application is allowed to read
        !           175:      *                           from the specified file descriptor.
        !           176:      * @see java.lang.SecurityManager#checkRead(java.io.FileDescriptor)
        !           177:      * @since JDK1.0
        !           178:      */
        !           179:     public MSFileInputStream(File file)
        !           180:             throws FileNotFoundException {
        !           181:         super(new FileInputStream(file));
        !           182:         this.file = file;
1.1       bmahe     183:     }
                    184: 
                    185:     /**
1.3     ! ylafon    186:      * Creates an input file stream to read from a file with the
        !           187:      * specified name.
        !           188:      *
        !           189:      * @param name the system-dependent file name.
        !           190:      * @throws FileNotFoundException if the file is not found.
        !           191:      * @throws SecurityException     if a security manager exists, its
        !           192:      *                               <code>checkRead</code> method is called with the name
        !           193:      *                               argument to see if the application is allowed read access
        !           194:      *                               to the file.
        !           195:      * @see java.lang.SecurityManager#checkRead(java.lang.String)
        !           196:      * @since JDK1.0
        !           197:      */
        !           198:     public MSFileInputStream(String name)
        !           199:             throws FileNotFoundException {
        !           200:         super(new FileInputStream(name));
        !           201:         this.file = new File(name);
1.1       bmahe     202:     }
                    203: 
                    204: }

Webmaster