< prev index next >

src/java.base/share/classes/java/io/BufferedInputStream.java

Print this page




 221         if (markpos < 0)
 222             pos = 0;            /* no mark: throw away the buffer */
 223         else if (pos >= buffer.length)  /* no room left in buffer */
 224             if (markpos > 0) {  /* can throw away early part of the buffer */
 225                 int sz = pos - markpos;
 226                 System.arraycopy(buffer, markpos, buffer, 0, sz);
 227                 pos = sz;
 228                 markpos = 0;
 229             } else if (buffer.length >= marklimit) {
 230                 markpos = -1;   /* buffer got too big, invalidate mark */
 231                 pos = 0;        /* drop buffer contents */
 232             } else if (buffer.length >= MAX_BUFFER_SIZE) {
 233                 throw new OutOfMemoryError("Required array size too large");
 234             } else {            /* grow buffer */
 235                 int nsz = (pos <= MAX_BUFFER_SIZE - pos) ?
 236                         pos * 2 : MAX_BUFFER_SIZE;
 237                 if (nsz > marklimit)
 238                     nsz = marklimit;
 239                 byte[] nbuf = new byte[nsz];
 240                 System.arraycopy(buffer, 0, nbuf, 0, pos);
 241                 if (!U.compareAndSetObject(this, BUF_OFFSET, buffer, nbuf)) {
 242                     // Can't replace buf if there was an async close.
 243                     // Note: This would need to be changed if fill()
 244                     // is ever made accessible to multiple threads.
 245                     // But for now, the only way CAS can fail is via close.
 246                     // assert buf == null;
 247                     throw new IOException("Stream closed");
 248                 }
 249                 buffer = nbuf;
 250             }
 251         count = pos;
 252         int n = getInIfOpen().read(buffer, pos, buffer.length - pos);
 253         if (n > 0)
 254             count = n + pos;
 255     }
 256 
 257     /**
 258      * See
 259      * the general contract of the <code>read</code>
 260      * method of <code>InputStream</code>.
 261      *


 465      *          the <code>mark</code> and <code>reset</code> methods.
 466      * @see     java.io.InputStream#mark(int)
 467      * @see     java.io.InputStream#reset()
 468      */
 469     public boolean markSupported() {
 470         return true;
 471     }
 472 
 473     /**
 474      * Closes this input stream and releases any system resources
 475      * associated with the stream.
 476      * Once the stream has been closed, further read(), available(), reset(),
 477      * or skip() invocations will throw an IOException.
 478      * Closing a previously closed stream has no effect.
 479      *
 480      * @exception  IOException  if an I/O error occurs.
 481      */
 482     public void close() throws IOException {
 483         byte[] buffer;
 484         while ( (buffer = buf) != null) {
 485             if (U.compareAndSetObject(this, BUF_OFFSET, buffer, null)) {
 486                 InputStream input = in;
 487                 in = null;
 488                 if (input != null)
 489                     input.close();
 490                 return;
 491             }
 492             // Else retry in case a new buf was CASed in fill()
 493         }
 494     }
 495 }


 221         if (markpos < 0)
 222             pos = 0;            /* no mark: throw away the buffer */
 223         else if (pos >= buffer.length)  /* no room left in buffer */
 224             if (markpos > 0) {  /* can throw away early part of the buffer */
 225                 int sz = pos - markpos;
 226                 System.arraycopy(buffer, markpos, buffer, 0, sz);
 227                 pos = sz;
 228                 markpos = 0;
 229             } else if (buffer.length >= marklimit) {
 230                 markpos = -1;   /* buffer got too big, invalidate mark */
 231                 pos = 0;        /* drop buffer contents */
 232             } else if (buffer.length >= MAX_BUFFER_SIZE) {
 233                 throw new OutOfMemoryError("Required array size too large");
 234             } else {            /* grow buffer */
 235                 int nsz = (pos <= MAX_BUFFER_SIZE - pos) ?
 236                         pos * 2 : MAX_BUFFER_SIZE;
 237                 if (nsz > marklimit)
 238                     nsz = marklimit;
 239                 byte[] nbuf = new byte[nsz];
 240                 System.arraycopy(buffer, 0, nbuf, 0, pos);
 241                 if (!U.compareAndSetReference(this, BUF_OFFSET, buffer, nbuf)) {
 242                     // Can't replace buf if there was an async close.
 243                     // Note: This would need to be changed if fill()
 244                     // is ever made accessible to multiple threads.
 245                     // But for now, the only way CAS can fail is via close.
 246                     // assert buf == null;
 247                     throw new IOException("Stream closed");
 248                 }
 249                 buffer = nbuf;
 250             }
 251         count = pos;
 252         int n = getInIfOpen().read(buffer, pos, buffer.length - pos);
 253         if (n > 0)
 254             count = n + pos;
 255     }
 256 
 257     /**
 258      * See
 259      * the general contract of the <code>read</code>
 260      * method of <code>InputStream</code>.
 261      *


 465      *          the <code>mark</code> and <code>reset</code> methods.
 466      * @see     java.io.InputStream#mark(int)
 467      * @see     java.io.InputStream#reset()
 468      */
 469     public boolean markSupported() {
 470         return true;
 471     }
 472 
 473     /**
 474      * Closes this input stream and releases any system resources
 475      * associated with the stream.
 476      * Once the stream has been closed, further read(), available(), reset(),
 477      * or skip() invocations will throw an IOException.
 478      * Closing a previously closed stream has no effect.
 479      *
 480      * @exception  IOException  if an I/O error occurs.
 481      */
 482     public void close() throws IOException {
 483         byte[] buffer;
 484         while ( (buffer = buf) != null) {
 485             if (U.compareAndSetReference(this, BUF_OFFSET, buffer, null)) {
 486                 InputStream input = in;
 487                 in = null;
 488                 if (input != null)
 489                     input.close();
 490                 return;
 491             }
 492             // Else retry in case a new buf was CASed in fill()
 493         }
 494     }
 495 }
< prev index next >