Package java.nio.charset

Examples of java.nio.charset.CharsetDecoder


        in.position(in.position() + length);
        return sb.toString()// doesn't copy the string char array
      }
    } while (false); // not a real do loop - do only once
 
    CharsetDecoder decoder = DECODER.get();
    if (null == decoder) {
      decoder = UTF8.newDecoder()
        .onMalformedInput(CodingErrorAction.REPLACE)
        .onUnmappableCharacter(CodingErrorAction.REPLACE);
      DECODER.set(decoder);      
    }
    ByteBuffer partToDecode = in.slice();
    partToDecode.limit(length);
    CharBuffer cb;
    try {
      cb = decoder.decode(partToDecode);
      in.position(in.position() + length);
    } catch (CharacterCodingException e) {
      // should never happen
      throw new RuntimeException(e);
    }
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

        private void writeTo(Writer out, String encoding) throws IOException{
            //Get the charset based on the encoding or return the default if
            //encoding == null
            Charset charset = (encoding == null) ?
                    Charset.defaultCharset() : Charset.forName(encoding);
            CharsetDecoder decoder = charset.newDecoder();
            CharBuffer decodedBuffer = decoder.decode(
                    ByteBuffer.wrap(_byteArrayOutputStream.getInnerArray(),
                            0,_byteArrayOutputStream.getInnerCount()));
            if (decodedBuffer.hasArray()){
                out.write(decodedBuffer.array());
            }
View Full Code Here

      return null;
  }
    }

    private static String getStringFromUTF8Buffer(ByteBuffer buffer) {
  CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
  try {
      return decoder.decode(buffer).toString();
  } catch (CharacterCodingException e) {
      return "(error decoding UTF-8 message: " + e.toString() + ")";
  }
    }
