use of java.nio.channels.ReadableByteChannel in project kafka by apache.
the class RemoteLogMetadataSnapshotFile method read.
/**
* @return the Snapshot if it exists.
* @throws IOException if there is any error in reading the stored snapshot.
*/
public synchronized Optional<Snapshot> read() throws IOException {
// Checking for empty files.
if (metadataStoreFile.length() == 0) {
return Optional.empty();
}
try (ReadableByteChannel channel = Channels.newChannel(new FileInputStream(metadataStoreFile))) {
// header: <version:short><metadata-partition:int><metadata-partition-offset:long>
// Read header
ByteBuffer headerBuffer = ByteBuffer.allocate(HEADER_SIZE);
channel.read(headerBuffer);
headerBuffer.rewind();
short version = headerBuffer.getShort();
int metadataPartition = headerBuffer.getInt();
long metadataPartitionOffset = headerBuffer.getLong();
int metadataSnapshotsSize = headerBuffer.getInt();
List<RemoteLogSegmentMetadataSnapshot> result = new ArrayList<>(metadataSnapshotsSize);
ByteBuffer lenBuffer = ByteBuffer.allocate(4);
int lenBufferReadCt;
while ((lenBufferReadCt = channel.read(lenBuffer)) > 0) {
lenBuffer.rewind();
if (lenBufferReadCt != lenBuffer.capacity()) {
throw new IOException("Invalid amount of data read for the length of an entry, file may have been corrupted.");
}
// entry format: <entry-length><entry-bytes>
// Read the length of each entry
final int len = lenBuffer.getInt();
lenBuffer.rewind();
// Read the entry
ByteBuffer data = ByteBuffer.allocate(len);
final int read = channel.read(data);
if (read != len) {
throw new IOException("Invalid amount of data read, file may have been corrupted.");
}
// We are always adding RemoteLogSegmentMetadata only as you can see in #write() method.
// Did not add a specific serde for RemoteLogSegmentMetadata and reusing RemoteLogMetadataSerde
final RemoteLogSegmentMetadataSnapshot remoteLogSegmentMetadata = (RemoteLogSegmentMetadataSnapshot) serde.deserialize(data.array());
result.add(remoteLogSegmentMetadata);
}
if (metadataSnapshotsSize != result.size()) {
throw new IOException("Unexpected entries in the snapshot file. Expected size: " + metadataSnapshotsSize + ", but found: " + result.size());
}
return Optional.of(new Snapshot(version, metadataPartition, metadataPartitionOffset, result));
}
}
use of java.nio.channels.ReadableByteChannel in project webpieces by deanhiller.
the class TestLocalStorage method testCopyFromClassPath.
@Test
public void testCopyFromClassPath() throws IOException {
String bucketName = "testbucket";
String blobName = "mytest.txt";
String copyBlobName = "mytest_copy.txt";
Storage.CopyRequest request = Storage.CopyRequest.newBuilder().setSource(BlobId.of(bucketName, blobName)).setTarget(BlobId.of(bucketName, copyBlobName)).build();
Page<GCPBlob> testbucket = instance.list("copybucket");
CopyInterface copy = instance.copy(request);
ReadableByteChannel readFile = instance.reader("copybucket", "mytest_copy.txt");
InputStream i = Channels.newInputStream(readFile);
String text = new BufferedReader(new InputStreamReader(i, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
// Passed.
Assert.assertEquals("Some Test", text);
}
use of java.nio.channels.ReadableByteChannel in project webpieces by deanhiller.
the class TestLocalStorage method testWriteThenReadFromBuildDir.
@Test
public void testWriteThenReadFromBuildDir() throws IOException {
BlobId id = BlobId.of("testbucket", "fileShit.txt");
writeFile(id);
ReadableByteChannel channel = instance.reader("testbucket", "fileShit.txt");
InputStream i = Channels.newInputStream(channel);
String text = new BufferedReader(new InputStreamReader(i, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
Assert.assertEquals("testing a bitch", text);
}
use of java.nio.channels.ReadableByteChannel in project webpieces by deanhiller.
the class TestLocalStorage method testReadFromClasspath.
@Test
public void testReadFromClasspath() {
ReadableByteChannel channel = instance.reader("testbucket", "mytest.txt");
InputStream i = Channels.newInputStream(channel);
String text = new BufferedReader(new InputStreamReader(i, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
Assert.assertEquals("Some Test", text);
}
use of java.nio.channels.ReadableByteChannel in project webpieces by deanhiller.
the class LocalStorage method reader.
@Override
public ReadableByteChannel reader(String bucket, String blob, Storage.BlobSourceOption... options) {
InputStream in = this.getClass().getClassLoader().getResourceAsStream(bucket + "/" + blob);
if (in != null) {
ReadableByteChannel channel = Channels.newChannel(in);
return channel;
}
// read from build directory
File file = new File(LOCAL_BUILD_DIR + bucket + "/" + blob);
try {
InputStream i = new FileInputStream(file);
ReadableByteChannel channel = Channels.newChannel(i);
return channel;
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
Aggregations