org.apache.commons.vfs2.RandomAccessContent Java Examples

The following examples show how to use org.apache.commons.vfs2.RandomAccessContent. 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: DefaultFileContent.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an input/output stream to use to read and write the content of the file in an random manner.
 *
 * @param mode The RandomAccessMode.
 * @return A RandomAccessContent object to access the file.
 * @throws FileSystemException if an error occurs.
 */
@Override
public RandomAccessContent getRandomAccessContent(final RandomAccessMode mode) throws FileSystemException {
    /*
     * if (getThreadData().getState() != STATE_NONE) { throw new
     * FileSystemException("vfs.provider/read-in-use.error", file); }
     */

    // Get the content
    final RandomAccessContent rastr = fileObject.getRandomAccessContent(mode);

    final FileRandomAccessContent rac = new FileRandomAccessContent(fileObject, rastr);

    getOrCreateThreadData().addRastr(rac);
    streamOpened();

    return rac;
}
 
Example #2
Source File: SharedRandomContentInputStream.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream newStream(final long start, final long end) {
    try {
        final long newFileStart = this.fileStart + start;
        final long newFileEnd = end < 0 ? this.fileEnd : this.fileStart + end;

        final RandomAccessContent rac = fo.getContent().getRandomAccessContent(RandomAccessMode.READ);
        rac.seek(newFileStart);
        return new SharedRandomContentInputStream(createdStreams, fo, newFileStart, newFileEnd,
                rac.getInputStream());
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #3
Source File: DefaultFileContent.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Handles the end of random access.
 */
private void endRandomAccess(final RandomAccessContent rac) {
    final FileContentThreadData fileContentThreadData = threadLocal.get();
    if (fileContentThreadData != null) {
        fileContentThreadData.removeRastr(rac);
    }
    if (fileContentThreadData == null || !fileContentThreadData.hasStreams()) {
        // remove even when no value is set to remove key
        threadLocal.remove();
    }
    streamClosed();
}
 
Example #4
Source File: ProviderRandomSetLengthTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a file
 */
public void testRandomSetLength() throws Exception {
    FileObject file = null;
    try {
        file = this.createScratchFolder().resolveFile("random_write.txt");
        file.createFile();
        final String fileString = file.toString();
        final RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READWRITE);

        // Write long string
        ra.writeBytes(TEST_DATA);
        Assert.assertEquals(fileString, TEST_DATA.length(), ra.length());

        // Shrink to length 1
        ra.setLength(1);
        Assert.assertEquals(fileString, 1, ra.length());
        // now read 1
        ra.seek(0);
        Assert.assertEquals(fileString, TEST_DATA.charAt(0), ra.readByte());

        try {
            ra.readByte();
            Assert.fail("Expected " + Exception.class.getName());
        } catch (final IOException e) {
            // Expected
        }

        // Grow to length 2
        ra.setLength(2);
        Assert.assertEquals(fileString, 2, ra.length());
        // We have an undefined extra byte
        ra.seek(1);
        ra.readByte();

    } finally {
        if (file != null) {
            file.close();
        }
    }
}
 
Example #5
Source File: HdfsFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * @see org.apache.commons.vfs2.provider.AbstractFileObject#doGetRandomAccessContent
 *      (org.apache.commons.vfs2.util.RandomAccessMode)
 */
@Override
protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
    if (mode.equals(RandomAccessMode.READWRITE)) {
        throw new UnsupportedOperationException();
    }
    return new HdfsRandomAccessContent(this.path, this.hdfs);
}
 
Example #6
Source File: AbstractFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an input/output stream to use to read and write the content of the file in and random manner.
 *
 * @param mode The RandomAccessMode.
 * @return The RandomAccessContent.
 * @throws FileSystemException if an error occurs.
 */
