Example usage for java.nio ByteBuffer flip

List of usage examples for java.nio ByteBuffer flip

Introduction

In this page you can find the example usage for java.nio ByteBuffer flip.

Prototype

public final Buffer flip() 

Source Link

Document

Flips this buffer.

Usage

From source file:com.github.ambry.utils.UtilsTest.java

@Test
public void testSerializeNullableString() {
    String randomString = getRandomString(10);
    ByteBuffer outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length);
    Utils.serializeNullableString(outputBuffer, randomString);
    outputBuffer.flip();
    int length = outputBuffer.getInt();
    assertEquals("Input and output string lengths don't match ", randomString.getBytes().length, length);
    byte[] output = new byte[length];
    outputBuffer.get(output);// w w  w  .  j ava2  s  .co m
    assertFalse("Output buffer shouldn't have any remaining, but has " + outputBuffer.remaining() + " bytes",
            outputBuffer.hasRemaining());
    String outputString = new String(output);
    assertEquals("Input and output strings don't match", randomString, outputString);

    randomString = null;
    outputBuffer = ByteBuffer.allocate(4);
    Utils.serializeNullableString(outputBuffer, randomString);
    outputBuffer.flip();
    length = outputBuffer.getInt();
    assertEquals("Input and output string lengths don't match", 0, length);
    output = new byte[length];
    outputBuffer.get(output);
    assertFalse("Output buffer shouldn't have any remaining, but has " + outputBuffer.remaining() + " bytes",
            outputBuffer.hasRemaining());
    outputString = new String(output);
    assertEquals("Output string \"" + outputString + "\" expected to be empty", outputString, "");
}

From source file:alluxio.shell.command.CpCommand.java

/**
 * Copies a file or directory specified by srcPath from the local filesystem to dstPath in the
 * Alluxio filesystem space.//from w  w  w. j  a  v  a 2 s .co m
 *
 * @param srcPath the {@link AlluxioURI} of the source file in the local filesystem
 * @param dstPath the {@link AlluxioURI} of the destination
 * @throws AlluxioException when Alluxio exception occurs
 * @throws IOException when non-Alluxio exception occurs
 */
