Java Code Examples for java.nio.charset.Charset.decode()

The following are Jave code examples for showing how to use decode() of the java.nio.charset.Charset class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: phone-simulator   File: CommandDataImpl.java   View Source Code Vote up 6 votes
public void decode() throws MAPException {

        if (this.isDecoded)
            return;
        this.isDecoded = true;

        this.decodedMessage = null;

        if (this.encodedData == null)
            throw new MAPException("Error decoding a text from Sms CommandData: encodedData field is null");

        // TODO: what is an encoding algorithm ?
        Charset chs = Charset.forName("US-ASCII");
        byte[] buf = this.encodedData;
        ByteBuffer bb = ByteBuffer.wrap(buf);
        CharBuffer bf = chs.decode(bb);
        this.decodedMessage = bf.toString();
    }
 
Example 2
Project: incubator-netbeans   File: Utils.java   View Source Code Vote up 6 votes
/**
    * Converts an input file stream into a char sequence.
    *
    * @throws IOException
    */
   static CharBuffer getCharSequence(final FileInputStream stream, Charset encoding) throws IOException {
       FileChannel channel = stream.getChannel();
       ByteBuffer bbuf = ByteBuffer.allocate((int) channel.size());
       try {
           channel.read(bbuf, 0);
       } catch (ClosedByInterruptException cbie) {
           return null;        //this is actually okay
       } finally {
           channel.close();
       }
       bbuf.rewind();
       CharBuffer cbuf = encoding.decode(bbuf);

       return cbuf;
}
 
Example 3
Project: Chorus-RF-Laptimer   File: UDPService.java   View Source Code Vote up 6 votes
public void run() {
    while (true) {
        try {
            mChannel.read(mReceiveBuf);
            mReceiveBuf.flip();
            Charset charset = Charset.forName("ASCII");
            CharBuffer cbuf = charset.decode(mReceiveBuf);
            String result = cbuf.toString();
            parseAndCallback(result);
            mReceiveBuf.clear();
        } catch (Exception e) {
            disconnect();
            //TODO: implement some handling here!
            break;
        }
    }
}
 
Example 4
Project: alfresco-repository   File: Utf7.java   View Source Code Vote up 5 votes
/**
 * Convert string from UTF-7 characters
 * 
 * @param string Input string for decoding
 * @return Decoded string
 */
public static String decode(String string, String charsetName)
{
    if (string.length() <= 1)
    {
        return string;
    }
    CharsetProvider provider = new CharsetProvider();
    Charset charset = provider.charsetForName(charsetName);
    CharBuffer charBuffer = charset.decode(ByteBuffer.wrap(string.getBytes()));
    return charBuffer.toString();
}
 
Example 5
Project: neoscada   File: AbstractScheduledInput.java   View Source Code Vote up 5 votes
protected static Object convert ( final byte[] data, final Charset charset )
{
    if ( data == null )
    {
        return null;
    }

    if ( charset == null )
    {
        return data;
    }

    final CharBuffer cb = charset.decode ( ByteBuffer.wrap ( data ) );
    return cb.toString ();
}
 
Example 6
Project: ditb   File: ByteIterator.java   View Source Code Vote up 4 votes
/** Consumes remaining contents of this object, and returns them as a string. */
public String toString() {
  Charset cset = Charset.forName("UTF-8");
  CharBuffer cb = cset.decode(ByteBuffer.wrap(this.toArray()));
  return cb.toString();
}