Search in sources :

Example 1 with RandomAccessInputStream

use of com.mucommander.commons.io.RandomAccessInputStream in project mucommander by mucommander.

the class AbstractFile method getInputStream.

/**
 * Returns an <code>InputStream</code> to read this file's contents, starting at the specified offset (in bytes).
 * A <code>java.io.IOException</code> is thrown if the file doesn't exist.
 *
 * <p>This implementation starts by checking whether the {@link FileOperation#RANDOM_READ_FILE} operation is
 * supported or not.
 * If it is, a {@link #getRandomAccessInputStream() random input stream} to this file is retrieved and used to seek
 * to the specified offset. If it's not, a regular {@link #getInputStream() input stream} is retrieved, and
 * {@link java.io.InputStream#skip(long)} is used to position the stream to the specified offset, which on most
 * <code>InputStream</code> implementations is very slow as it causes the bytes to be read and discarded.
 * For this reason, file implementations that do not provide random read access may want to override this method
 * if a more efficient implementation can be provided.</p>
 *
 * @param offset the offset in bytes from the beginning of the file, must be >0
 * @throws IOException if this file cannot be read or is a folder.
 * @throws UnsupportedFileOperationException if this method relies on a file operation that is not supported
 * or not implemented by the underlying filesystem.
 * @return an <code>InputStream</code> to read this file's contents, skipping the specified number of bytes
 */
public InputStream getInputStream(long offset) throws IOException, UnsupportedFileOperationException {
    // Use a random access input stream when available
    if (isFileOperationSupported(FileOperation.RANDOM_READ_FILE)) {
        RandomAccessInputStream rais = getRandomAccessInputStream();
        rais.seek(offset);
        return rais;
    }
    InputStream in = getInputStream();
    // Skip exactly the specified number of bytes
    StreamUtils.skipFully(in, offset);
    return in;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) RandomAccessInputStream(com.mucommander.commons.io.RandomAccessInputStream) ChecksumInputStream(com.mucommander.commons.io.ChecksumInputStream) InputStream(java.io.InputStream) RandomAccessInputStream(com.mucommander.commons.io.RandomAccessInputStream)

Example 2 with RandomAccessInputStream

use of com.mucommander.commons.io.RandomAccessInputStream in project mucommander by mucommander.

the class IsoArchiveFile method getEntryInputStream.

@Override
public InputStream getEntryInputStream(ArchiveEntry entry, ArchiveEntryIterator entryIterator) throws IOException, UnsupportedFileOperationException {
    // Cast the entry before creating the stream, in case it fails
    IsoArchiveEntry isoEntry = (IsoArchiveEntry) entry;
    RandomAccessInputStream rais;
    // If a IsoEntryIterator is specified, reuse the iterator's stream
    if (entryIterator != null && entryIterator instanceof IsoEntryIterator) {
        // Override close() as a no-op so that the stream is re-used from one entry to another -- the stream will
        // be closed when the iterator is closed.
        rais = new FilterRandomAccessInputStream(((IsoEntryIterator) entryIterator).getRandomAccessInputStream()) {

            @Override
            public void close() throws IOException {
            // No-op
            }
        };
    } else {
        rais = getRandomAccessInputStream();
    }
    return new IsoEntryInputStream(rais, isoEntry);
}
Also used : FilterRandomAccessInputStream(com.mucommander.commons.io.FilterRandomAccessInputStream) FilterRandomAccessInputStream(com.mucommander.commons.io.FilterRandomAccessInputStream) RandomAccessInputStream(com.mucommander.commons.io.RandomAccessInputStream) IOException(java.io.IOException)

Example 3 with RandomAccessInputStream

use of com.mucommander.commons.io.RandomAccessInputStream in project mucommander by mucommander.

the class TextViewer method startEditing.

void startEditing(AbstractFile file, DocumentListener documentListener) throws IOException {
    // Auto-detect encoding
    // Get a RandomAccessInputStream on the file if possible, if not get a simple InputStream
    InputStream in = null;
    try {
        if (file.isFileOperationSupported(FileOperation.RANDOM_READ_FILE)) {
            try {
                in = file.getRandomAccessInputStream();
            } catch (IOException e) {
            // In that case we simply get an InputStream
            }
        }
        if (in == null) {
            in = file.getInputStream();
        }
        String encoding = EncodingDetector.detectEncoding(in);
        if (in instanceof RandomAccessInputStream) {
            // Seek to the beginning of the file and reuse the stream
            ((RandomAccessInputStream) in).seek(0);
        } else {
            // TODO: it would be more efficient to use some sort of PushBackInputStream, though we can't use PushBackInputStream because we don't want to keep pushing back for the whole InputStream lifetime
            // Close the InputStream and open a new one
            // Note: we could use mark/reset if the InputStream supports it, but it is almost never implemented by
            // InputStream subclasses and a broken by design anyway.
            in.close();
            in = file.getInputStream();
        }
        // Load the file into the text area
        loadDocument(in, encoding, documentListener);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            // Nothing to do here.
            }
        }
    }
}
Also used : RandomAccessInputStream(com.mucommander.commons.io.RandomAccessInputStream) BOMInputStream(com.mucommander.commons.io.bom.BOMInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) RandomAccessInputStream(com.mucommander.commons.io.RandomAccessInputStream)

Aggregations

RandomAccessInputStream (com.mucommander.commons.io.RandomAccessInputStream)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ChecksumInputStream (com.mucommander.commons.io.ChecksumInputStream)1 FilterRandomAccessInputStream (com.mucommander.commons.io.FilterRandomAccessInputStream)1 BOMInputStream (com.mucommander.commons.io.bom.BOMInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1