Пример #1
0
  /**
   * Read next chunk from the response stream and convert it to a Java instance using the {@link
   * #getChunkType() chunk media type}. The method returns {@code null} if the underlying entity
   * input stream has been closed (either implicitly or explicitly by calling the {@link #close()}
   * method).
   *
   * <p>Note: Access to internal chunk parser is not a thread-safe operation and has to be
   * explicitly synchronized in case the chunked input is used from multiple threads.
   *
   * @return next streamed chunk or {@code null} if the underlying entity input stream has been
   *     closed while reading next chunk data.
   * @throws IllegalStateException in case this chunked input has been closed.
   */
  @SuppressWarnings("unchecked")
  public T read() throws IllegalStateException {
    if (closed.get()) {
      throw new IllegalStateException(LocalizationMessages.CHUNKED_INPUT_CLOSED());
    }

    try {
      final byte[] chunk = parser.readChunk(inputStream);
      if (chunk == null) {
        close();
      } else {
        ByteArrayInputStream chunkStream = new ByteArrayInputStream(chunk);
        //noinspection unchecked
        // TODO: add interceptors: interceptors are used in ChunkedOutput, so the stream should
        // be intercepted in the ChunkedInput too. Interceptors cannot be easily added to the
        // readFrom
        // method as they should wrap the stream before it is processed by ChunkParser. Also please
        // check todo
        // in ChunkedInput (this should be fixed together with this todo)
        // issue: JERSEY-1809
        return (T)
            messageBodyWorkers.readFrom(
                getRawType(),
                getType(),
                annotations,
                mediaType,
                headers,
                propertiesDelegate,
                chunkStream,
                Collections.<ReaderInterceptor>emptyList(),
                false);
      }
    } catch (IOException e) {
      Logger.getLogger(this.getClass().getName()).log(Level.FINE, e.getMessage(), e);
      close();
    }
    return null;
  }