src/share/classes/javax/crypto/CipherInputStream.java

Print this page
rev 10175 : 8047721: @since should have JDK version
Reviewed-by:


 153      */
 154     protected CipherInputStream(InputStream is) {
 155         super(is);
 156         input = is;
 157         cipher = new NullCipher();
 158     }
 159 
 160     /**
 161      * Reads the next byte of data from this input stream. The value
 162      * byte is returned as an <code>int</code> in the range
 163      * <code>0</code> to <code>255</code>. If no byte is available
 164      * because the end of the stream has been reached, the value
 165      * <code>-1</code> is returned. This method blocks until input data
 166      * is available, the end of the stream is detected, or an exception
 167      * is thrown.
 168      * <p>
 169      *
 170      * @return  the next byte of data, or <code>-1</code> if the end of the
 171      *          stream is reached.
 172      * @exception  IOException  if an I/O error occurs.
 173      * @since JCE1.2
 174      */
 175     public int read() throws IOException {
 176         if (ostart >= ofinish) {
 177             // we loop for new data as the spec says we are blocking
 178             int i = 0;
 179             while (i == 0) i = getMoreData();
 180             if (i == -1) return -1;
 181         }
 182         return ((int) obuffer[ostart++] & 0xff);
 183     };
 184 
 185     /**
 186      * Reads up to <code>b.length</code> bytes of data from this input
 187      * stream into an array of bytes.
 188      * <p>
 189      * The <code>read</code> method of <code>InputStream</code> calls
 190      * the <code>read</code> method of three arguments with the arguments
 191      * <code>b</code>, <code>0</code>, and <code>b.length</code>.
 192      *
 193      * @param      b   the buffer into which the data is read.
 194      * @return     the total number of bytes read into the buffer, or
 195      *             <code>-1</code> is there is no more data because the end of
 196      *             the stream has been reached.
 197      * @exception  IOException  if an I/O error occurs.
 198      * @see        java.io.InputStream#read(byte[], int, int)
 199      * @since      JCE1.2
 200      */
 201     public int read(byte b[]) throws IOException {
 202         return read(b, 0, b.length);
 203     }
 204 
 205     /**
 206      * Reads up to <code>len</code> bytes of data from this input stream
 207      * into an array of bytes. This method blocks until some input is
 208      * available. If the first argument is <code>null,</code> up to
 209      * <code>len</code> bytes are read and discarded.
 210      *
 211      * @param      b     the buffer into which the data is read.
 212      * @param      off   the start offset in the destination array
 213      *                   <code>buf</code>
 214      * @param      len   the maximum number of bytes read.
 215      * @return     the total number of bytes read into the buffer, or
 216      *             <code>-1</code> if there is no more data because the end of
 217      *             the stream has been reached.
 218      * @exception  IOException  if an I/O error occurs.
 219      * @see        java.io.InputStream#read()
 220      * @since      JCE1.2
 221      */
 222     public int read(byte b[], int off, int len) throws IOException {
 223         if (ostart >= ofinish) {
 224             // we loop for new data as the spec says we are blocking
 225             int i = 0;
 226             while (i == 0) i = getMoreData();
 227             if (i == -1) return -1;
 228         }
 229         if (len <= 0) {
 230             return 0;
 231         }
 232         int available = ofinish - ostart;
 233         if (len < available) available = len;
 234         if (b != null) {
 235             System.arraycopy(obuffer, ostart, b, off, available);
 236         }
 237         ostart = ostart + available;
 238         return available;
 239     }
 240 
 241     /**
 242      * Skips <code>n</code> bytes of input from the bytes that can be read
 243      * from this input stream without blocking.
 244      *
 245      * <p>Fewer bytes than requested might be skipped.
 246      * The actual number of bytes skipped is equal to <code>n</code> or
 247      * the result of a call to
 248      * {@link #available() available},
 249      * whichever is smaller.
 250      * If <code>n</code> is less than zero, no bytes are skipped.
 251      *
 252      * <p>The actual number of bytes skipped is returned.
 253      *
 254      * @param      n the number of bytes to be skipped.
 255      * @return     the actual number of bytes skipped.
 256      * @exception  IOException  if an I/O error occurs.
 257      * @since JCE1.2
 258      */
 259     public long skip(long n) throws IOException {
 260         int available = ofinish - ostart;
 261         if (n > available) {
 262             n = available;
 263         }
 264         if (n < 0) {
 265             return 0;
 266         }
 267         ostart += n;
 268         return n;
 269     }
 270 
 271     /**
 272      * Returns the number of bytes that can be read from this input
 273      * stream without blocking. The <code>available</code> method of
 274      * <code>InputStream</code> returns <code>0</code>. This method
 275      * <B>should</B> be overridden by subclasses.
 276      *
 277      * @return     the number of bytes that can be read from this input stream
 278      *             without blocking.
 279      * @exception  IOException  if an I/O error occurs.
 280      * @since      JCE1.2
 281      */
 282     public int available() throws IOException {
 283         return (ofinish - ostart);
 284     }
 285 
 286     /**
 287      * Closes this input stream and releases any system resources
 288      * associated with the stream.
 289      * <p>
 290      * The <code>close</code> method of <code>CipherInputStream</code>
 291      * calls the <code>close</code> method of its underlying input
 292      * stream.
 293      *
 294      * @exception  IOException  if an I/O error occurs.
 295      * @since JCE1.2
 296      */
 297     public void close() throws IOException {
 298         if (closed) {
 299             return;
 300         }
 301 
 302         closed = true;
 303         input.close();
 304         try {
 305             // throw away the unprocessed data
 306             if (!done) {
 307                 cipher.doFinal();
 308             }
 309         }
 310         catch (BadPaddingException | IllegalBlockSizeException ex) {
 311         }
 312         ostart = 0;
 313         ofinish = 0;
 314     }
 315 
 316     /**
 317      * Tests if this input stream supports the <code>mark</code>
 318      * and <code>reset</code> methods, which it does not.
 319      *
 320      * @return  <code>false</code>, since this class does not support the
 321      *          <code>mark</code> and <code>reset</code> methods.
 322      * @see     java.io.InputStream#mark(int)
 323      * @see     java.io.InputStream#reset()
 324      * @since   JCE1.2
 325      */
 326     public boolean markSupported() {
 327         return false;
 328     }
 329 }


 153      */
 154     protected CipherInputStream(InputStream is) {
 155         super(is);
 156         input = is;
 157         cipher = new NullCipher();
 158     }
 159 
 160     /**
 161      * Reads the next byte of data from this input stream. The value
 162      * byte is returned as an <code>int</code> in the range
 163      * <code>0</code> to <code>255</code>. If no byte is available
 164      * because the end of the stream has been reached, the value
 165      * <code>-1</code> is returned. This method blocks until input data
 166      * is available, the end of the stream is detected, or an exception
 167      * is thrown.
 168      * <p>
 169      *
 170      * @return  the next byte of data, or <code>-1</code> if the end of the
 171      *          stream is reached.
 172      * @exception  IOException  if an I/O error occurs.
 173      * @since 1.4, JCE1.2
 174      */
 175     public int read() throws IOException {
 176         if (ostart >= ofinish) {
 177             // we loop for new data as the spec says we are blocking
 178             int i = 0;
 179             while (i == 0) i = getMoreData();
 180             if (i == -1) return -1;
 181         }
 182         return ((int) obuffer[ostart++] & 0xff);
 183     };
 184 
 185     /**
 186      * Reads up to <code>b.length</code> bytes of data from this input
 187      * stream into an array of bytes.
 188      * <p>
 189      * The <code>read</code> method of <code>InputStream</code> calls
 190      * the <code>read</code> method of three arguments with the arguments
 191      * <code>b</code>, <code>0</code>, and <code>b.length</code>.
 192      *
 193      * @param      b   the buffer into which the data is read.
 194      * @return     the total number of bytes read into the buffer, or
 195      *             <code>-1</code> is there is no more data because the end of
 196      *             the stream has been reached.
 197      * @exception  IOException  if an I/O error occurs.
 198      * @see        java.io.InputStream#read(byte[], int, int)
 199      * @since      1.4, JCE1.2
 200      */
 201     public int read(byte b[]) throws IOException {
 202         return read(b, 0, b.length);
 203     }
 204 
 205     /**
 206      * Reads up to <code>len</code> bytes of data from this input stream
 207      * into an array of bytes. This method blocks until some input is
 208      * available. If the first argument is <code>null,</code> up to
 209      * <code>len</code> bytes are read and discarded.
 210      *
 211      * @param      b     the buffer into which the data is read.
 212      * @param      off   the start offset in the destination array
 213      *                   <code>buf</code>
 214      * @param      len   the maximum number of bytes read.
 215      * @return     the total number of bytes read into the buffer, or
 216      *             <code>-1</code> if there is no more data because the end of
 217      *             the stream has been reached.
 218      * @exception  IOException  if an I/O error occurs.
 219      * @see        java.io.InputStream#read()
 220      * @since      1.4, JCE1.2
 221      */
 222     public int read(byte b[], int off, int len) throws IOException {
 223         if (ostart >= ofinish) {
 224             // we loop for new data as the spec says we are blocking
 225             int i = 0;
 226             while (i == 0) i = getMoreData();
 227             if (i == -1) return -1;
 228         }
 229         if (len <= 0) {
 230             return 0;
 231         }
 232         int available = ofinish - ostart;
 233         if (len < available) available = len;
 234         if (b != null) {
 235             System.arraycopy(obuffer, ostart, b, off, available);
 236         }
 237         ostart = ostart + available;
 238         return available;
 239     }
 240 
 241     /**
 242      * Skips <code>n</code> bytes of input from the bytes that can be read
 243      * from this input stream without blocking.
 244      *
 245      * <p>Fewer bytes than requested might be skipped.
 246      * The actual number of bytes skipped is equal to <code>n</code> or
 247      * the result of a call to
 248      * {@link #available() available},
 249      * whichever is smaller.
 250      * If <code>n</code> is less than zero, no bytes are skipped.
 251      *
 252      * <p>The actual number of bytes skipped is returned.
 253      *
 254      * @param      n the number of bytes to be skipped.
 255      * @return     the actual number of bytes skipped.
 256      * @exception  IOException  if an I/O error occurs.
 257      * @since 1.4, JCE1.2
 258      */
 259     public long skip(long n) throws IOException {
 260         int available = ofinish - ostart;
 261         if (n > available) {
 262             n = available;
 263         }
 264         if (n < 0) {
 265             return 0;
 266         }
 267         ostart += n;
 268         return n;
 269     }
 270 
 271     /**
 272      * Returns the number of bytes that can be read from this input
 273      * stream without blocking. The <code>available</code> method of
 274      * <code>InputStream</code> returns <code>0</code>. This method
 275      * <B>should</B> be overridden by subclasses.
 276      *
 277      * @return     the number of bytes that can be read from this input stream
 278      *             without blocking.
 279      * @exception  IOException  if an I/O error occurs.
 280      * @since      1.4, JCE1.2
 281      */
 282     public int available() throws IOException {
 283         return (ofinish - ostart);
 284     }
 285 
 286     /**
 287      * Closes this input stream and releases any system resources
 288      * associated with the stream.
 289      * <p>
 290      * The <code>close</code> method of <code>CipherInputStream</code>
 291      * calls the <code>close</code> method of its underlying input
 292      * stream.
 293      *
 294      * @exception  IOException  if an I/O error occurs.
 295      * @since 1.4, JCE1.2
 296      */
 297     public void close() throws IOException {
 298         if (closed) {
 299             return;
 300         }
 301 
 302         closed = true;
 303         input.close();
 304         try {
 305             // throw away the unprocessed data
 306             if (!done) {
 307                 cipher.doFinal();
 308             }
 309         }
 310         catch (BadPaddingException | IllegalBlockSizeException ex) {
 311         }
 312         ostart = 0;
 313         ofinish = 0;
 314     }
 315 
 316     /**
 317      * Tests if this input stream supports the <code>mark</code>
 318      * and <code>reset</code> methods, which it does not.
 319      *
 320      * @return  <code>false</code>, since this class does not support the
 321      *          <code>mark</code> and <code>reset</code> methods.
 322      * @see     java.io.InputStream#mark(int)
 323      * @see     java.io.InputStream#reset()
 324      * @since   1.4, JCE1.2
 325      */
 326     public boolean markSupported() {
 327         return false;
 328     }
 329 }