Example usage for java.nio CharBuffer get

List of usage examples for java.nio CharBuffer get

Introduction

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

Prototype

public abstract char get(int index);

Source Link

Document

Returns a char at the specified index; the position is not changed.

Usage

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(5);
    cb1.put(2, 'j');
    cb1.rewind();//from  www  . j  av  a 2 s.co m

    System.out.println(cb1.get(2));
}

From source file:Main.java

public static void main(String[] args) {
    CharBuffer cb1 = CharBuffer.allocate(5);
    cb1.put(2, 'j');
    cb1.rewind();/* www  .  j a v  a2  s .  c o  m*/

    char[] charArray = new char[5];
    cb1.get(charArray);
    System.out.println(Arrays.toString(charArray));

}

From source file:Main.java

/**
 * Converts a byte array into a character array using the default character
 * set.//w ww.ja v  a 2  s. c  om
 * 
 * @param bytes
 *            The source bytes.
 * @param charsetName
 *            The character set to use.
 * @return The result characters.
 */
public static char[] toCharArray(byte[] bytes, String charsetName) {
    java.nio.ByteBuffer bb = java.nio.ByteBuffer.wrap(bytes);
    java.nio.CharBuffer cb = java.nio.charset.Charset.forName(charsetName).decode(bb);
    char[] r = new char[cb.remaining()];
    cb.get(r);
    return r;
}

From source file:it.geosolutions.geostore.core.security.password.SecurityUtils.java

/**
 * Converts byte array to char array./*from   w ww. j  ava  2s  .  c  om*/
 */
public static char[] toChars(byte[] b, Charset charset) {
    CharBuffer buff = charset.decode(ByteBuffer.wrap(b));
    char[] tmp = new char[buff.limit()];
    buff.get(tmp);
    return tmp;
}

From source file:ape.NetworkDisconnectCommand.java

/**
 * This method writes to the Stand output
 *///  w ww  .  j  a  v  a 2s .  c  om
private boolean writeSTDOut(Process p) {
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    int count;
    CharBuffer cbuf = CharBuffer.allocate(99999);
    try {
        count = stdInput.read(cbuf);
        if (count != -1)
            cbuf.array()[count] = '\0';
        else if (cbuf.array()[0] != '\0')
            count = cbuf.array().length;
        else
            count = 0;
        for (int i = 0; i < count; i++)
            System.out.print(cbuf.get(i));
    } catch (IOException e) {
        System.err.println(
                "Writing Stdout in NetworkDisconnectCommand catches an exception, Turn on the VERBOSE flag to see the stack trace");
        e.printStackTrace();
        return false;
    }

    try {
        stdInput.close();
    } catch (IOException e) {
        System.err.println("Unable to close the IOStream");
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:com.arksoft.epamms.ZGlobal1_Operation.java

int WriteCSV_RecordFromEntity(View lLibPers, String entityName, int lFile) throws IOException {
    CharBuffer charBuffer = CharBuffer.allocate(32000);
    int nLth;// www .  ja v a  2 s  . c  o m

    charBuffer.put(0, '"');
    charBuffer.put(1, entityName.charAt(0)); // S E P (Student Employee Prospect)
    charBuffer.put(2, '"');
    charBuffer.put(3, ',');
    nLth = 4;

    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, entityName, "Status", false);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Person", "CampusID", false);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Person", "ID", true);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Person", "LastName", false);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Person", "FirstName", false);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Person", "MiddleName", false);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Person", "Suffix", false);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Person", "PreferedFirstName", false);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Person", "Gender", false);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Person", "MaritalStatus", false);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Person", "HomePhone", false);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Person", "WorkPhone", false);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Person", "Extension", false);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Person", "eMailAddress", false);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Person", "DateOfBirth", false);
    if (CheckExistenceOfEntity(lLibPers, "Address") == 0) {
        nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Address", "Line1", false);
        nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Address", "City", false);
        nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Address", "StateProvince", false);
        nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Address", "PostalCode", false);
        nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "Address", "Country", false);
    } else {
        charBuffer.put(nLth++, ',');
        charBuffer.put(nLth++, ',');
        charBuffer.put(nLth++, ',');
        charBuffer.put(nLth++, ',');
        charBuffer.put(nLth++, ',');
    }

    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, entityName, "ID", false);
    nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, entityName, "eMailAddress", false);
    if (entityName.charAt(0) == 'S') {
        nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, entityName, "CurrentLevel", false);
        nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, "AdministrativeDivision", "Name", false);
        nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, entityName, "ClearingHouseGradDate", false);
    } else if (entityName.charAt(0) == 'P') {
        nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, entityName, "ExpectedEntryTerm", false);
        nLth += AddAttributeToCSV(charBuffer, nLth, lLibPers, entityName, "ExpectedEntryYear", false);
    }

    if (nLth > 0 && charBuffer.get(nLth - 1) == ',')
        charBuffer.put(nLth - 1, '\0'); // drop terminating ',' and null terminate
    else
        charBuffer.put(nLth++, '\0'); // ensure null termination

    m_KZOEP1AA.SysWriteLine(lLibPers, lFile, charBuffer.toString());
    return nLth;
}

From source file:org.marketcetera.util.unicode.ReaderTest.java

