Package java.nio.charset

Examples of java.nio.charset.CharsetDecoder


   
  for (int i = 0; i < manual_charset.length; i++) {
     try {
     String  name = manual_charset[i];
    
     CharsetDecoder decoder = Charset.forName(name).newDecoder();
    
     if ( decoder != null ){
      
       LocaleUtilDecoder  lu_decoder =  new LocaleUtilDecoderReal(decoders.size(),decoder);
      
       decoder_names.add( lu_decoder.getName());
     
       if ( i == 0 ){
        
         system_decoder = lu_decoder;
       }
      
       decoders.add( lu_decoder );
      
     }else if ( i == 0 ){
      
       Debug.out( "System decoder failed to be found!!!!" );
     }
    
     }catch (Exception ignore) {
     }
   }

  general_decoders = new LocaleUtilDecoder[generalCharsets.length];
 
  for (int i=0;i<general_decoders.length;i++){
   
    int  gi = decoder_names.indexOf( generalCharsets[i]);
   
    if ( gi != -1 ){
   
      general_decoders[i] = (LocaleUtilDecoder)decoders.get(gi);
    }
  }

  boolean show_all = COConfigurationManager.getBooleanParameter("File.Decoder.ShowAll" );

  if ( show_all ){
   
    Map m = Charset.availableCharsets();
     
    Iterator it = m.keySet().iterator();
 
    while(it.hasNext()){
       
      String  charset_name = (String)it.next();
       
      if ( !decoder_names.contains( charset_name)){
       
        try {
          CharsetDecoder decoder = Charset.forName(charset_name).newDecoder();
        
          if ( decoder != null ){
           
            LocaleUtilDecoder  lu_decoder = new LocaleUtilDecoderReal(decoders.size(),decoder);
         
View Full Code Here


        canonical_requested_name = LocaleUtilDecoderFallback.NAME;

      } else {

        CharsetDecoder requested_decoder = Charset.forName(encoding).newDecoder();

        canonical_requested_name = requested_decoder.charset().name();
      }

      boolean ok = false;

      for (int i = 0; i < candidates.length; i++) {
View Full Code Here

    public DecoderTest() throws DecoderException {
        b = Hex.decodeHex(dump.toCharArray());
    }
   
    public void testDecodeError() throws CharacterCodingException {
        CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
        ByteBuffer buffer = ByteBuffer.wrap(b);
       
        try {
            decoder.decode(buffer);
            fail("Must throw MalformedInputException");
        } catch (MalformedInputException e) {
            // OK
        }
    }
View Full Code Here

    public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
        Charset cs = (this.charset == null)
            ? Charset.forName(encodingName)
            : this.charset;
        CharsetDecoder decoder = cs.newDecoder();

        CodingErrorAction action;
        if (ignoreEncodingErrors)
            action = CodingErrorAction.REPLACE;
        else
            action = CodingErrorAction.REPORT;

        return decoder
            .onMalformedInput(action)
            .onUnmappableCharacter(action);
    }
View Full Code Here

            return encName;
    }

    public CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
        String encodingName = getEncodingName();
        CharsetDecoder decoder;
        try {
            decoder = getDecoder(encodingName, ignoreEncodingErrors);
        } catch (IllegalCharsetNameException e) {
            log.error("unsupported.encoding", encodingName);
            return (CharBuffer)CharBuffer.allocate(1).flip();
        } catch (UnsupportedCharsetException e) {
            log.error("unsupported.encoding", encodingName);
            return (CharBuffer)CharBuffer.allocate(1).flip();
        }

        // slightly overestimate the buffer size to avoid reallocation.
        float factor =
            decoder.averageCharsPerByte() * 0.8f +
            decoder.maxCharsPerByte() * 0.2f;
        CharBuffer dest = CharBuffer.
            allocate(10 + (int)(inbuf.remaining()*factor));

        while (true) {
            CoderResult result = decoder.decode(inbuf, dest, true);
            dest.flip();

            if (result.isUnderflow()) { // done reading
                // make sure there is at least one extra character
                if (dest.limit() == dest.capacity()) {
                    dest = CharBuffer.allocate(dest.capacity()+1).put(dest);
                    dest.flip();
                }
                return dest;
            } else if (result.isOverflow()) { // buffer too small; expand
                int newCapacity =
                    10 + dest.capacity() +
                    (int)(inbuf.remaining()*decoder.maxCharsPerByte());
                dest = CharBuffer.allocate(newCapacity).put(dest);
            } else if (result.isMalformed() || result.isUnmappable()) {
                // bad character in input

                // report coding error (warn only pre 1.5)
View Full Code Here

     * @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) {
           throw new IllegalStateException("Invalid characted encoding", e);
        }
        return charBuffer.toString();
    }
View Full Code Here

  byte[] ba = new byte[n];
  StringBuffer sb = new StringBuffer(n);
  ByteBuffer bb = ByteBuffer.allocate(n);
  CharBuffer cb = CharBuffer.allocate(n);
  CharsetDecoder dec = ThreadLocalCoders.decoderFor("UTF-8")
      .onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE);

  // This is not horribly efficient, but it will do for now
  char c = s.charAt(0);
      boolean betweenBrackets = false;

  for (int i = 0; i < n;) {
      assert c == s.charAt(i)// Loop invariant
      if (c == '[') {
    betweenBrackets = true;
      } else if (betweenBrackets && c == ']') {
    betweenBrackets = false;
      }
      if (c != '%' || betweenBrackets) {
    sb.append(c);
    if (++i >= n)
        break;
    c = s.charAt(i);
    continue;
      }
      bb.clear();
      int ui = i;
      for (;;) {
    assert (n - i >= 2);
    bb.put(decode(s.charAt(++i), s.charAt(++i)));
    if (++i >= n)
        break;
    c = s.charAt(i);
    if (c != '%')
        break;
      }
      bb.flip();
      cb.clear();
      dec.reset();
      CoderResult cr = dec.decode(bb, cb, true);
      assert cr.isUnderflow();
      cr = dec.flush(cb);
      assert cr.isUnderflow();
      sb.append(cb.flip().toString());
  }

  return sb.toString();
View Full Code Here

   * @param encoding the encoding to use
   * @return the ByteByuffer as String
   */
  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) {
      RuntimeException re = new RuntimeException("coding exception for `" + encoding + "` occured: " + cce.toString(), cce);
      throw re;
    }
View Full Code Here

   public static String decodePath(String path)
   {
      Matcher matcher = encodedCharsMulti.matcher(path);
      StringBuffer buf = new StringBuffer();
      CharsetDecoder decoder = Charset.forName(UTF_8).newDecoder();
      while (matcher.find())
      {
         decoder.reset();
         String decoded = decodeBytes(matcher.group(1), decoder);
         decoded = decoded.replace("\\", "\\\\");
         decoded = decoded.replace("$", "\\$");
         matcher.appendReplacement(buf, decoded);
      }
View Full Code Here

        if (!s.respondsTo("to_str")) {
            throw runtime.newTypeError("can't convert " + s.getMetaClass() + " into String");
        }
        ByteList bytes = s.convertToString().getByteList();
        ByteBuffer buf = ByteBuffer.wrap(bytes.unsafeBytes(), bytes.begin(), bytes.length());
        CharsetDecoder decoder = Charset.forName("x-JISAutoDetect").newDecoder();
        try {
            decoder.decode(buf);
        } catch (CharacterCodingException e) {
            return runtime.newFixnum(UNKNOWN.getValue());
        }
        if (!decoder.isCharsetDetected()) {
            return runtime.newFixnum(UNKNOWN.getValue());
        }
        Charset charset = decoder.detectedCharset();
        String name = charset.name();
//        System.out.println("detect: " + name + "\n");
        if ("Shift_JIS".equals(name))
            return runtime.newFixnum(SJIS.getValue());
        if ("windows-31j".equals(name))
View Full Code Here

TOP

Related Classes of java.nio.charset.CharsetDecoder

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.