public RandomAccessContent getRandomAccessContent(final RandomAccessMode mode) throws FileSystemException {
    /*
     * VFS-210 if (!getType().hasContent()) { throw new FileSystemException("vfs.provider/read-not-file.error",
     * name); }
     */

    if (mode.requestRead()) {
        if (!fileSystem.hasCapability(Capability.RANDOM_ACCESS_READ)) {
            throw new FileSystemException("vfs.provider/random-access-read-not-supported.error");
        }
        if (!isReadable()) {
            throw new FileSystemException("vfs.provider/read-not-readable.error", fileName);
        }
    }

    if (mode.requestWrite()) {
        if (!fileSystem.hasCapability(Capability.RANDOM_ACCESS_WRITE)) {
            throw new FileSystemException("vfs.provider/random-access-write-not-supported.error");
        }
        if (!isWriteable()) {
            throw new FileSystemException("vfs.provider/write-read-only.error", fileName);
        }
    }

    // Get the raw input stream
    try {
        return doGetRandomAccessContent(mode);
    } catch (final Exception exc) {
        throw new FileSystemException("vfs.provider/random-access.error", fileName, exc);
    }
}
 
Example #7
Source File: LoadingInfo.java    From otroslogviewer with Apache License 2.0 5 votes vote down vote up
public LoadingInfo(FileObject fileObject, boolean tailing, OpenMode openMode) throws IOException {
  this.fileObject = fileObject;
  this.tailing = tailing;
  friendlyUrl = fileObject.getName().getFriendlyURI();

  fileObject.refresh();
  InputStream inputStream = fileObject.getContent().getInputStream();
  byte[] probe = loadProbe(inputStream, 10000);
  gzipped = checkIfIsGzipped(probe, probe.length);
  inputStreamBufferedStart = gzipped ? ungzip(probe) : probe;

  if (openMode == FROM_START || gzipped) {
    PushbackInputStream pushBackInputStream = new PushbackInputStream(inputStream, probe.length + 1); // +1 to support empty files
    pushBackInputStream.unread(probe);
    observableInputStreamImpl = new ObservableInputStreamImpl(pushBackInputStream);
    contentInputStream = gzipped ? new GZIPInputStream(observableInputStreamImpl) : observableInputStreamImpl;
  } else {
    RandomAccessContent randomAccessContent = fileObject.getContent().getRandomAccessContent(READ);
    randomAccessContent.seek(randomAccessContent.length());
    observableInputStreamImpl = new ObservableInputStreamImpl(randomAccessContent.getInputStream());
    contentInputStream = observableInputStreamImpl;
  }

  if (fileObject.getType().hasContent()) {
    lastFileSize = fileObject.getContent().getSize();
  }
}
 
Example #8
Source File: Http4FileObject.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
    return new Http4RandomAccessContent<>(this, mode);
}
 
Example #9
Source File: RepositoryVfsFileContent.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public RandomAccessContent getRandomAccessContent( RandomAccessMode mode ) throws FileSystemException {
  throw new NotImplementedException( "Random access to file in repository is not possible" );
}
 
Example #10
Source File: ConnectionFileObject.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override public RandomAccessContent getRandomAccessContent( RandomAccessMode mode ) throws FileSystemException {
  return resolvedFileObject.getRandomAccessContent( mode );
}
 
Example #11
Source File: ProviderRandomReadWriteTests.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Writes a file
 */
public void testRandomWrite() throws Exception {
    FileObject file = null;
    try {
        file = createScratchFolder().resolveFile("random_write.txt");
        file.createFile();
        final RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READWRITE);

        // write first byte
        ra.writeByte(TEST_DATA.charAt(0));

        // start at pos 4
        ra.seek(3);
        ra.writeByte(TEST_DATA.charAt(3));
        ra.writeByte(TEST_DATA.charAt(4));

        // restart at pos 4 (but overwrite with different content)
        ra.seek(3);
        ra.writeByte(TEST_DATA.charAt(7));
        ra.writeByte(TEST_DATA.charAt(8));

        // advance to pos 11
        ra.seek(10);
        ra.writeByte(TEST_DATA.charAt(10));
        ra.writeByte(TEST_DATA.charAt(11));

        // now read
        ra.seek(0);
        assertEquals(ra.readByte(), TEST_DATA.charAt(0));

        ra.seek(3);
        assertEquals(ra.readByte(), TEST_DATA.charAt(7));
        assertEquals(ra.readByte(), TEST_DATA.charAt(8));

        ra.seek(10);
        assertEquals(ra.readByte(), TEST_DATA.charAt(10));
        assertEquals(ra.readByte(), TEST_DATA.charAt(11));
    } finally {
        if (file != null) {
            file.close();
        }
    }
}
 
