use of alluxio.client.file.FileInStream in project alluxio by Alluxio.
the class TailCommand method runCommand.
@Override
protected void runCommand(AlluxioURI path, CommandLine cl) throws AlluxioException, IOException {
URIStatus status = mFileSystem.getStatus(path);
int numOfBytes = Constants.KB;
if (cl.hasOption('c')) {
numOfBytes = Integer.parseInt(cl.getOptionValue('c'));
Preconditions.checkArgument(numOfBytes > 0, "specified bytes must be > 0");
}
if (status.isFolder()) {
throw new IOException(ExceptionMessage.PATH_MUST_BE_FILE.getMessage(path));
}
OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
try (FileInStream is = mFileSystem.openFile(path, options)) {
byte[] buf = new byte[numOfBytes];
long bytesToRead;
if (status.getLength() > numOfBytes) {
bytesToRead = numOfBytes;
} else {
bytesToRead = status.getLength();
}
is.skip(status.getLength() - bytesToRead);
int read = is.read(buf);
if (read != -1) {
System.out.write(buf, 0, read);
}
}
}
use of alluxio.client.file.FileInStream in project alluxio by Alluxio.
the class FileInStreamIntegrationTest method seekExceptionTest1.
/**
* Tests {@link FileInStream#seek(long)}. Validate the expected exception for seeking a negative
* position.
*/
@Test
public void seekExceptionTest1() throws Exception {
mThrown.expect(IllegalArgumentException.class);
for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) {
for (CreateFileOptions op : getOptionSet()) {
String filename = sTestPath + "/file_" + k + "_" + op.hashCode();
AlluxioURI uri = new AlluxioURI(filename);
try (FileInStream is = sFileSystem.openFile(uri, FileSystemTestUtils.toOpenFileOptions(op))) {
is.seek(-1);
}
}
}
}
use of alluxio.client.file.FileInStream in project alluxio by Alluxio.
the class FileInStreamIntegrationTest method seekExceptionTest2.
/**
* Tests {@link FileInStream#seek(long)}. Validate the expected exception for seeking a position
* that is past EOF.
*/
@Test
public void seekExceptionTest2() throws Exception {
mThrown.expect(IllegalArgumentException.class);
for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) {
for (CreateFileOptions op : getOptionSet()) {
String filename = sTestPath + "/file_" + k + "_" + op.hashCode();
AlluxioURI uri = new AlluxioURI(filename);
try (FileInStream is = sFileSystem.openFile(uri, FileSystemTestUtils.toOpenFileOptions(op))) {
is.seek(k + 1);
}
}
}
}
use of alluxio.client.file.FileInStream in project alluxio by Alluxio.
the class IsolatedFileSystemIntegrationTest method unlockBlockTest2.
@Test
public void unlockBlockTest2() throws Exception {
String uniqPath = PathUtils.uniqPath();
FileInStream is;
ByteBuffer buf;
int numOfFiles = 5;
int fileSize = WORKER_CAPACITY_BYTES / numOfFiles;
List<AlluxioURI> files = new ArrayList<>();
for (int k = 0; k < numOfFiles; k++) {
FileSystemTestUtils.createByteFile(mFileSystem, uniqPath + k, fileSize, mWriteBoth);
files.add(new AlluxioURI(uniqPath + k));
}
for (int k = 0; k < numOfFiles; k++) {
URIStatus info = mFileSystem.getStatus(files.get(k));
Assert.assertTrue(info.getInMemoryPercentage() == 100);
is = mFileSystem.openFile(files.get(k), FileSystemTestUtils.toOpenFileOptions(mWriteBoth));
buf = ByteBuffer.allocate((int) info.getBlockSizeBytes());
Assert.assertTrue(is.read(buf.array()) != -1);
is.seek(0);
buf.clear();
Assert.assertTrue(is.read(buf.array()) != -1);
is.close();
}
FileSystemTestUtils.createByteFile(mFileSystem, uniqPath + numOfFiles, fileSize, mWriteBoth);
files.add(new AlluxioURI(uniqPath + numOfFiles));
for (int k = 1; k < numOfFiles; k++) {
URIStatus info = mFileSystem.getStatus(files.get(k));
Assert.assertTrue(info.getInMemoryPercentage() == 100);
}
HeartbeatScheduler.execute(HeartbeatContext.WORKER_BLOCK_SYNC);
URIStatus info = mFileSystem.getStatus(files.get(numOfFiles));
Assert.assertTrue(info.getInMemoryPercentage() == 100);
}
use of alluxio.client.file.FileInStream in project alluxio by Alluxio.
the class LocalBlockInStreamIntegrationTest method readTest1.
/**
* Tests {@link alluxio.client.block.LocalBlockInStream#read()}.
*/
@Test
public void readTest1() 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];
int value = is.read();
int cnt = 0;
while (value != -1) {
Assert.assertTrue(value >= 0);
Assert.assertTrue(value < 256);
ret[cnt++] = (byte) value;
value = is.read();
}
Assert.assertEquals(cnt, k);
Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
is.close();
is = sFileSystem.openFile(uri, sReadCachePromote);
ret = new byte[k];
value = is.read();
cnt = 0;
while (value != -1) {
Assert.assertTrue(value >= 0);
Assert.assertTrue(value < 256);
ret[cnt++] = (byte) value;
value = is.read();
}
Assert.assertEquals(cnt, k);
Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
is.close();
Assert.assertTrue(sFileSystem.getStatus(uri).getInMemoryPercentage() == 100);
}
}
}
Aggregations