Example usage for java.nio ByteBuffer asCharBuffer

List of usage examples for java.nio ByteBuffer asCharBuffer

Introduction

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

Prototype

public abstract CharBuffer asCharBuffer();

Source Link

Document

Returns a char buffer which is based on the remaining content of this byte buffer.

Usage

From source file:Main.java

/**
 * @param size number of chars the buffer should hold
 * @return the newly allocated char buffer
 *///from   w  ww .jav a 2s  .com
public static CharBuffer createCharBuffer(int size) {
    ByteBuffer bb = ByteBuffer.allocateDirect(2 * size);
    bb.order(ByteOrder.nativeOrder());
    return bb.asCharBuffer();
}

From source file:Main.java

public static CharBuffer newCharBuffer(int numChars) {
    ByteBuffer buffer = ByteBuffer.allocateDirect(numChars * 2);
    buffer.order(ByteOrder.nativeOrder());
    return buffer.asCharBuffer();
}

From source file:com.atilika.kuromoji.trie.DoubleArrayTrie.java

/**
 * Load Stored data//from   ww  w.  ja v a  2 s. co  m
 *
 * @param input  input stream to read the double array trie from
 * @return double array trie, not null
 * @throws IOException if an IO error occured during reading the double array trie
 */
public static DoubleArrayTrie read(InputStream input) throws IOException {
    DoubleArrayTrie trie = new DoubleArrayTrie();
    DataInputStream dis = new DataInputStream(new BufferedInputStream(input));

    trie.compact = dis.readBoolean();
    int baseCheckSize = dis.readInt(); // Read size of baseArr and checkArr
    int tailSize = dis.readInt(); // Read size of tailArr
    ReadableByteChannel channel = Channels.newChannel(dis);

    ByteBuffer tmpBaseBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    channel.read(tmpBaseBuffer);
    tmpBaseBuffer.rewind();
    trie.baseBuffer = tmpBaseBuffer.asIntBuffer();

    ByteBuffer tmpCheckBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    channel.read(tmpCheckBuffer);
    tmpCheckBuffer.rewind();
    trie.checkBuffer = tmpCheckBuffer.asIntBuffer();

    ByteBuffer tmpTailBuffer = ByteBuffer.allocate(tailSize * 2);
    channel.read(tmpTailBuffer);
    tmpTailBuffer.rewind();
    trie.tailBuffer = tmpTailBuffer.asCharBuffer();

    input.close();
    return trie;
}

From source file:org.minig.imap.impl.MailboxNameUTF7Converter.java

/**
 * Decode the IMAP UTF-7 mailbox name to a Java String.
 * //from  w w  w.  j a  v  a2s . c  o m
 * @param mailboxName
 * @return the Java representation of the mailbox names
 */
public static String decode(String mailboxName) {
    int lastEnd = 0;
    int nextAnd = mailboxName.indexOf('&');

    // if no & is in the name no decoding must be done
    if (nextAnd == -1)
        return mailboxName;

    StringBuilder result = new StringBuilder(mailboxName.length());

    while (nextAnd != -1) {
        // add all the printable characters until this position
        result.append(mailboxName.substring(lastEnd, nextAnd));

        // can be either a switch to utf-7 or &-
        if (mailboxName.charAt(nextAnd + 1) == '-') {
            result.append('&');
            lastEnd = nextAnd + 2;
        } else {
            // find end of base64 code
            lastEnd = mailboxName.indexOf('-', nextAnd);
            int expanded = 0;

            // pad with A's for modified base64
            StringBuilder rawEncoded = new StringBuilder(
                    mailboxName.substring(nextAnd + 1, lastEnd).replace(',', '/'));
            while (rawEncoded.length() % 4 != 0) {
                rawEncoded.append('A');
                expanded++;
            }

            // decode the transformed modified-base64
            ByteBuffer decoded = ByteBuffer.wrap(Base64.decodeBase64(rawEncoded.toString()));
            decoded.limit(decoded.limit() - expanded);
            if (decoded.limit() % 2 != 0) {
                // delete the end zeros
                decoded.limit(decoded.limit() - (decoded.limit() % 2));
            }

            result.append(decoded.asCharBuffer());

            lastEnd++;
        }

        nextAnd = mailboxName.indexOf('&', lastEnd);
    }

    result.append(mailboxName.substring(lastEnd));
    String returnString = result.toString();
    return returnString;
}

From source file:com.cloudera.sqoop.lib.RecordParser.java

public List<String> parseRecord(ByteBuffer input) throws ParseError {
    if (null == input) {
        throw new ParseError("null input string");
    }//from w  w w. j a  va2s . com

    return parseRecord(input.asCharBuffer());
}

From source file:org.apache.sqoop.lib.RecordParser.java

