Example usage for org.apache.commons.io Charsets UTF_8

List of usage examples for org.apache.commons.io Charsets UTF_8

Introduction

In this page you can find the example usage for org.apache.commons.io Charsets UTF_8.

Prototype

Charset UTF_8

To view the source code for org.apache.commons.io Charsets UTF_8.

Click Source Link

Document

Eight-bit Unicode Transformation Format.

Usage

From source file:org.hobbit.core.components.AbstractCommandReceivingComponent.java

/**
 * Sends the given command to the command queue with the given data appended
 * and using the given properties.//  w  ww .  java  2 s  .  co  m
 *
 * @param command
 *            the command that should be sent
 * @param data
 *            data that should be appended to the command
 * @param props
 *            properties that should be used for the message
 * @throws IOException
 *             if a communication problem occurs
 */
protected void sendToCmdQueue(byte command, byte data[], BasicProperties props) throws IOException {
    byte sessionIdBytes[] = getHobbitSessionId().getBytes(Charsets.UTF_8);
    // + 5 because 4 bytes for the session ID length and 1 byte for the
    // command
    int dataLength = sessionIdBytes.length + 5;
    boolean attachData = (data != null) && (data.length > 0);
    if (attachData) {
        dataLength += data.length;
    }
    ByteBuffer buffer = ByteBuffer.allocate(dataLength);
    buffer.putInt(sessionIdBytes.length);
    buffer.put(sessionIdBytes);
    buffer.put(command);
    if (attachData) {
        buffer.put(data);
    }
    cmdChannel.basicPublish(Constants.HOBBIT_COMMAND_EXCHANGE_NAME, "", props, buffer.array());
}

From source file:org.hobbit.core.components.AbstractSystemAdapter.java

/**
 * This method sends the given result data for the task with the given task id
 * to the evaluation storage.//from www.j  a  v  a  2 s .  c  o m
 *
 * @param taskIdString
 *            the id of the task
 * @param data
 *            the data of the task
 * @throws IOException
 *             if there is an error during the sending
 */
protected void sendResultToEvalStorage(String taskIdString, byte[] data) throws IOException {
    byte[] taskIdBytes = taskIdString.getBytes(Charsets.UTF_8);
    // + 4 for taskIdBytes.length
    // + 4 for data.length
    int capacity = 4 + 4 + taskIdBytes.length + data.length;
    ByteBuffer buffer = ByteBuffer.allocate(capacity);
    buffer.putInt(taskIdBytes.length);
    buffer.put(taskIdBytes);
    buffer.putInt(data.length);
    buffer.put(data);
    sender2EvalStore.sendData(buffer.array());
}

From source file:org.hobbit.core.rabbit.FileStreamingTest.java

