Search in sources :

Example 1 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project deeplearning4j by deeplearning4j.

the class DoubleArrayTrie method write.

public void write(OutputStream output) throws IOException {
    baseBuffer.rewind();
    checkBuffer.rewind();
    tailBuffer.rewind();
    int baseCheckSize = Math.min(maxBaseCheckIndex + 64, baseBuffer.capacity());
    int tailSize = Math.min(tailIndex - TAIL_OFFSET + 64, tailBuffer.capacity());
    DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream(output));
    dataOutput.writeBoolean(compact);
    dataOutput.writeInt(baseCheckSize);
    dataOutput.writeInt(tailSize);
    WritableByteChannel channel = Channels.newChannel(dataOutput);
    ByteBuffer tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    IntBuffer tmpIntBuffer = tmpBuffer.asIntBuffer();
    tmpIntBuffer.put(baseBuffer.array(), 0, baseCheckSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);
    tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    tmpIntBuffer = tmpBuffer.asIntBuffer();
    tmpIntBuffer.put(checkBuffer.array(), 0, baseCheckSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);
    tmpBuffer = ByteBuffer.allocate(tailSize * 2);
    CharBuffer tmpCharBuffer = tmpBuffer.asCharBuffer();
    tmpCharBuffer.put(tailBuffer.array(), 0, tailSize);
    tmpBuffer.rewind();
    channel.write(tmpBuffer);
    dataOutput.flush();
}
Also used : IntBuffer(java.nio.IntBuffer) WritableByteChannel(java.nio.channels.WritableByteChannel) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 2 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project deeplearning4j by deeplearning4j.

the class IntegerArrayIO method writeArray.

public static void writeArray(OutputStream output, int[] array) throws IOException {
    DataOutputStream dataOutput = new DataOutputStream(output);
    int length = array.length;
    dataOutput.writeInt(length);
    ByteBuffer tmpBuffer = ByteBuffer.allocate(length * INT_BYTES);
    IntBuffer intBuffer = tmpBuffer.asIntBuffer();
    tmpBuffer.rewind();
    intBuffer.put(array);
    WritableByteChannel channel = Channels.newChannel(dataOutput);
    channel.write(tmpBuffer);
}
Also used : IntBuffer(java.nio.IntBuffer) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 3 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project deeplearning4j by deeplearning4j.

the class ConnectionCostsCompiler method compile.

@Override
public void compile() throws IOException {
    DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream(output));
    dataOutput.writeInt(cardinality);
    dataOutput.writeInt(bufferSize * SHORT_BYTES);
    ByteBuffer byteBuffer = ByteBuffer.allocate(costs.array().length * SHORT_BYTES);
    for (short cost : this.costs.array()) {
        byteBuffer.putShort(cost);
    }
    WritableByteChannel channel = Channels.newChannel(dataOutput);
    byteBuffer.flip();
    channel.write(byteBuffer);
    dataOutput.close();
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 4 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project AndroidAsync by koush.

the class NetworkEventReporterWrapper method interpretResponseEmitter.

public DataEmitter interpretResponseEmitter(final String requestId, @Nullable DataEmitter body, final boolean b64Encode) {
    final NetworkPeerManager peerManager = getPeerManagerIfEnabled();
    if (peerManager == null)
        return null;
    final WritableByteChannel channel;
    try {
        if (b64Encode) {
            final Base64OutputStream b64out = new Base64OutputStream(peerManager.getResponseBodyFileManager().openResponseBodyFile(requestId, false), Base64.DEFAULT);
            channel = Channels.newChannel(b64out);
        } else {
            channel = ((FileOutputStream) peerManager.getResponseBodyFileManager().openResponseBodyFile(requestId, false)).getChannel();
        }
    } catch (IOException e) {
        return null;
    }
    FilteredDataEmitter ret = new FilteredDataEmitter() {

        ByteBufferList pending = new ByteBufferList();

        @Override
        protected void report(Exception e) {
            super.report(e);
            StreamUtility.closeQuietly(channel);
            if (e == null)
                responseReadFinished(requestId);
            else
                responseReadFailed(requestId, e.toString());
        }

        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
            int amount = bb.remaining();
            ByteBuffer[] original = bb.getAllArray();
            ByteBuffer[] copy = new ByteBuffer[original.length];
            for (int i = 0; i < original.length; i++) {
                copy[i] = original[i].duplicate();
            }
            try {
                for (ByteBuffer c : copy) {
                    channel.write(c);
                }
            } catch (IOException ignored) {
                StreamUtility.closeQuietly(channel);
            }
            pending.addAll(original);
            dataReceived(requestId, amount, amount);
            super.onDataAvailable(emitter, pending);
        }
    };
    ret.setDataEmitter(body);
    return ret;
}
Also used : FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) ByteBufferList(com.koushikdutta.async.ByteBufferList) NetworkPeerManager(com.facebook.stetho.inspector.network.NetworkPeerManager) DataEmitter(com.koushikdutta.async.DataEmitter) FilteredDataEmitter(com.koushikdutta.async.FilteredDataEmitter) WritableByteChannel(java.nio.channels.WritableByteChannel) IOException(java.io.IOException) Base64OutputStream(android.util.Base64OutputStream) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException)