@Test
public void regularReader() throws Exception {
    CloseableRegistry r = new CloseableRegistry();
    try {/*from   w  w w  . ja  v  a2 s. com*/
        ByteArrayInputStream is = new ByteArrayInputStream(HELLO_EN_NAT);
        r.register(new InputStreamWrapper(is));
        UnicodeInputStreamReader reader = new UnicodeInputStreamReader(is);
        r.register(new ReaderWrapper(reader));

        assertNull(reader.getDecodingStrategy());
        assertNull(reader.getRequestedSignatureCharset());
        assertNull(reader.getSignatureCharset());

        assertTrue(reader.ready());

        assertFalse(reader.markSupported());

        try {
            reader.mark(0);
            fail();
        } catch (IOException ex) {
            // Desired.
        }

        try {
            reader.reset();
            fail();
        } catch (IOException ex) {
            // Desired.
        }

        assertEquals(HELLO_EN.charAt(0), reader.read());

        assertEquals(1, reader.skip(1));

        char[] charArray = new char[1];
        assertEquals(1, reader.read(charArray));
        assertEquals(HELLO_EN.charAt(2), charArray[0]);

        charArray = new char[3];
        assertEquals(1, reader.read(charArray, 1, 1));
        assertEquals(HELLO_EN.charAt(3), charArray[1]);

        CharBuffer charBuffer = CharBuffer.allocate(10);
        assertEquals(1, reader.read(charBuffer));
        assertEquals(HELLO_EN.charAt(4), charBuffer.get(0));

        assertEquals(-1, reader.read());
        assertEquals(-1, reader.read(charArray));
        assertEquals(-1, reader.read(charArray, 1, 1));
        assertEquals(-1, reader.read(charBuffer));
        assertFalse(reader.ready());

        reader.close();
        reader.close();

        // Ensure that close() has closed the stream, by trying to
        // read from the reader: this is not testing whether
        // read() fails; it tests whether close() worked.
        try {
            reader.read();
            fail();
        } catch (IOException ex) {
            // Desired.
        }

        try {
            reader.ready();
            fail();
        } catch (IOException ex) {
            // Desired.
        }
    } finally {
        r.close();
    }
}

From source file:org.opencms.i18n.CmsEncoder.java

/**
 * Encodes all characters that are contained in the String which can not displayed 
 * in the given encodings charset with HTML entity references
 * like <code>&amp;#8364;</code>.<p>
 * //  w  ww . j  a  v  a  2  s .c  om
 * This is required since a Java String is 
 * internally always stored as Unicode, meaning it can contain almost every character, but 
 * the HTML charset used might not support all such characters.<p>
 * 
 * @param input the input to encode for HTML
 * @param encoding the charset to encode the result with
 * 
 * @return the input with the encoded HTML entities
 * 
 * @see #decodeHtmlEntities(String, String)
 */
public static String encodeHtmlEntities(String input, String encoding) {

    StringBuffer result = new StringBuffer(input.length() * 2);
    CharBuffer buffer = CharBuffer.wrap(input.toCharArray());
    Charset charset = Charset.forName(encoding);
    CharsetEncoder encoder = charset.newEncoder();
    for (int i = 0; i < buffer.length(); i++) {
        int c = buffer.get(i);
        if (c < 128) {
            // first 128 chars are contained in almost every charset
            result.append((char) c);
            // this is intended as performance improvement since 
            // the canEncode() operation appears quite CPU heavy
        } else if (encoder.canEncode((char) c)) {
            // encoder can encode this char
            result.append((char) c);
        } else {
            // append HTML entity reference
            result.append(ENTITY_PREFIX);
            result.append(c);
            result.append(";");
        }
    }
    return result.toString();
}

From source file:org.opencms.i18n.CmsEncoder.java

/**
 * Encodes all characters that are contained in the String which can not displayed 
 * in the given encodings charset with Java escaping like <code>\u20ac</code>.<p>
 * /*w  w w.ja  va 2 s  . c  om*/
 * This can be used to escape values used in Java property files.<p>
 * 
 * @param input the input to encode for Java
 * @param encoding the charset to encode the result with
 * 
 * @return the input with the encoded Java entities
 */
public static String encodeJavaEntities(String input, String encoding) {

    StringBuffer result = new StringBuffer(input.length() * 2);
    CharBuffer buffer = CharBuffer.wrap(input.toCharArray());
    Charset charset = Charset.forName(encoding);
    CharsetEncoder encoder = charset.newEncoder();
    for (int i = 0; i < buffer.length(); i++) {
        int c = buffer.get(i);
        if (c < 128) {
            // first 128 chars are contained in almost every charset
            result.append((char) c);
            // this is intended as performance improvement since 
            // the canEncode() operation appears quite CPU heavy
        } else if (encoder.canEncode((char) c)) {
            // encoder can encode this char
            result.append((char) c);
        } else {
            // append Java entity reference
            result.append("\\u");
            String hex = Integer.toHexString(c);
            int pad = 4 - hex.length();
            for (int p = 0; p < pad; p++) {
                result.append('0');
            }
            result.append(hex);
        }
    }
    return result.toString();
}

From source file:ubic.basecode.io.ByteArrayConverter.java

/**
 * @param barray/*from   ww w  .  ja v  a2s.  c om*/
 * @return char[]
 */
public char[] byteArrayToChars(byte[] barray) {
    if (barray == null)
        return null;

    CharBuffer buf = ByteBuffer.wrap(barray).asCharBuffer();
    char[] array = new char[buf.remaining()];
    buf.get(array);
    return array;

}