Esempio n. 1
0
    @Override
    public int read(byte[] bytes, int off, int len) throws IOException {
      if (bytes == null) {
        throw new NullPointerException("null destination buffer");
      }
      if ((len | off | (off + len) | (bytes.length - (off + len))) < 0) {
        throw new IndexOutOfBoundsException();
      }
      if (len == 0) {
        return 0;
      }

      try {
        synchronized (stream) {
          final int available = stream.bufferedInputBytesRemaining();
          if (available >= len) {
            return stream.copyBufferedBytes(bytes, off, len);
          } else if (stream.getDescriptor().getChannel() instanceof SelectableChannel) {
            SelectableChannel ch = (SelectableChannel) stream.getDescriptor().getChannel();
            synchronized (ch.blockingLock()) {
              boolean oldBlocking = ch.isBlocking();
              try {
                if (!oldBlocking) {
                  ch.configureBlocking(true);
                }
                return stream.bufferedRead(ByteBuffer.wrap(bytes, off, len), true);
              } finally {
                if (!oldBlocking) {
                  ch.configureBlocking(oldBlocking);
                }
              }
            }
          } else {
            return stream.bufferedRead(ByteBuffer.wrap(bytes, off, len), true);
          }
        }
      } catch (BadDescriptorException ex) {
        throw new IOException(ex.getMessage());
      } catch (EOFException ex) {
        return -1;
      }
    }
Esempio n. 2
0
 @Override
 public int available() throws IOException {
   synchronized (stream) {
     return !stream.eof ? stream.bufferedInputBytesRemaining() : 0;
   }
 }