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();
}
}
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);
}
}
}
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;
}
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;
}
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;
}
}
Aggregations