private void copyPath(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException {
    File src = new File(srcPath.getPath());
    if (!src.isDirectory()) {
        // If the dstPath is a directory, then it should be updated to be the path of the file where
        // src will be copied to.
        if (mFileSystem.exists(dstPath) && mFileSystem.getStatus(dstPath).isFolder()) {
            dstPath = dstPath.join(src.getName());
        }

        FileOutStream os = null;
        try (Closer closer = Closer.create()) {
            os = closer.register(mFileSystem.createFile(dstPath));
            FileInputStream in = closer.register(new FileInputStream(src));
            FileChannel channel = closer.register(in.getChannel());
            ByteBuffer buf = ByteBuffer.allocate(8 * Constants.MB);
            while (channel.read(buf) != -1) {
                buf.flip();
                os.write(buf.array(), 0, buf.limit());
            }
        } catch (Exception e) {
            // Close the out stream and delete the file, so we don't have an incomplete file lying
            // around.
            if (os != null) {
                os.cancel();
                if (mFileSystem.exists(dstPath)) {
                    mFileSystem.delete(dstPath);
                }
            }
            throw e;
        }
    } else {
        mFileSystem.createDirectory(dstPath);
        List<String> errorMessages = new ArrayList<>();
        File[] fileList = src.listFiles();
        if (fileList == null) {
            String errMsg = String.format("Failed to list files for directory %s", src);
            errorMessages.add(errMsg);
            fileList = new File[0];
        }
        int misFiles = 0;
        for (File srcFile : fileList) {
            AlluxioURI newURI = new AlluxioURI(dstPath, new AlluxioURI(srcFile.getName()));
            try {
                copyPath(new AlluxioURI(srcPath.getScheme(), srcPath.getAuthority(), srcFile.getPath()),
                        newURI);
            } catch (IOException e) {
                errorMessages.add(e.getMessage());
                if (!mFileSystem.exists(newURI)) {
                    misFiles++;
                }
            }
        }
        if (errorMessages.size() != 0) {
            if (misFiles == fileList.length) {
                // If the directory doesn't exist and no files were created, then delete the directory
                if (mFileSystem.exists(dstPath)) {
                    mFileSystem.delete(dstPath);
                }
            }
            throw new IOException(Joiner.on('\n').join(errorMessages));
        }
    }
}

From source file:com.openteach.diamond.network.waverider.network.DefaultNetWorkClient.java

/**
 * ?, ?inputBuffer/*from w  w w .j ava 2 s  .c o m*/
 * @param key
 * @throws IOException
 * @throws InterruptedException
 */
private void onRead(SelectionKey key) throws IOException, InterruptedException {
    logger.debug("onRead");
    // ?
    ByteBuffer readByteBuffer = ByteBuffer.allocate(NetWorkConstants.DEFAULT_NETWORK_BUFFER_SIZE);
    int ret = 0;
    do {
        ret = socketChannel.read(readByteBuffer);
    } while (ret > 0);

    // , ???
    if (ret == -1) {
        throw new IOException("EOF");
    }
    readByteBuffer.flip();
    if (readByteBuffer.hasRemaining()) {
        inputBuffer.put(readByteBuffer);
        synchronized (waitMoreDataLock) {
            waitMoreDataLock.notifyAll();
        }
    }

    //logger.info("Slave read " + readByteBuffer.remaining() + " bytes");
}

From source file:eu.stratosphere.nephele.services.iomanager.IOManagerPerformanceBenchmark.java

@SuppressWarnings("resource")
private final void speedTestNIO(int bufferSize, boolean direct) throws IOException {
    final Channel.ID tmpChannel = ioManager.createChannel();

    File tempFile = null;//from www.  j ava  2 s  . c om
    FileChannel fs = null;

    try {
        tempFile = new File(tmpChannel.getPath());

        RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
        fs = raf.getChannel();

        ByteBuffer buf = direct ? ByteBuffer.allocateDirect(bufferSize) : ByteBuffer.allocate(bufferSize);

        long writeStart = System.currentTimeMillis();

        int valsLeft = NUM_INTS_WRITTEN;
        while (valsLeft-- > 0) {
            if (buf.remaining() < 4) {
                buf.flip();
                fs.write(buf);
                buf.clear();
            }
            buf.putInt(valsLeft);
        }

        if (buf.position() > 0) {
            buf.flip();
            fs.write(buf);
        }

        fs.close();
        raf.close();
        fs = null;

        long writeElapsed = System.currentTimeMillis() - writeStart;

        // ----------------------------------------------------------------

        raf = new RandomAccessFile(tempFile, "r");
        fs = raf.getChannel();
        buf.clear();

        long readStart = System.currentTimeMillis();

        fs.read(buf);
        buf.flip();

        valsLeft = NUM_INTS_WRITTEN;
        while (valsLeft-- > 0) {
            if (buf.remaining() < 4) {
                buf.compact();
                fs.read(buf);
                buf.flip();
            }
            if (buf.getInt() != valsLeft) {
                throw new IOException();
            }
        }

        fs.close();
        raf.close();

        long readElapsed = System.currentTimeMillis() - readStart;

        LOG.info("NIO Channel with buffer " + bufferSize + ": write " + writeElapsed + " msecs, read "
                + readElapsed + " msecs.");
    } finally {
        // close if possible
        if (fs != null) {
            fs.close();
            fs = null;
        }
        // try to delete the file
        if (tempFile != null) {
            tempFile.delete();
        }
    }
}

From source file:alluxio.cli.fs.command.CpCommand.java

/**
 * Copies a file or directory specified by srcPath from the local filesystem to dstPath in the
 * Alluxio filesystem space./*from  w w  w  .  j a  va 2 s . c o  m*/
 *
 * @param srcPath the {@link AlluxioURI} of the source file in the local filesystem
 * @param dstPath the {@link AlluxioURI} of the destination
 */
private void copyPath(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException {
    File src = new File(srcPath.getPath());
    if (!src.isDirectory()) {
        // If the dstPath is a directory, then it should be updated to be the path of the file where
        // src will be copied to.
        if (mFileSystem.exists(dstPath) && mFileSystem.getStatus(dstPath).isFolder()) {
            dstPath = dstPath.join(src.getName());
        }

        FileOutStream os = null;
        try (Closer closer = Closer.create()) {
            FileWriteLocationPolicy locationPolicy;
            locationPolicy = CommonUtils.createNewClassInstance(
                    Configuration.<FileWriteLocationPolicy>getClass(
                            PropertyKey.USER_FILE_COPY_FROM_LOCAL_WRITE_LOCATION_POLICY),
                    new Class[] {}, new Object[] {});
            os = closer.register(mFileSystem.createFile(dstPath,
                    CreateFileOptions.defaults().setLocationPolicy(locationPolicy)));
            FileInputStream in = closer.register(new FileInputStream(src));
            FileChannel channel = closer.register(in.getChannel());
            ByteBuffer buf = ByteBuffer.allocate(8 * Constants.MB);
            while (channel.read(buf) != -1) {
                buf.flip();
                os.write(buf.array(), 0, buf.limit());
            }
        } catch (Exception e) {
            // Close the out stream and delete the file, so we don't have an incomplete file lying
            // around.
            if (os != null) {
                os.cancel();
                if (mFileSystem.exists(dstPath)) {
                    mFileSystem.delete(dstPath);
                }
            }
            throw e;
        }
    } else {
        mFileSystem.createDirectory(dstPath);
        List<String> errorMessages = new ArrayList<>();
        File[] fileList = src.listFiles();
        if (fileList == null) {
            String errMsg = String.format("Failed to list files for directory %s", src);
            errorMessages.add(errMsg);
            fileList = new File[0];
        }
        int misFiles = 0;
        for (File srcFile : fileList) {
            AlluxioURI newURI = new AlluxioURI(dstPath, new AlluxioURI(srcFile.getName()));
            try {
                copyPath(new AlluxioURI(srcPath.getScheme(), srcPath.getAuthority(), srcFile.getPath()),
                        newURI);
            } catch (IOException e) {
                errorMessages.add(e.getMessage());
                if (!mFileSystem.exists(newURI)) {
                    misFiles++;
                }
            }
        }
        if (errorMessages.size() != 0) {
            if (misFiles == fileList.length) {
                // If the directory doesn't exist and no files were created, then delete the directory
                if (mFileSystem.exists(dstPath)) {
                    mFileSystem.delete(dstPath);
                }
            }
            throw new IOException(Joiner.on('\n').join(errorMessages));
        }
    }
}

From source file:com.turn.ttorrent.client.TorrentHandler.java

/**
 * Build this torrent's pieces array./*from w w  w  .ja  v a  2 s .  c  om*/
 *
 * <p>
 * Hash and verify any potentially present local data and create this
 * torrent's pieces array from their respective hash provided in the
 * torrent meta-info.
 * </p>
 *
 * <p>
 * This function should be called soon after the constructor to initialize
 * the pieces array.
 * </p>
 */
@VisibleForTesting
/* pp */ void init() throws InterruptedException, IOException {
    {
        State s = getState();
        if (s != State.WAITING) {
            LOG.info("Restarting torrent from state " + s);
            return;
        }
    }
    setState(State.VALIDATING);

    try {
        int npieces = torrent.getPieceCount();

        long size = getSize();
        // Store in a local so we can update with minimal synchronization.
        BitSet completedPieces = new BitSet(npieces);
        long completedSize = 0;

        ThreadPoolExecutor executor = client.getEnvironment().getExecutorService();
        // TorrentCreator.newExecutor("TorrentHandlerInit");
        try {
            LOG.info("{}: Analyzing local data for {} ({} pieces)...",
                    new Object[] { getLocalPeerName(), getName(), npieces });

            int step = 10;
            CountDownLatch latch = new CountDownLatch(npieces);
            for (int index = 0; index < npieces; index++) {
                // TODO: Read the file sequentially and pass it to the validator.
                // Otherwise we thrash the disk on validation.
                ByteBuffer buffer = ByteBuffer.allocate(getPieceLength(index));
                bucket.read(buffer, getPieceOffset(index));
                buffer.flip();
                executor.execute(new PieceValidator(torrent, index, buffer, completedPieces, latch));

                if (index / (float) npieces * 100f > step) {
                    LOG.info("{}:  ... {}% complete", getLocalPeerName(), step);
                    step += 10;
                }
            }
            latch.await();

            for (int i = completedPieces.nextSetBit(0); i >= 0; i = completedPieces.nextSetBit(i + 1)) {
                completedSize += getPieceLength(i);
            }
        } finally {
            // Request orderly executor shutdown and wait for hashing tasks to
            // complete.
            // executor.shutdown();
            // executor.awaitTermination(1, TimeUnit.SECONDS);
        }

        LOG.debug("{}: {}: we have {}/{} bytes ({}%) [{}/{} pieces].",
                new Object[] { getLocalPeerName(), getName(), completedSize, size,
                        String.format("%.1f", (100f * (completedSize / (float) size))),
                        completedPieces.cardinality(), getPieceCount() });

        synchronized (lock) {
            this.completedPieces = completedPieces;
        }

        if (isComplete())
            setState(State.SEEDING);
        else
            setState(State.SHARING);
    } catch (Exception e) {
        setState(State.ERROR);
        Throwables.propagateIfPossible(e, InterruptedException.class, IOException.class);
        throw Throwables.propagate(e);
    }
}

From source file:com.github.ambry.utils.UtilsTest.java

@Test
public void testDeserializeString() {
    String randomString = getRandomString(10);
    ByteBuffer outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length);
    Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII);
    outputBuffer.flip();
    String outputString = Utils.deserializeString(outputBuffer, StandardCharsets.US_ASCII);
    assertEquals("Input and output strings don't match", randomString, outputString);

    randomString = getRandomString(10) + "";
    outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length);
    Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII);
    outputBuffer.flip();/*  www.j  a  va 2s  .  c  om*/
    outputString = Utils.deserializeString(outputBuffer, StandardCharsets.US_ASCII);
    randomString = randomString.substring(0, randomString.length() - 1) + "?";
    assertEquals("Input and output strings don't match", randomString, outputString);

    randomString = "";
    outputBuffer = ByteBuffer.allocate(4);
    Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII);
    outputBuffer.flip();
    outputString = Utils.deserializeString(outputBuffer, StandardCharsets.US_ASCII);
    assertEquals("Output string \"" + outputString + "\" expected to be empty", outputString, "");

    randomString = getRandomString(10);
    outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length);
    outputBuffer.putInt(12);
    outputBuffer.put(randomString.getBytes());
    outputBuffer.flip();
    try {
        outputString = Utils.deserializeString(outputBuffer, StandardCharsets.US_ASCII);
        Assert.fail("Deserialization should have failed " + randomString);
    } catch (RuntimeException e) {
    }
}