View Full Code Here

    }
   
    public void testGetString() throws Exception
    {
        ByteBuffer buf = ByteBuffer.allocate( 16 );
        CharsetDecoder decoder;

        decoder = Charset.forName( "ISO-8859-1" ).newDecoder();
        buf.put( (byte) 'A' );
        buf.put( (byte) 'B' );
        buf.put( (byte) 'C' );
View Full Code Here

        dst.flip();
        return dst;
    }

    static String decodeString(ByteBuffer src, Charset charset) {
        final CharsetDecoder decoder = CharsetUtil.getDecoder(charset);
        final CharBuffer dst = CharBuffer.allocate(
                (int) ((double) src.remaining() * decoder.maxCharsPerByte()));
        try {
            CoderResult cr = decoder.decode(src, dst, true);
            if (!cr.isUnderflow()) {
                cr.throwException();
            }
            cr = decoder.flush(dst);
            if (!cr.isUnderflow()) {
                cr.throwException();
            }
        } catch (CharacterCodingException x) {
            throw new IllegalStateException(x);
View Full Code Here

        return retval;
    }
   
    public void refreshComments() {
        Charset latin1 = Charset.forName("ISO-8859-1");
        CharsetDecoder decoder = null;
        if (latin1 != null)
            decoder = latin1.newDecoder();
        if (_ss != null) {
            _scomments = _map.getServiceComments(_ss.getHostName(), _ss.getServiceDescription());
            if (_scomments != null) {
               DefaultTableModel model = (DefaultTableModel)jTableComments.getModel();
               int rows = jTableComments.getRowCount();
               int sindex = -1;
               int eindex = -1;
               for (int i = 0; i < rows; i++)
                   model.removeRow(0);
               if (_scomments != null) {
                   for (StatServiceComment ssc: _scomments) {
                       sindex = -1;
                       eindex = -1;
                       Vector vector = new Vector();
                       vector.add(ssc.getCommentId());
                        if (ssc.getCommentData().toLowerCase().startsWith("issue <a href")) {
                           sindex = ssc.getCommentData().indexOf("\">");
                           if (sindex > 0)
                               eindex = ssc.getCommentData().indexOf("<", sindex);
                           if (sindex > 0 && eindex > sindex)
                               vector.add("Issue "  +  ssc.getCommentData().substring(sindex + 2,eindex));
                           else {
                               if (decoder != null) {
                                    try {
                                        vector.add(decoder.decode(ByteBuffer.wrap(ssc.getCommentData().getBytes())).toString());
                                    } catch (CharacterCodingException ex) {
                                        // let's not give up insert the comment data as is
                                        vector.add(ssc.getCommentData());
                                    }
                               } else {
                                   vector.add(ssc.getCommentData());
                                   System.out.println("adding default");
                               }
                           }
                       } else {
                           if (decoder != null) {
                                try {
                                    vector.add(decoder.decode(ByteBuffer.wrap(ssc.getCommentData().getBytes())).toString());
                                } catch (CharacterCodingException ex) {
                                    // let's not give up insert the comment data as is
                                    vector.add(ssc.getCommentData());
                                }
                           } else {
View Full Code Here

    }
   
   
    public void refreshComments() {
        Charset latin1 = Charset.forName("ISO-8859-1");
        CharsetDecoder decoder = null;
        if (latin1 != null)
            decoder = latin1.newDecoder();
        ImageIcon hostIcon = _map.getIcon_Vertex(_h.getName()).getOriginal();
        if (hostIcon != null) {
            jLblHostImage.setIcon(hostIcon);
        }
 
        TableColumn col = jTableComments.getColumnModel().getColumn(0);
        col.setPreferredWidth(30);
        col = jTableComments.getColumnModel().getColumn(3);
        col.setPreferredWidth(70);
        col = jTableComments.getColumnModel().getColumn(2);
        col.setPreferredWidth(50);
       
        if (_h != null) {
            _hcomments = _map.getHostComments(_h.getName());
            if (_hcomments != null) {
               DefaultTableModel model = (DefaultTableModel)jTableComments.getModel();
//               jTableComments.getColumn("Delete").setCellEditor(new DefaultCellEditor(jChkDeleteComment));
//               jTableComments.getColumn("Delete").setCellRenderer(deleteCommentRenderer);
               int rows = jTableComments.getRowCount();
               int sindex = -1;
               int eindex = -1;
               for (int i = 0; i < rows; i++)
                   model.removeRow(0);
               for (StatHostComment shc: _hcomments) {
                   sindex = -1;
                   eindex = -1;
                   Vector vector = new Vector();
                   vector.add(shc.getCommentId());
                   if (shc.getCommentData().toLowerCase().startsWith("issue <a href")) {
                       sindex = shc.getCommentData().indexOf("\">");
                       if (sindex > 0)
                           eindex = shc.getCommentData().indexOf("<", sindex);
                       if (sindex > 0 && eindex > sindex)
                           vector.add("Issue "  +  shc.getCommentData().substring(sindex + 2,eindex));
                       else {
                           if (decoder != null) {
                               try {
                                   vector.add(decoder.decode(ByteBuffer.wrap(shc.getCommentData().getBytes())).toString());
                               } catch (CharacterCodingException ex) {
                                   // let's not give up insert the comment data as is
                                   vector.add(shc.getCommentData());
                               }
                           } else {
                               vector.add(shc.getCommentData());
                               System.out.println("adding default");
                           }
                       }
                   } else {
                        if (decoder != null) {
                                try {
                                    vector.add(decoder.decode(ByteBuffer.wrap(shc.getCommentData().getBytes())).toString());
                                } catch (CharacterCodingException ex) {
                                    // let's not give up insert the comment data as is
                                    vector.add(shc.getCommentData());
                                }
                           } else {
View Full Code Here

        }
    }

    private CharsetDecoder charsetDecoder(IoSession session) {
        // convert to string using the charset decoder
        CharsetDecoder decoder = (CharsetDecoder)session.getAttribute(CHARSET_DECODER);
        if (decoder == null) {
            decoder = config.getCharset().newDecoder();
            session.setAttribute(CHARSET_DECODER, decoder);
        }
        return decoder;
View Full Code Here

    }

    @Test
    public void testGetString() throws Exception {
        IoBuffer buf = IoBuffer.allocate(16);
        CharsetDecoder decoder;

        Charset charset = Charset.forName("UTF-8");
        buf.clear();
        buf.putString("hello", charset.newEncoder());
        buf.put((byte) 0);
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.