Example usage for java.nio ByteBuffer array

List of usage examples for java.nio ByteBuffer array

Introduction

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

Prototype

public final byte[] array() 

Source Link

Document

Returns the byte array which this buffer is based on, if there is one.

Usage

From source file:com.mapr.storm.test.TailSpoutTest.java

private byte[] payload(String msg) {
    byte[] content = msg.getBytes(Charsets.UTF_8);
    ByteBuffer buf = ByteBuffer.allocate(4 + content.length);
    buf.putInt(content.length);//from   w  w w .ja va 2s. c  o m
    buf.put(content);
    buf.flip();
    return buf.array();
}

From source file:reactor.io.netty.http.PostAndGetTests.java

private void get(String path, SocketAddress address) {
    try {/*  w ww  . j a  va  2s .co m*/
        StringBuilder request = new StringBuilder().append(String.format("GET %s HTTP/1.1\r\n", path))
                .append("Connection: Keep-Alive\r\n").append("\r\n");
        java.nio.channels.SocketChannel channel = java.nio.channels.SocketChannel.open(address);
        System.out.println(String.format("get: request >> [%s]", request.toString()));
        channel.write(ByteBuffer.wrap(request.toString().getBytes()));
        ByteBuffer buf = ByteBuffer.allocate(4 * 1024);
        while (channel.read(buf) > -1)
            ;
        String response = new String(buf.array());
        System.out.println(String.format("get: << Response: %s", response));
        channel.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:reactor.io.net.http.PostAndGetTests.java

private void get(String path, SocketAddress address) {
    try {// w  ww .ja v a  2 s.  c o m
        StringBuilder request = new StringBuilder().append(String.format("GET %s HTTP/1.1\r\n", path))
                .append("Connection: Keep-Alive\r\n").append("\r\n");
        java.nio.channels.SocketChannel channel = java.nio.channels.SocketChannel.open(address);
        System.out.println(String.format("get: request >> [%s]", request.toString()));
        channel.write(Buffer.wrap(request.toString()).byteBuffer());
        ByteBuffer buf = ByteBuffer.allocate(4 * 1024);
        while (channel.read(buf) > -1)
            ;
        String response = new String(buf.array());
        System.out.println(String.format("get: << Response: %s", response));
        channel.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:reactor.io.netty.http.PostAndGetTests.java

private void post(String path, String data, SocketAddress address) {
    try {/* w w  w  .j  a v a  2  s. c o m*/
        StringBuilder request = new StringBuilder().append(String.format("POST %s HTTP/1.1\r\n", path))
                .append("Connection: Keep-Alive\r\n");
        request.append(String.format("Content-Length: %s\r\n", data.length())).append("\r\n").append(data)
                .append("\r\n");
        java.nio.channels.SocketChannel channel = java.nio.channels.SocketChannel.open(address);
        System.out.println(String.format("post: request >> [%s]", request.toString()));
        channel.write(ByteBuffer.wrap(request.toString().getBytes()));
        ByteBuffer buf = ByteBuffer.allocate(4 * 1024);
        while (channel.read(buf) > -1)
            ;
        String response = new String(buf.array());
        System.out.println(String.format("post: << Response: %s", response));
        channel.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static byte[] aesIGEencrypt(byte[] tmpAESiv, byte[] tmpAesKey, byte[] data) {
    try {//  w w w .ja  v a  2 s .  c o m

        ByteBuffer out = ByteBuffer.allocate(data.length);

        byte[] ivp = Arrays.copyOfRange(tmpAESiv, 0, tmpAESiv.length / 2);
        byte[] iv2p = Arrays.copyOfRange(tmpAESiv, tmpAESiv.length / 2, tmpAESiv.length);

        int len = data.length / AES_BLOCK_SIZE;

        byte[] xorInput = null;
        byte[] xorOutput = null;

        SecretKeySpec keySpec = null;
        keySpec = new SecretKeySpec(tmpAesKey, "AES");
        Cipher cipher = null;
        cipher = Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);

        byte[] input = null;
        byte[] output = null;

        for (int i = 0; i < len; i++) {

            input = Arrays.copyOfRange(data, i * AES_BLOCK_SIZE, (i + 1) * AES_BLOCK_SIZE);
            xorInput = xor(input, ivp);
            output = cipher.doFinal(xorInput);
            xorOutput = xor(output, iv2p);
            out.put(xorOutput);

            ivp = xorOutput;
            iv2p = input;
        }
        return out.array();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:reactor.io.net.http.PostAndGetTests.java

private void post(String path, String data, SocketAddress address) {
    try {/* www .  j  a v  a  2  s.  co  m*/
        StringBuilder request = new StringBuilder().append(String.format("POST %s HTTP/1.1\r\n", path))
                .append("Connection: Keep-Alive\r\n");
        request.append(String.format("Content-Length: %s\r\n", data.length())).append("\r\n").append(data)
                .append("\r\n");
        java.nio.channels.SocketChannel channel = java.nio.channels.SocketChannel.open(address);
        System.out.println(String.format("post: request >> [%s]", request.toString()));
        channel.write(Buffer.wrap(request.toString()).byteBuffer());
        ByteBuffer buf = ByteBuffer.allocate(4 * 1024);
        while (channel.read(buf) > -1)
            ;
        String response = new String(buf.array());
        System.out.println(String.format("post: << Response: %s", response));
        channel.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:reactor.ipc.netty.http.client.PostAndGetTests.java

private void get(String path, SocketAddress address) {
    try {//from  ww  w .jav a2 s  .  co  m
        StringBuilder request = new StringBuilder().append(String.format("GET %s HTTP/1.1\r\n", path))
                .append("Connection: Keep-Alive\r\n").append("\r\n");
        java.nio.channels.SocketChannel channel = java.nio.channels.SocketChannel.open(address);
        System.out.println(String.format("get: request >> [%s]", request.toString()));
        channel.write(ByteBuffer.wrap(request.toString().getBytes()));
        ByteBuffer buf = ByteBuffer.allocate(4 * 1024);
        while (channel.read(buf) > -1) {
        }
        String response = new String(buf.array());
        System.out.println(String.format("get: << Response: %s", response));
        channel.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:reactor.ipc.netty.http.client.PostAndGetTests.java

private void post(String path, String data, SocketAddress address) {
    try {//  ww  w .  jav a  2  s .co  m
        StringBuilder request = new StringBuilder().append(String.format("POST %s HTTP/1.1\r\n", path))
                .append("Connection: Keep-Alive\r\n");
        request.append(String.format("Content-Length: %s\r\n", data.length())).append("\r\n").append(data)
                .append("\r\n");
        java.nio.channels.SocketChannel channel = java.nio.channels.SocketChannel.open(address);
        System.out.println(String.format("post: request >> [%s]", request.toString()));
        channel.write(ByteBuffer.wrap(request.toString().getBytes()));
        ByteBuffer buf = ByteBuffer.allocate(4 * 1024);
        while (channel.read(buf) > -1) {
        }
        String response = new String(buf.array());
        Loggers.getLogger(PostAndGetTests.class).info("post: << " + "Response: %s", response);
        channel.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:deployer.publishers.openshift.RhcApplicationGitRepoModificationsTest.java

@Test
public void shouldUpdateGitRepoWithModifications()
        throws IOException, GitAPIException, ArchiveException, DeploymentException {
    File remoteTestDir = Files.resolve(GIT_TEST_DIR, "shouldUpdateGitRepoWithModificationsOrigin.git");
    File testDir = Files.resolve(GIT_TEST_DIR, "shouldUpdateGitRepoWithModifications");
    Files.createDirectories(testDir);

    Git remoteGit = Git.init().setBare(true).setDirectory(remoteTestDir).call();

    Git git = Git.cloneRepository().setURI(remoteTestDir.toString()).setDirectory(testDir).call();
    populateWithInitialFiles(git, testDir);

    IApplication instance = createMock(IApplication.class);
    IDomain domain = createMock(IDomain.class);
    IUser mockUser = createMock(IUser.class);

    expect(instance.getName()).andReturn("UnitTestApplication").anyTimes();
    expect(domain.getUser()).andReturn(mockUser).anyTimes();
    expect(mockUser.getRhlogin()).andReturn("UnitTestuser").anyTimes();

    replay(instance, domain, mockUser);//from   w w  w.  ja  v a  2s .  c om

    RhcApplication app = new RhcApplication(git, instance, domain, testDir, null);
    ByteBuffer sampleTarBall = TestUtils.createSampleAppTarBall(ArtifactType.Thrift);
    app.updateWithTarBall(sampleTarBall.array(), "v1.0");
    app.addStreamAsFile(Files.get(TestUtils.CONFIG_DIRECTORY, "extraInfo.conf"),
            new ByteArrayInputStream("Extra information".getBytes()));
    app.publishChanges();
    verify(instance, domain, mockUser);
    assertGitRepositoryFilesForUpdateGitRepoTest(testDir);
    File ensuredDir = Files.resolve(GIT_TEST_DIR, "shouldUpdateGitRepoWithModificationsEnsured");
    Git.cloneRepository().setURI(remoteTestDir.toURI().toString()).setDirectory(ensuredDir).call();
    assertGitRepositoryFilesForUpdateGitRepoTest(ensuredDir);
}

From source file:com.sastix.cms.server.services.cache.CacheFileUtilsServiceImpl.java

@Override
public byte[] downloadResource(URL url) throws IOException {
    //create buffer with capacity in bytes
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {//from  www  .j  a v a 2s. c  om
        ByteBuffer bufIn = ByteBuffer.allocate(1024);
        ReadableByteChannel rbc = Channels.newChannel(url.openStream());
        int bytesRead;
        while ((bytesRead = rbc.read(bufIn)) > 0) {
            baos.write(bufIn.array(), 0, bytesRead);
            bufIn.rewind();
        }
        bufIn.clear();
        return baos.toByteArray();
    } finally {
        baos.close();
    }
}