Search in sources :

Example 96 with ReadableByteChannel

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));
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream)

Example 97 with ReadableByteChannel

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);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) GCPStorage(org.webpieces.googlecloud.storage.api.GCPStorage) GCPBlob(org.webpieces.googlecloud.storage.api.GCPBlob) CopyInterface(org.webpieces.googlecloud.storage.api.CopyInterface) Test(org.junit.Test)

Example 98 with ReadableByteChannel

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);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) Test(org.junit.Test)

Example 99 with ReadableByteChannel

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);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) Test(org.junit.Test)

Example 100 with ReadableByteChannel

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);
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel)

Aggregations

ReadableByteChannel (java.nio.channels.ReadableByteChannel)307 ByteBuffer (java.nio.ByteBuffer)111 IOException (java.io.IOException)84 FileOutputStream (java.io.FileOutputStream)62 WritableByteChannel (java.nio.channels.WritableByteChannel)62 Test (org.junit.Test)52 File (java.io.File)50 FileChannel (java.nio.channels.FileChannel)49 FileInputStream (java.io.FileInputStream)43 ByteArrayInputStream (java.io.ByteArrayInputStream)38 InputStream (java.io.InputStream)36 URL (java.net.URL)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 Path (java.nio.file.Path)18 Test (org.testng.annotations.Test)14 FileNotFoundException (java.io.FileNotFoundException)13 ArrayList (java.util.ArrayList)12 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)11 MalformedURLException (java.net.MalformedURLException)11 Vector (java.util.Vector)11