public List<String> parseRecord(ByteBuffer input) throws com.cloudera.sqoop.lib.RecordParser.ParseError {
    if (null == input) {
        throw new com.cloudera.sqoop.lib.RecordParser.ParseError("null input string");
    }//from   www  .java  2s.  c  o  m

    return parseRecord(input.asCharBuffer());
}

From source file:org.apache.mnemonic.collections.DurableHashSetNGTest.java

@BeforeClass
public void setUp() throws Exception {
    rand = Utils.createRandom();//ww w. ja va 2s.  co  m
    unsafe = Utils.getUnsafe();
    m_act = new NonVolatileMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"),
            1024 * 1024 * 1024, "./pobj_hashset.dat", true);
    cKEYCAPACITY = m_act.handlerCapacity();
    m_act.setBufferReclaimer(new Reclaim<ByteBuffer>() {
        @Override
        public boolean reclaim(ByteBuffer mres, Long sz) {
            System.out.println(String.format("Reclaim Memory Buffer: %X  Size: %s",
                    System.identityHashCode(mres), null == sz ? "NULL" : sz.toString()));
            System.out.println(" String buffer " + mres.asCharBuffer().toString());
            return false;
        }
    });
    m_act.setChunkReclaimer(new Reclaim<Long>() {
        @Override
        public boolean reclaim(Long mres, Long sz) {
            System.out.println(String.format("Reclaim Memory Chunk: %X  Size: %s",
                    System.identityHashCode(mres), null == sz ? "NULL" : sz.toString()));
            return false;
        }
    });

    for (long i = 0; i < cKEYCAPACITY; ++i) {
        m_act.setHandler(i, 0L);
    }
}

From source file:org.apache.mnemonic.collections.DurableArrayNGTest.java

@BeforeClass
public void setUp() throws Exception {
    rand = Utils.createRandom();/*from w  ww  .  j a v  a2s.c  o m*/
    unsafe = Utils.getUnsafe();
    m_act = new NonVolatileMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"),
            1024 * 1024 * 1024, "./pobj_array.dat", true);
    cKEYCAPACITY = m_act.handlerCapacity();
    m_act.setBufferReclaimer(new Reclaim<ByteBuffer>() {
        @Override
        public boolean reclaim(ByteBuffer mres, Long sz) {
            System.out.println(String.format("Reclaim Memory Buffer: %X  Size: %s",
                    System.identityHashCode(mres), null == sz ? "NULL" : sz.toString()));
            System.out.println(" String buffer " + mres.asCharBuffer().toString());
            return false;
        }
    });
    m_act.setChunkReclaimer(new Reclaim<Long>() {
        @Override
        public boolean reclaim(Long mres, Long sz) {
            System.out.println(String.format("Reclaim Memory Chunk: %X  Size: %s",
                    System.identityHashCode(mres), null == sz ? "NULL" : sz.toString()));
            return false;
        }
    });

    for (long i = 0; i < cKEYCAPACITY; ++i) {
        m_act.setHandler(i, 0L);
    }
}

From source file:com.atilika.kuromoji.trie.DoubleArrayTrie.java

public void write(OutputStream output) throws IOException {

    baseBuffer.rewind();/* w w  w.  j a  va  2  s .  c  o  m*/
    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();
}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffWriter.java

private void writeMMHeaderAndSummaryMD(JSONObject summaryMD) throws IOException {
    if (summaryMD.has("Comment")) {
        summaryMD.remove("Comment");
    }//w  w w  . j a  v  a  2  s  .  co m
    String summaryMDString = summaryMD.toString();
    int mdLength = summaryMDString.length();
    ByteBuffer buffer = ByteBuffer.allocate(40).order(BYTE_ORDER);
    if (BYTE_ORDER.equals(ByteOrder.BIG_ENDIAN)) {
        buffer.asCharBuffer().put(0, (char) 0x4d4d);
    } else {
        buffer.asCharBuffer().put(0, (char) 0x4949);
    }
    buffer.asCharBuffer().put(1, (char) 42);
    buffer.putInt(4, 40 + mdLength);
    //8 bytes for file header +
    //8 bytes for index map offset header and offset +
    //8 bytes for display settings offset header and display settings offset
    //8 bytes for comments offset header and comments offset
    //8 bytes for summaryMD header  summary md length + 
    //1 byte for each character of summary md     
    buffer.putInt(32, SUMMARY_MD_HEADER);
    buffer.putInt(36, mdLength);
    ByteBuffer[] buffers = new ByteBuffer[2];
    buffers[0] = buffer;
    buffers[1] = ByteBuffer.wrap(getBytesFromString(summaryMDString));
    fileChannel_.write(buffers);
    filePosition_ += buffer.position() + mdLength;
}