< prev index next >

src/java.net.http/share/classes/jdk/internal/net/http/ResponseSubscribers.java

Print this page

 521         public int read() throws IOException {
 522             ByteBuffer buffer;
 523             if ((buffer = current()) == LAST_BUFFER) return -1;
 524             return buffer.get() & 0xFF;
 525         }
 526 
 527         @Override
 528         public int available() throws IOException {
 529             // best effort: returns the number of remaining bytes in
 530             // the current buffer if any, or 1 if the current buffer
 531             // is null or empty but the queue or current buffer list
 532             // are not empty. Returns 0 otherwise.
 533             if (closed) return 0;
 534             int available = 0;
 535             ByteBuffer current = currentBuffer;
 536             if (current == LAST_BUFFER) return 0;
 537             if (current != null) available = current.remaining();
 538             if (available != 0) return available;
 539             Iterator<?> iterator = currentListItr;
 540             if (iterator != null && iterator.hasNext()) return 1;
 541             if (buffers.isEmpty()) return 0;
 542             return 1;
 543         }
 544 
 545         @Override
 546         public void onSubscribe(Flow.Subscription s) {
 547             Objects.requireNonNull(s);
 548             if (debug.on()) debug.log("onSubscribe called");
 549             try {
 550                 if (!subscribed.compareAndSet(false, true)) {
 551                     if (debug.on()) debug.log("Already subscribed: canceling");
 552                     s.cancel();
 553                 } else {
 554                     // check whether the stream is already closed.
 555                     // if so, we should cancel the subscription
 556                     // immediately.
 557                     boolean closed;
 558                     synchronized (this) {
 559                         closed = this.closed;
 560                         if (!closed) {
 561                             this.subscription = s;
 562                             // should contain at least 2, unless closed or failed.

 521         public int read() throws IOException {
 522             ByteBuffer buffer;
 523             if ((buffer = current()) == LAST_BUFFER) return -1;
 524             return buffer.get() & 0xFF;
 525         }
 526 
 527         @Override
 528         public int available() throws IOException {
 529             // best effort: returns the number of remaining bytes in
 530             // the current buffer if any, or 1 if the current buffer
 531             // is null or empty but the queue or current buffer list
 532             // are not empty. Returns 0 otherwise.
 533             if (closed) return 0;
 534             int available = 0;
 535             ByteBuffer current = currentBuffer;
 536             if (current == LAST_BUFFER) return 0;
 537             if (current != null) available = current.remaining();
 538             if (available != 0) return available;
 539             Iterator<?> iterator = currentListItr;
 540             if (iterator != null && iterator.hasNext()) return 1;
 541             if (!buffers.isEmpty() && buffers.peek() != LAST_LIST ) return 1;
 542             return available;
 543         }
 544 
 545         @Override
 546         public void onSubscribe(Flow.Subscription s) {
 547             Objects.requireNonNull(s);
 548             if (debug.on()) debug.log("onSubscribe called");
 549             try {
 550                 if (!subscribed.compareAndSet(false, true)) {
 551                     if (debug.on()) debug.log("Already subscribed: canceling");
 552                     s.cancel();
 553                 } else {
 554                     // check whether the stream is already closed.
 555                     // if so, we should cancel the subscription
 556                     // immediately.
 557                     boolean closed;
 558                     synchronized (this) {
 559                         closed = this.closed;
 560                         if (!closed) {
 561                             this.subscription = s;
 562                             // should contain at least 2, unless closed or failed.
< prev index next >