Package java.nio.charset

Examples of java.nio.charset.Charset


     * Decode the string encoded in the bytebuffer in UTF-8 format.
     * @param buffer the given buffer to analyze.
     * @return the decoded string
     */
    protected String decode(final ByteBuffer buffer) {
        Charset charset = Charset.forName("UTF-8");
        CharsetDecoder charsetDecoder = charset.newDecoder();

        CharBuffer charBuffer = null;
        try {
            charBuffer = charsetDecoder.decode(buffer);
        } catch (CharacterCodingException e) {
View Full Code Here


  String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
  if ((sd == null) || !(csn.equals(sd.requestedCharsetName())
            || csn.equals(sd.charsetName()))) {
      sd = null;
      try {
    Charset cs = lookupCharset(csn);
    if (cs != null)
        sd = new StringDecoder(cs, csn);
      } catch (IllegalCharsetNameException x) {}
            if (sd == null)
                throw new UnsupportedEncodingException(csn);
View Full Code Here

  String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
   if ((se == null) || !(csn.equals(se.requestedCharsetName())
             || csn.equals(se.charsetName()))) {
      se = null;
      try {
    Charset cs = lookupCharset(csn);
    if (cs != null)
        se = new StringEncoder(cs, csn);
      } catch (IllegalCharsetNameException x) {}
      if (se == null)
                throw new UnsupportedEncodingException (csn);
View Full Code Here

    public static String encode(String s, String enc)
  throws UnsupportedEncodingException {

  boolean needToChange = false;
        StringBuffer out = new StringBuffer(s.length());
  Charset charset;
  CharArrayWriter charArrayWriter = new CharArrayWriter();

  if (enc == null)
      throw new NullPointerException("charsetName");
View Full Code Here

    }
    return text;
  }

  protected String readTextFromStream(InputStream aTextStream) {
    Charset charset = getCharSet();
    int size = urlConnection.getContentLength();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(size != -1 ? size : 1024);
    byte[] buffer = new byte[1024];
    int len;
    try {
      while ((len = aTextStream.read(buffer)) != -1) {
        bos.write(buffer, 0, len);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    String streamedText = ""; // be robust
    try {
      streamedText = bos.toString(charset.name());
    } catch (UnsupportedEncodingException e) {} // we already checked for that above
    // check Content-Type text/plain; charset=iso-8859-1
    return streamedText;
  }
View Full Code Here

   */
  protected Charset getCharSet() {
    String contentType = urlConnection.getContentType();
    // find out about the charset from the URLConnection
    Matcher m = charsetPattern.matcher(contentType);
    Charset charset = Charset.forName("iso-8859-1"); // default charset
    if (m.find()) {
      String charsetString = m.group(1);
      try {
        charset = Charset.forName(charsetString);
      } catch (IllegalCharsetNameException e) {
View Full Code Here

   */
  public static String toString(ByteBuffer buffer, String encoding) throws UnsupportedEncodingException {
    try {
      CharsetDecoder decoder = decoders.get(encoding);
      if (decoder == null) {
        Charset charset = Charset.forName(encoding);
        decoder = charset.newDecoder();
          decoders.put(encoding, decoder);
          encoders.put(encoding, charset.newEncoder());
      }
     
      return decoder.decode(buffer).toString();
     
    } catch (CharacterCodingException cce) {
View Full Code Here

 
 
 
  public static ByteBuffer getAsByteBuffer() {
    try {
      Charset charset = Charset.forName("ISO-8859-1");
        CharsetEncoder encoder = charset.newEncoder();
        ByteBuffer buf = encoder.encode(CharBuffer.wrap(testMail.toCharArray()));
        return buf;
    } catch (Exception e) {
      throw new RuntimeException(e.toString());
    }
View Full Code Here

 
 
 
  public static ByteBuffer getAsByteBuffer() {
    try {
      Charset charset = Charset.forName("ISO-8859-1");
        CharsetEncoder encoder = charset.newEncoder();
        ByteBuffer buf = encoder.encode(CharBuffer.wrap(testMail.toCharArray()));
        return buf;
    } catch (Exception e) {
      throw new RuntimeException(e.toString());
    }
View Full Code Here

 
 
 
  public static ByteBuffer getAsByteBuffer() {
    try {
      Charset charset = Charset.forName("ISO-8859-1");
        CharsetEncoder encoder = charset.newEncoder();
        ByteBuffer buf = encoder.encode(CharBuffer.wrap(testMail.toCharArray()));
        return buf;
    } catch (Exception e) {
      throw new RuntimeException(e.toString());
    }
View Full Code Here

TOP

Related Classes of java.nio.charset.Charset

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.