Search in sources :

Example 66 with FileInStream

use of alluxio.client.file.FileInStream in project alluxio by Alluxio.

the class CpCommand method copyFileToLocal.

/**
 * Copies a file specified by argv from the filesystem to the local filesystem. This is the
 * utility function.
 *
 * @param srcPath The source {@link AlluxioURI} (has to be a file)
 * @param dstPath The {@link AlluxioURI} of the destination in the local filesystem
 */
private void copyFileToLocal(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException {
    File dstFile = new File(dstPath.getPath());
    String randomSuffix = String.format(".%s_copyToLocal_", RandomStringUtils.randomAlphanumeric(8));
    File outputFile;
    if (dstFile.isDirectory()) {
        outputFile = new File(PathUtils.concatPath(dstFile.getAbsolutePath(), srcPath.getName()));
    } else {
        outputFile = dstFile;
    }
    File tmpDst = new File(outputFile.getPath() + randomSuffix);
    try (Closer closer = Closer.create()) {
        FileInStream is = closer.register(mFileSystem.openFile(srcPath));
        FileOutputStream out = closer.register(new FileOutputStream(tmpDst));
        byte[] buf = new byte[mCopyToLocalBufferSize];
        int t = is.read(buf);
        while (t != -1) {
            out.write(buf, 0, t);
            t = is.read(buf);
        }
        if (!tmpDst.renameTo(outputFile)) {
            throw new IOException("Failed to rename " + tmpDst.getPath() + " to destination " + outputFile.getPath());
        }
        System.out.println("Copied " + srcPath + " to " + "file://" + outputFile.getPath());
    } finally {
        tmpDst.delete();
    }
}
Also used : Closer(com.google.common.io.Closer) FileOutputStream(java.io.FileOutputStream) FileInStream(alluxio.client.file.FileInStream) IOException(java.io.IOException) File(java.io.File)

Example 67 with FileInStream

use of alluxio.client.file.FileInStream in project alluxio by Alluxio.

the class CatCommand method runPlainPath.

@Override
protected void runPlainPath(AlluxioURI path, CommandLine cl) throws AlluxioException, IOException {
    URIStatus status = mFileSystem.getStatus(path);
    if (status.isFolder()) {
        throw new FileDoesNotExistException(ExceptionMessage.PATH_MUST_BE_FILE.getMessage(path));
    }
    byte[] buf = new byte[Constants.MB];
    try (FileInStream is = mFileSystem.openFile(path)) {
        int read = is.read(buf);
        while (read != -1) {
            System.out.write(buf, 0, read);
            read = is.read(buf);
        }
    }
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) FileInStream(alluxio.client.file.FileInStream) URIStatus(alluxio.client.file.URIStatus)

Example 68 with FileInStream

use of alluxio.client.file.FileInStream in project alluxio by Alluxio.

the class BasicCheckpoint method readFile.

private boolean readFile(FileSystem fs) throws IOException, AlluxioException {
    boolean pass = true;
    for (int i = 0; i < mNumFiles; i++) {
        AlluxioURI filePath = new AlluxioURI(mFileFolder + "/part-" + i);
        LOG.debug("Reading data from {}", filePath);
        FileInStream is = fs.openFile(filePath);
        URIStatus status = fs.getStatus(filePath);
        ByteBuffer buf = ByteBuffer.allocate((int) status.getBlockSizeBytes());
        is.read(buf.array());
        buf.order(ByteOrder.nativeOrder());
        for (int k = 0; k < mNumFiles; k++) {
            pass = pass && (buf.getInt() == k);
        }
        is.close();
    }
    return pass;
}
Also used : FileInStream(alluxio.client.file.FileInStream) URIStatus(alluxio.client.file.URIStatus) ByteBuffer(java.nio.ByteBuffer) AlluxioURI(alluxio.AlluxioURI)

Example 69 with FileInStream

use of alluxio.client.file.FileInStream in project alluxio by Alluxio.

the class BasicOperations method readFile.

private boolean readFile(FileSystem fileSystem) throws IOException, AlluxioException {
    boolean pass = true;
    LOG.debug("Reading data...");
    final long startTimeMs = CommonUtils.getCurrentMs();
    FileInStream is = fileSystem.openFile(mFilePath, mReadOptions);
    ByteBuffer buf = ByteBuffer.allocate((int) is.remaining());
    is.read(buf.array());
    buf.order(ByteOrder.nativeOrder());
    for (int k = 0; k < NUMBERS; k++) {
        pass = pass && (buf.getInt() == k);
    }
    is.close();
    LOG.info(FormatUtils.formatTimeTakenMs(startTimeMs, "readFile file " + mFilePath));
    return pass;
}
Also used : FileInStream(alluxio.client.file.FileInStream) ByteBuffer(java.nio.ByteBuffer)

Example 70 with FileInStream

use of alluxio.client.file.FileInStream in project alluxio by Alluxio.

the class AbstractFileSystemShellTest method readContent.

/**
 * Reads content from the file that the uri points to.
 *
 * @param uri the path of the file to read
 * @param length the length of content to read
 * @return the content that has been read
 */
protected byte[] readContent(AlluxioURI uri, int length) throws IOException, AlluxioException {
    try (FileInStream tfis = sFileSystem.openFile(uri, OpenFilePOptions.newBuilder().setReadType(ReadPType.NO_CACHE).build())) {
        byte[] read = new byte[length];
        tfis.read(read);
        return read;
    }
}
Also used : FileInStream(alluxio.client.file.FileInStream)

Aggregations

FileInStream (alluxio.client.file.FileInStream)179 AlluxioURI (alluxio.AlluxioURI)148 Test (org.junit.Test)132 BaseIntegrationTest (alluxio.testutils.BaseIntegrationTest)67 URIStatus (alluxio.client.file.URIStatus)44 CreateFilePOptions (alluxio.grpc.CreateFilePOptions)31 FileOutStream (alluxio.client.file.FileOutStream)25 CreateFileOptions (alluxio.client.file.options.CreateFileOptions)21 FileSystem (alluxio.client.file.FileSystem)17 ByteBuffer (java.nio.ByteBuffer)16 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)11 File (java.io.File)11 OpenFileOptions (alluxio.client.file.options.OpenFileOptions)10 FileAlreadyExistsException (alluxio.exception.FileAlreadyExistsException)8 FileInfo (alluxio.wire.FileInfo)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 InstancedConfiguration (alluxio.conf.InstancedConfiguration)5 FileIncompleteException (alluxio.exception.FileIncompleteException)5