use of alluxio.client.file.FileInStream in project alluxio by Alluxio.
the class LocalBlockInStreamIntegrationTest method skip.
/**
* Tests {@link alluxio.client.block.LocalBlockInStream#skip(long)}.
*/
@Test
public void skip() throws Exception {
for (int k = MIN_LEN + DELTA; k <= MAX_LEN; k += DELTA) {
for (CreateFileOptions op : getOptionSet()) {
AlluxioURI uri = new AlluxioURI(sTestPath + "/file_" + k + "_" + op.hashCode());
FileInStream is = sFileSystem.openFile(uri, sReadNoCache);
Assert.assertEquals(k / 2, is.skip(k / 2));
Assert.assertEquals(k / 2, is.read());
is.close();
is = sFileSystem.openFile(uri, sReadCachePromote);
int t = k / 3;
Assert.assertEquals(t, is.skip(t));
Assert.assertEquals(t, is.read());
Assert.assertEquals(t, is.skip(t));
Assert.assertEquals(2 * t + 1, is.read());
is.close();
Assert.assertTrue(sFileSystem.getStatus(uri).getInMemoryPercentage() == 100);
}
}
}
use of alluxio.client.file.FileInStream in project alluxio by Alluxio.
the class LocalBlockInStreamIntegrationTest method readTest3.
/**
* Tests {@link alluxio.client.block.LocalBlockInStream#read(byte[], int, int)}.
*/
@Test
public void readTest3() throws Exception {
for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) {
for (CreateFileOptions op : getOptionSet()) {
AlluxioURI uri = new AlluxioURI(sTestPath + "/file_" + k + "_" + op.hashCode());
FileInStream is = sFileSystem.openFile(uri, sReadNoCache);
byte[] ret = new byte[k / 2];
Assert.assertEquals(k / 2, is.read(ret, 0, k / 2));
Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k / 2, ret));
is.close();
is = sFileSystem.openFile(uri, sReadCachePromote);
ret = new byte[k];
Assert.assertEquals(k, is.read(ret, 0, k));
Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
is.close();
Assert.assertTrue(sFileSystem.getStatus(uri).getInMemoryPercentage() == 100);
}
}
}
use of alluxio.client.file.FileInStream in project alluxio by Alluxio.
the class LocalBlockInStreamIntegrationTest method seekExceptionTest2.
/**
* Tests {@link alluxio.client.block.LocalBlockInStream#seek(long)}. Validate the expected
* exception for seeking a position that is past buffer limit.
*/
@Test
public void seekExceptionTest2() throws Exception {
mThrown.expect(IllegalArgumentException.class);
mThrown.expectMessage(String.format(PreconditionMessage.ERR_SEEK_PAST_END_OF_FILE.toString(), 1));
for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) {
for (CreateFileOptions op : getOptionSet()) {
AlluxioURI uri = new AlluxioURI(sTestPath + "/file_" + k + "_" + op.hashCode());
try (FileInStream is = sFileSystem.openFile(uri, sReadNoCache)) {
is.seek(k + 1);
}
}
}
}
use of alluxio.client.file.FileInStream in project alluxio by Alluxio.
the class LocalBlockInStreamIntegrationTest method readTest2.
/**
* Tests {@link alluxio.client.block.LocalBlockInStream#read(byte[])}.
*/
@Test
public void readTest2() throws Exception {
for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) {
for (CreateFileOptions op : getOptionSet()) {
AlluxioURI uri = new AlluxioURI(sTestPath + "/file_" + k + "_" + op.hashCode());
FileInStream is = sFileSystem.openFile(uri, sReadNoCache);
byte[] ret = new byte[k];
Assert.assertEquals(k, is.read(ret));
Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
is.close();
is = sFileSystem.openFile(uri, sReadCachePromote);
ret = new byte[k];
Assert.assertEquals(k, is.read(ret));
Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
is.close();
Assert.assertTrue(sFileSystem.getStatus(uri).getInMemoryPercentage() == 100);
}
}
}
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
* @throws AlluxioException when Alluxio exception occurs
* @throws IOException when non-Alluxio exception occurs
*/
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 tmpDst = new File(dstFile.getAbsolutePath() + randomSuffix);
try (Closer closer = Closer.create()) {
OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
FileInStream is = closer.register(mFileSystem.openFile(srcPath, options));
FileOutputStream out = closer.register(new FileOutputStream(tmpDst));
byte[] buf = new byte[64 * Constants.MB];
int t = is.read(buf);
while (t != -1) {
out.write(buf, 0, t);
t = is.read(buf);
}
if (!tmpDst.renameTo(dstFile)) {
throw new IOException("Failed to rename " + tmpDst.getPath() + " to destination " + dstPath);
}
System.out.println("Copied " + srcPath + " to " + dstPath);
} finally {
tmpDst.delete();
}
}
Aggregations