Example #12
Source File: ProviderRandomReadTests.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Read a file
 */
public void testRandomRead() throws Exception {
    FileObject file = null;
    try {
        file = getReadFolder().resolveFile("file1.txt");
        final RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READ);

        // read first byte
        byte c = ra.readByte();
        assertEquals(TEST_DATA.charAt(0), c);
        assertEquals("fp", 1, ra.getFilePointer());

        // start at pos 4
        ra.seek(3);
        c = ra.readByte();
        assertEquals(TEST_DATA.charAt(3), c);
        assertEquals("fp", 4, ra.getFilePointer());

        c = ra.readByte();
        assertEquals(TEST_DATA.charAt(4), c);
        assertEquals("fp", 5, ra.getFilePointer());

        // restart at pos 4
        ra.seek(3);
        c = ra.readByte();
        assertEquals(TEST_DATA.charAt(3), c);
        assertEquals("fp", 4, ra.getFilePointer());

        c = ra.readByte();
        assertEquals(TEST_DATA.charAt(4), c);
        assertEquals("fp", 5, ra.getFilePointer());

        // advance to pos 11
        ra.seek(10);
        c = ra.readByte();
        assertEquals(TEST_DATA.charAt(10), c);
        assertEquals("fp", 11, ra.getFilePointer());

        c = ra.readByte();
        assertEquals(TEST_DATA.charAt(11), c);
        assertEquals("fp", 12, ra.getFilePointer());
    } finally {
        if (file != null) {
            file.close();
        }
    }
}
 
Example #13
Source File: MonitorRandomAccessContent.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
public MonitorRandomAccessContent(final RandomAccessContent content) {
    this.content = content;
}
 
Example #14
Source File: FileContentThreadData.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
public void removeRastr(final RandomAccessContent randomAccessContent) {
    this.randomAccessContentList.remove(randomAccessContent);
}
 
Example #15
Source File: FileContentThreadData.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
void addRastr(final RandomAccessContent randomAccessContent) {
    this.randomAccessContentList.add(randomAccessContent);
}
 
Example #16
Source File: HttpFileObject.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
    return new HttpRandomAccessContent<>(this, mode);
}
 
Example #17
Source File: SftpFileObject.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
    return new SftpRandomAccessContent(this, mode);
}
 
Example #18
Source File: FtpFileObject.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
    return new FtpRandomAccessContent(this, mode);
}
 
Example #19
Source File: Http5FileObject.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
    return new Http5RandomAccessContent<>(this, mode);
}
 
Example #20
Source File: RamFileObject.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
    return new RamFileRandomAccessContent(this, mode);
}
 
Example #21
Source File: LocalFile.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
    return new LocalFileRandomAccessContent(file, mode);
}
 
Example #22
Source File: DefaultFileContent.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
FileRandomAccessContent(final FileObject file, final RandomAccessContent content) {
    super(content);
    this.file = file;
}
 
Example #23
Source File: RACRandomAccessFile.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
public RACRandomAccessFile(final RandomAccessContent rac) throws IOException {
    this(createTempFile());
    this.rac = rac;
}
 
Example #24
Source File: SmbFileObject.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * random access
 */
@Override
protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
    return new SmbFileRandomAccessContent(file, mode);
}
 
Example #25
Source File: DelegateFileObject.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Creates access to the file for random i/o.
 *
 * @since 2.0
 */
@Override
protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
    return file.getContent().getRandomAccessContent(mode);
}
 
Example #26
Source File: AbstractFileObject.java    From commons-vfs with Apache License 2.0 2 votes vote down vote up
/**
 * Creates access to the file for random i/o. Is only called if {@link #doGetType} returns {@link FileType#FILE}.
 * <p>
 * It is guaranteed that there are no open output streams for this file when this method is called.
 * </p>
 *
 * @param mode The mode to access the file.
 * @return The RandomAccessContext.
 * @throws Exception if an error occurs.
 */
protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
    throw new FileSystemException("vfs.provider/random-access-not-supported.error");
}