Example 5 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project hadoop by apache.

the class TestFadvisedFileRegion method testCustomShuffleTransfer.

@Test(timeout = 100000)
public void testCustomShuffleTransfer() throws IOException {
    File absLogDir = new File("target", TestFadvisedFileRegion.class.getSimpleName() + "LocDir").getAbsoluteFile();
    String testDirPath = StringUtils.join(Path.SEPARATOR, new String[] { absLogDir.getAbsolutePath(), "testCustomShuffleTransfer" });
    File testDir = new File(testDirPath);
    testDir.mkdirs();
    System.out.println(testDir.getAbsolutePath());
    File inFile = new File(testDir, "fileIn.out");
    File outFile = new File(testDir, "fileOut.out");
    //Initialize input file
    byte[] initBuff = new byte[FILE_SIZE];
    Random rand = new Random();
    rand.nextBytes(initBuff);
    FileOutputStream out = new FileOutputStream(inFile);
    try {
        out.write(initBuff);
    } finally {
        IOUtils.cleanup(LOG, out);
    }
    //define position and count to read from a file region.
    int position = 2 * 1024 * 1024;
    int count = 4 * 1024 * 1024 - 1;
    RandomAccessFile inputFile = null;
    RandomAccessFile targetFile = null;
    WritableByteChannel target = null;
    FadvisedFileRegion fileRegion = null;
    try {
        inputFile = new RandomAccessFile(inFile.getAbsolutePath(), "r");
        targetFile = new RandomAccessFile(outFile.getAbsolutePath(), "rw");
        target = targetFile.getChannel();
        Assert.assertEquals(FILE_SIZE, inputFile.length());
        //create FadvisedFileRegion
        fileRegion = new FadvisedFileRegion(inputFile, position, count, false, 0, null, null, 1024, false);
        //test corner cases
        customShuffleTransferCornerCases(fileRegion, target, count);
        long pos = 0;
        long size;
        while ((size = fileRegion.customShuffleTransfer(target, pos)) > 0) {
            pos += size;
        }
        //assert size
        Assert.assertEquals(count, (int) pos);
        Assert.assertEquals(count, targetFile.length());
    } finally {
        if (fileRegion != null) {
            fileRegion.releaseExternalResources();
        }
        IOUtils.cleanup(LOG, target);
        IOUtils.cleanup(LOG, targetFile);
        IOUtils.cleanup(LOG, inputFile);
    }
    //Read the target file and verify that copy is done correctly
    byte[] buff = new byte[FILE_SIZE];
    FileInputStream in = new FileInputStream(outFile);
    try {
        int total = in.read(buff, 0, count);
        Assert.assertEquals(count, total);
        for (int i = 0; i < count; i++) {
            Assert.assertEquals(initBuff[position + i], buff[i]);
        }
    } finally {
        IOUtils.cleanup(LOG, in);
    }
    //delete files and folders
    inFile.delete();
    outFile.delete();
    testDir.delete();
    absLogDir.delete();
}
Also used : Random(java.util.Random) RandomAccessFile(java.io.RandomAccessFile) FileOutputStream(java.io.FileOutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

WritableByteChannel (java.nio.channels.WritableByteChannel)220 ByteBuffer (java.nio.ByteBuffer)78 ReadableByteChannel (java.nio.channels.ReadableByteChannel)56 IOException (java.io.IOException)48 ByteArrayOutputStream (java.io.ByteArrayOutputStream)45 FileOutputStream (java.io.FileOutputStream)34 Test (org.junit.Test)33 File (java.io.File)28 OutputStream (java.io.OutputStream)24 ByteArrayInputStream (java.io.ByteArrayInputStream)23 Test (org.testng.annotations.Test)19 FileInputStream (java.io.FileInputStream)17 FileChannel (java.nio.channels.FileChannel)16 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)10 Path (java.nio.file.Path)9 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)8 Vector (java.util.Vector)8 ByteBufferOutputStream (com.github.ambry.utils.ByteBufferOutputStream)7 Writer (java.io.Writer)7