private void generateFiles(String sendInputDir) {
    System.out.println("Generating files...");
    if (!sendInputDir.endsWith(File.separator)) {
        sendInputDir += File.separator;
    }//from  w ww  .  ja  v a 2  s.  c  om

    OutputStream os = null;
    // create first file
    try {
        os = new BufferedOutputStream(new FileOutputStream(sendInputDir + "file1.dat"));
        ByteBuffer buffer = ByteBuffer.allocate(4000);
        IntBuffer intBuffer = buffer.asIntBuffer();
        int number = 0;
        // for (int i = 0; i < 200; ++i) {
        for (int j = 0; j < 1000; ++j) {
            intBuffer.put(number);
            ++number;
        }
        os.write(buffer.array());
        buffer.position(0);
        intBuffer.position(0);
        // }
        os.flush();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    } finally {
        IOUtils.closeQuietly(os);
    }
    // create second file
    try {
        os = new BufferedOutputStream(new FileOutputStream(sendInputDir + "file2.txt"));
        byte data[] = "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
                .getBytes(Charsets.UTF_8);
        for (int i = 0; i < 200; ++i) {
            os.write(data);
        }
        os.flush();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    } finally {
        IOUtils.closeQuietly(os);
    }
    // create third file
    try {
        os = new BufferedOutputStream(new FileOutputStream(sendInputDir + "file3.dat"));
        ByteBuffer buffer = ByteBuffer.allocate(400);
        IntBuffer intBuffer = buffer.asIntBuffer();
        Random random = new Random();
        // for (int i = 0; i < 200; ++i) {
        for (int j = 0; j < 100; ++j) {
            intBuffer.put(random.nextInt());
        }
        os.write(buffer.array());
        buffer.position(0);
        intBuffer.position(0);
        // }
        os.flush();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:org.hobbit.core.rabbit.RabbitMQUtils.java

/**
 * Transforms the given byte data into a String using the UTF-8 encoding.
 *
 * @param data//from   www  . j  a  va 2s  . c  o m
 *            the byte array that should be transformed
 * @return the String or null if the given byte array is null
 */
public static String readString(byte[] data) {
    if (data == null) {
        return null;
    } else {
        return new String(data, Charsets.UTF_8);
    }
}

From source file:org.hobbit.core.rabbit.RabbitMQUtils.java

/**
 * Transforms the given byte data into a String using the UTF-8 encoding.
 *
 * @param data//from  w  w w .  java 2 s  .  c o m
 *            the byte array that should be transformed
 * @param offset
 *            position at which the parsing will start
 * @param length
 *            number of bytes that should be parsed
 * @return the String or null if the given byte array is null
 */
public static String readString(byte[] data, int offset, int length) {
    if (data == null) {
        return null;
    } else {
        return new String(data, offset, length, Charsets.UTF_8);
    }
}

From source file:org.hobbit.core.rabbit.RabbitMQUtils.java

/**
 * Reads a String from the given buffer assuming that it is preceded by an
 * int value containing the length of the String.
 *
 * @param buffer//from  www .  j a v  a 2 s .  c  om
 *            the buffer that contains the byte data of the String
 * @return the String or null if the given buffer is null
 */
public static String readString(ByteBuffer buffer) {
    if (buffer == null) {
        return null;
    } else {
        return new String(readByteArray(buffer), Charsets.UTF_8);
    }
}

From source file:org.hobbit.core.rabbit.RabbitMQUtils.java

/**
 * Creates a byte array representation of the given String using UTF-8
 * encoding.//from  www. j a  va  2s .c  om
 *
 * @param string
 *            the String that should be transformed into a byte array
 * @return the UTF-8 byte array of the given String
 */
public static byte[] writeString(String string) {
    if (string == null) {
        return new byte[0];
    } else {
        return string.getBytes(Charsets.UTF_8);
    }
}

From source file:org.hobbit.core.rabbit.RabbitMQUtilsTest.java

@Test
public void testByteArrays() {
    performByteArraysTest(new byte[][] { new byte[0], new byte[0], new byte[0] });
    performByteArraysTest(new byte[][] { new byte[0], new byte[0], new byte[100] });
    performByteArraysTest(new byte[][] { "test".getBytes(Charsets.UTF_8), new byte[0],
            "one more test".getBytes(Charsets.ISO_8859_1) });
}

From source file:org.hobbit.core.rabbit.RpcClientBasedEchoClient.java

@Override
public void run() {
    try {/*  w w w  .  j a  v  a  2s .  c  o  m*/
        String msg, rsp;
        for (int i = 0; i < numberOfMessages; ++i) {
            msg = Integer.toString(random.nextInt());
            rsp = new String(client.request(msg.getBytes(Charsets.UTF_8)), Charsets.UTF_8);
            if (!msg.equals(rsp)) {
                System.err.println("Message \"" + msg + "\" and response \"" + rsp + "\" are not equal!");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.isisaddons.wicket.pdfjs.cpt.ui.PdfJsViewerPanel.java

PdfJsViewerPanel(String id, ScalarModel scalarModel) {
    super(id, scalarModel);

    final URL resource = Resources.getResource(PdfJsViewerPanel.class, "PdfJsViewerPanelCallbacks.template.js");
    try {//  w  w  w  .ja  va 2  s  .  com
        pdfJsViewerPanelCallbacksTemplateJs = Resources.toString(resource, Charsets.UTF_8);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}