Example usage for java.nio CharBuffer asReadOnlyBuffer

List of usage examples for java.nio CharBuffer asReadOnlyBuffer

Introduction

In this page you can find the example usage for java.nio CharBuffer asReadOnlyBuffer.

Prototype

public abstract CharBuffer asReadOnlyBuffer();

Source Link

Document

Returns a read-only buffer that shares its content with this buffer.

Usage

From source file:Main.java

/**
 * Returns a new byte array containing the characters of the specified
 * string encoded using the given charset.
 * //  w  ww  .  ja  v  a  2  s .c  o m
 * It is equivalent to <code>input.getBytes(charset)</code> except it has
 * workaround for the bug ID 61917.
 * 
 * @see https://code.google.com/p/android/issues/detail?id=61917
 */
//@formatter:off
/*
 * The original code is available from
 *     https://android.googlesource.com/platform/libcore/+/android-4.4_r1.2/libdvm/src/main/java/java/lang/String.java
 */
//@formatter:on
public static byte[] getBytes(String input, Charset charset) {
    CharBuffer chars = CharBuffer.wrap(input.toCharArray());
    // @formatter:off
    CharsetEncoder encoder = charset.newEncoder().onMalformedInput(CodingErrorAction.REPLACE)
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
    // @formatter:on
    ByteBuffer buffer;
    buffer = encode(chars.asReadOnlyBuffer(), encoder);
    byte[] bytes = new byte[buffer.limit()];
    buffer.get(bytes);
    return bytes;

}

From source file:com.metawiring.load.generators.LoremExtractGenerator.java

private CharBuffer loadLoremIpsum() {
    InputStream stream = LoremExtractGenerator.class.getClassLoader()
            .getResourceAsStream("data/lorem_ipsum_full.txt");
    if (stream == null) {
        throw new RuntimeException("lorem_ipsum_full.txt was missing.");
    }//from  w w w.ja  v a  2s. com

    CharBuffer image;
    try {
        InputStreamReader isr = new InputStreamReader(stream);
        image = CharBuffer.allocate(1024 * 1024);
        isr.read(image);
        isr.close();
    } catch (IOException e) {
        logger.error(e.getMessage());
        throw new RuntimeException(e);
    }
    image.flip();

    return image.asReadOnlyBuffer();

}

From source file:com.metawiring.load.generators.ExtractGenerator.java

private CharBuffer loadFileData() {
    InputStream stream = null;/*from w  ww. j  a v  a 2  s  .  co  m*/
    File onFileSystem = new File("data" + File.separator + fileName);

    if (onFileSystem.exists()) {
        try {
            stream = new FileInputStream(onFileSystem);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(
                    "Unable to find file " + onFileSystem.getPath() + " after verifying that it exists.");
        }
        logger.debug("Loaded file data from " + onFileSystem.getPath());
    }

    if (stream == null) {
        stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("data/" + fileName);
        logger.debug("Loaded file data from classpath resource " + fileName);
    }

    if (stream == null) {
        throw new RuntimeException(fileName + " was missing.");
    }

    CharBuffer image;
    try {
        InputStreamReader isr = new InputStreamReader(stream);
        image = CharBuffer.allocate(1024 * 1024);
        isr.read(image);
        isr.close();
    } catch (IOException e) {
        logger.error(e.getMessage());
        throw new RuntimeException(e);
    }
    image.flip();

    return image.asReadOnlyBuffer();

}