From source file:com.github.ambry.utils.UtilsTest.java

@Test
public void testSerializeString() {
    String randomString = getRandomString(10);
    ByteBuffer outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length);
    Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII);
    outputBuffer.flip();
    int length = outputBuffer.getInt();
    assertEquals("Input and output string lengths don't match", randomString.getBytes().length, length);
    byte[] output = new byte[length];
    outputBuffer.get(output);/* w w w. j  a va 2  s .  c om*/
    assertFalse("Output buffer shouldn't have any remaining, but has " + outputBuffer.remaining() + " bytes",
            outputBuffer.hasRemaining());
    String outputString = new String(output);
    assertEquals("Input and output strings don't match", randomString, outputString);

    randomString = getRandomString(10) + "";
    outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length);
    Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII);
    outputBuffer.flip();
    length = outputBuffer.getInt();
    assertEquals("Input and output string lengths don't match ", (randomString.getBytes().length - 1), length);
    output = new byte[length];
    outputBuffer.get(output);
    assertFalse("Output buffer shouldn't have any remaining, but has " + outputBuffer.remaining() + " bytes",
            outputBuffer.hasRemaining());
    outputString = new String(output);
    randomString = randomString.substring(0, randomString.length() - 1) + "?";
    assertEquals("Input and output strings don't match", randomString, outputString);

    randomString = "";
    outputBuffer = ByteBuffer.allocate(4);
    Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII);
    outputBuffer.flip();
    length = outputBuffer.getInt();
    assertEquals("Input and output string lengths don't match", 0, length);
    output = new byte[length];
    outputBuffer.get(output);
    assertFalse("Output buffer shouldn't have any remaining, but has " + outputBuffer.remaining() + " bytes",
            outputBuffer.hasRemaining());
    outputString = new String(output);
    assertEquals("Output string \"" + outputString + "\" expected to be empty", outputString, "");

    randomString = getRandomString(10);
    outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length - 1);
    try {
        Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII);
        Assert.fail("Serialization should have failed due to insufficient space");
    } catch (RuntimeException e) {
    }
}