jug.ORG.ua >  Dashboard > Java Almanac > ... > java.nio.charset > Converting Between Strings (Unicode) and Other Character Set Encodings
Converting Between Strings (Unicode) and Other Character Set Encodings Log In   View a printable version of the current page.

  • View Info
  •  

Added by Sergey Druzkin, last edited by Sergey Druzkin on Sep 08, 2006  (view change)
Labels: 

e186. Converting Between Strings (Unicode) and Other Character Set Encodings

Many network protocols and files store their characters with a
byte-oriented character set such as ISO-8859-1 (ISO-Latin-1).
However, Java's native character encoding is Unicode.

This example demonstrates how to convert ISO-8859-1 encoded
bytes in a ByteBuffer to a string in a CharBuffer and visa versa.

// Create the encoder and decoder for ISO-8859-1
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = charset.newDecoder();
    CharsetEncoder encoder = charset.newEncoder();
    
    try {
        // Convert a string to ISO-LATIN-1 bytes in a ByteBuffer
        // The new ByteBuffer is ready to be read.
        ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("a string"));
    
        // Convert ISO-LATIN-1 bytes in a ByteBuffer to a character ByteBuffer and then to a string.
        // The new ByteBuffer is ready to be read.
        CharBuffer cbuf = decoder.decode(bbuf);
        String s = cbuf.toString();
    } catch (CharacterCodingException e) {
    }

In the example above, the encoding and decoding methods created new
ByteBuffers into which to encode or decoding the data. Moreover,
the newly allocated ByteBuffers are non-direct
(e168 Determining If a ByteBuffer Is Direct). The encoder and decoder provide
methods that use a supplied ByteBuffer rather than create one.
Here's an example that uses these methods:

// Create a direct ByteBuffer.
    // This buffer will be used to send and recieve data from channels.
    ByteBuffer bbuf = ByteBuffer.allocateDirect(1024);
    
    // Create a non-direct character ByteBuffer
    CharBuffer cbuf = CharBuffer.allocate(1024);
    
    // Convert characters in cbuf to bbuf
    encoder.encode(cbuf, bbuf, false);
    
    // flip bbuf before reading from it
    bbuf.flip();
    
    // Convert bytes in bbuf to cbuf
    decoder.decode(bbuf, cbuf, false);
    
    // flip cbuf before reading from it
    cbuf.flip();

Машинный перевод

e186. Преобразование Между Вереницами(Нитями) (Unicode) и Другие Зашифровывания Набора символов

Много протоколов сети и файлов хранят их характеры с a
ориентируемый на байт набор символов, типа ISO-8859-1 (ISO-Latin-1).
Однако, родное зашифровывание характера(знака) Явы - Unicode.

Этот пример демонстрирует, как преобразовать закодированный ISO-8859-1
байты в Битебаффере к веренице(нити) в CharBuffer и визе versa.

// Create the encoder and decoder for ISO-8859-1
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = charset.newDecoder();
    CharsetEncoder encoder = charset.newEncoder();
    
    try {
        // Convert a string to ISO-LATIN-1 bytes in a ByteBuffer
        // The new ByteBuffer is ready to be read.
        ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("a string"));
    
        // Convert ISO-LATIN-1 bytes in a ByteBuffer to a character ByteBuffer and then to a string.
        // The new ByteBuffer is ready to be read.
        CharBuffer cbuf = decoder.decode(bbuf);
        String s = cbuf.toString();
    } catch (CharacterCodingException e) {
    }

В примере выше, зашифровывание и расшифровка методов создали новый
ByteBuffers, чтобы закодировать или расшифровка данных. Кроме того,
недавно ассигнованные(размещенные) ByteBuffers являются непрямыми
(e168 Определение, Если Битебаффер Является Прямым). Кодирующее устройство и декодер обеспечивают
методы, которые используют снабженного Битебаффера, а не создают тот.
Вот - пример, который использует эти методы:

// Create a direct ByteBuffer.
    // This buffer will be used to send and recieve data from channels.
    ByteBuffer bbuf = ByteBuffer.allocateDirect(1024);
    
    // Create a non-direct character ByteBuffer
    CharBuffer cbuf = CharBuffer.allocate(1024);
    
    // Convert characters in cbuf to bbuf
    encoder.encode(cbuf, bbuf, false);
    
    // flip bbuf before reading from it
    bbuf.flip();
    
    // Convert bytes in bbuf to cbuf
    decoder.decode(bbuf, cbuf, false);
    
    // flip cbuf before reading from it
    cbuf.flip();

Оригинальная версия

e186. Converting Between Strings (Unicode) and Other Character Set Encodings

Many network protocols and files store their characters with a
byte-oriented character set such as ISO-8859-1 (ISO-Latin-1).
However, Java's native character encoding is Unicode.

This example demonstrates how to convert ISO-8859-1 encoded
bytes in a ByteBuffer to a string in a CharBuffer and visa versa.

// Create the encoder and decoder for ISO-8859-1
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = charset.newDecoder();
    CharsetEncoder encoder = charset.newEncoder();
    
    try {
        // Convert a string to ISO-LATIN-1 bytes in a ByteBuffer
        // The new ByteBuffer is ready to be read.
        ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("a string"));
    
        // Convert ISO-LATIN-1 bytes in a ByteBuffer to a character ByteBuffer and then to a string.
        // The new ByteBuffer is ready to be read.
        CharBuffer cbuf = decoder.decode(bbuf);
        String s = cbuf.toString();
    } catch (CharacterCodingException e) {
    }

In the example above, the encoding and decoding methods created new
ByteBuffers into which to encode or decoding the data. Moreover,
the newly allocated ByteBuffers are non-direct
(e168 Determining If a ByteBuffer Is Direct). The encoder and decoder provide
methods that use a supplied ByteBuffer rather than create one.
Here's an example that uses these methods:

// Create a direct ByteBuffer.
    // This buffer will be used to send and recieve data from channels.
    ByteBuffer bbuf = ByteBuffer.allocateDirect(1024);
    
    // Create a non-direct character ByteBuffer
    CharBuffer cbuf = CharBuffer.allocate(1024);
    
    // Convert characters in cbuf to bbuf
    encoder.encode(cbuf, bbuf, false);
    
    // flip bbuf before reading from it
    bbuf.flip();
    
    // Convert bytes in bbuf to cbuf
    decoder.decode(bbuf, cbuf, false);
    
    // flip cbuf before reading from it
    cbuf.flip();
Site running on a free Atlassian Confluence Open Source Project / Non-profit License granted to Java developers community of KPI ("JUG KPI"),. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.6.0 Build:#913 Sep 27, 2007) - Bug/feature request - Contact Administrators