Java Code Examples for org.apache.commons.io.IOUtils#EOF

The following examples show how to use org.apache.commons.io.IOUtils#EOF . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CryptoInputStream.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
private int readNextChunk() throws IOException {
    final ByteBuffer ciphertextBuf = ByteBuffer.allocate(chunkSize);
    final int read = IOUtils.read(proxy, ciphertextBuf.array());
    if(read == 0) {
        return IOUtils.EOF;
    }
    ciphertextBuf.position(read);
    ciphertextBuf.flip();
    try {
        buffer = cryptor.decryptChunk(ciphertextBuf, chunkIndexOffset++, header, true);
    }
    catch(CryptoException e) {
        throw new IOException(e.getMessage(), new CryptoAuthenticationException(e.getMessage(), e));
    }
    return read;
}
 
Example 2
Source File: TripleCryptInputStream.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int read(final byte[] dst, final int off, final int len) throws IOException {
    final ByteBuffer dest = ByteBuffer.allocate(len);
    int remaining = len;
    while(remaining > 0) {
        final int location = len - remaining;
        final int count = this.read(dest, off + location, remaining);
        if(IOUtils.EOF == count) {
            if(remaining == len) {
                // nothing read before
                return IOUtils.EOF;
            }
            break;
        }
        dest.get(dst, off + location, count);
        remaining -= count;
    }
    return len - remaining;
}
 
Example 3
Source File: FileBuffer.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
@Override
public synchronized int read(final byte[] chunk, final Long offset) throws IOException {
    final RandomAccessFile file = random();
    if(offset < file.length()) {
        file.seek(offset);
        if(chunk.length + offset > file.length()) {
            return file.read(chunk, 0, (int) (file.length() - offset));
        }
        else {
            return file.read(chunk, 0, chunk.length);
        }
    }
    else {
        final NullInputStream nullStream = new NullInputStream(length);
        if(nullStream.available() > 0) {
            nullStream.skip(offset);
            return nullStream.read(chunk, 0, chunk.length);
        }
        else {
            return IOUtils.EOF;
        }
    }
}
 
Example 4
Source File: KeyInputStream.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Copies some or all bytes from a large (over 2GB) <code>InputStream</code>
 * to an <code>OutputStream</code>, optionally skipping input bytes.
 * <p>
 * Copy the method from IOUtils of commons-io to reimplement skip by seek
 * rather than read. The reason why IOUtils of commons-io implement skip
 * by read can be found at
 * <a href="https://issues.apache.org/jira/browse/IO-203">IO-203</a>.
 * </p>
 * <p>
 * This method uses the provided buffer, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * </p>
 *
 * @param output the <code>OutputStream</code> to write to
 * @param inputOffset : number of bytes to skip from input before copying
 * -ve values are ignored
 * @param length : number of bytes to copy. -ve means all
 * @param buffer the buffer to use for the copy
 * @return the number of bytes copied
 * @throws NullPointerException if the input or output is null
 * @throws IOException          if an I/O error occurs
 */
public long copyLarge(final OutputStream output,
    final long inputOffset, final long len, final byte[] buffer)
    throws IOException {
  if (inputOffset > 0) {
    seek(inputOffset);
  }

  if (len == 0) {
    return 0;
  }

  final int bufferLength = buffer.length;
  int bytesToRead = bufferLength;
  if (len > 0 && len < bufferLength) {
    bytesToRead = (int) len;
  }

  int read;
  long totalRead = 0;
  while (bytesToRead > 0) {
    read = read(buffer, 0, bytesToRead);
    if (read == IOUtils.EOF) {
      break;
    }

    output.write(buffer, 0, read);
    totalRead += read;
    if (len > 0) { // only adjust len if not reading to the end
      // Note the cast must work because buffer.length is an integer
      bytesToRead = (int) Math.min(len - totalRead, bufferLength);
    }
  }

  return totalRead;
}
 
Example 5
Source File: TripleCryptInputStream.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
private int readNextChunk() throws IOException {
    final ByteBuffer ciphertextBuf = ByteBuffer.allocate(SDSSession.DEFAULT_CHUNKSIZE);
    final int read = IOUtils.read(proxy, ciphertextBuf.array());
    if(lastread == 0) {
        return IOUtils.EOF;
    }
    ciphertextBuf.position(read);
    ciphertextBuf.flip();
    try {
        final PlainDataContainer pDataContainer;
        if(read == 0) {
            final PlainDataContainer c1 = cipher.processBytes(createEncryptedDataContainer(ciphertextBuf.array(), read, null));
            final PlainDataContainer c2 = cipher.doFinal(new EncryptedDataContainer(null, tag));
            pDataContainer = new PlainDataContainer(ArrayUtils.addAll(c1.getContent(), c2.getContent()));
        }
        else {
            pDataContainer = cipher.processBytes(createEncryptedDataContainer(ciphertextBuf.array(), read, null));
        }
        final byte[] content = pDataContainer.getContent();
        buffer = ByteBuffer.allocate(content.length);
        buffer.put(content);
        buffer.flip();
        lastread = read;
        return content.length;
    }
    catch(CryptoException e) {
        throw new IOException(e);
    }
}