Package java.nio.charset

Examples of java.nio.charset.CharsetDecoder


    @Test
    public void testGetPrefixedString() throws Exception {
        IoBuffer buf = IoBuffer.allocate(16);
        CharsetEncoder encoder;
        CharsetDecoder decoder;
        encoder = Charset.forName("ISO-8859-1").newEncoder();
        decoder = Charset.forName("ISO-8859-1").newDecoder();

        buf.putShort((short) 3);
        buf.putString("ABCD", encoder);
View Full Code Here


        TEST_CASES.add(testCase);
    }

    @Test
    public void testHarmonyDecoder() {
        CharsetDecoder decoder = new Utf8Decoder();
        for (Utf8TestCase testCase : TEST_CASES) {
            doTest(decoder, testCase, 0);
        }
    }
View Full Code Here

    }


    @Test
    public void testJvmDecoder() {
        CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
        int testCount = 0;
        try {
            for (Utf8TestCase testCase : TEST_CASES) {
                doTest(decoder, testCase, testCase.flagsJvm);
                testCount++;
View Full Code Here

    }

    public void testGetString() throws Exception
    {
        ByteBuffer buf = ByteBuffer.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

    public void testGetPrefixedString() throws Exception
    {
        ByteBuffer buf = ByteBuffer.allocate( 16 );
        CharsetEncoder encoder;
        CharsetDecoder decoder;
        encoder = Charset.forName( "ISO-8859-1" ).newEncoder();
        decoder = Charset.forName( "ISO-8859-1" ).newDecoder();

        buf.putShort( ( short ) 3 );
        buf.putString( "ABCD", encoder );
View Full Code Here

        socket.setTcpNoDelay(this.sconfig.isTcpNoDelay());
        final int linger = this.sconfig.getSoLinger();
        if (linger >= 0) {
            socket.setSoLinger(linger > 0, linger);
        }
        CharsetDecoder chardecoder = null;
        CharsetEncoder charencoder = null;
        final Charset charset = this.cconfig.getCharset();
        final CodingErrorAction malformedInputAction = this.cconfig.getMalformedInputAction() != null ?
                this.cconfig.getMalformedInputAction() : CodingErrorAction.REPORT;
        final CodingErrorAction unmappableInputAction = this.cconfig.getUnmappableInputAction() != null ?
                this.cconfig.getUnmappableInputAction() : CodingErrorAction.REPORT;
        if (charset != null) {
            chardecoder = charset.newDecoder();
            chardecoder.onMalformedInput(malformedInputAction);
            chardecoder.onUnmappableCharacter(unmappableInputAction);
            charencoder = charset.newEncoder();
            charencoder.onMalformedInput(malformedInputAction);
            charencoder.onUnmappableCharacter(unmappableInputAction);
        }
        final DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(
View Full Code Here

      String parametersLine = body.readLine();
      // Throws UnsupportedEncodingException.
      byte[] utf8Bytes = parametersLine.getBytes("UTF-8");

      CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder();
      utf8Decoder.onMalformedInput(CodingErrorAction.IGNORE);
      utf8Decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);

      // Throws CharacterCodingException.
      CharBuffer parsed = utf8Decoder.decode(ByteBuffer.wrap(utf8Bytes));
      parametersLine = parsed.toString();

      MultiMap<String> parameters = new UrlEncoded(parametersLine);
      CallbackHandler callbackHandler = new HttpRequestBasedCallbackHandler(parameters);
View Full Code Here

        FileChannel inChannel = inRandom.getChannel();

        //将输入文件的通道通过只读的权限 映射到内存中。
        MappedByteBuffer byteMapper = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) inFile.length());

        CharsetDecoder inDecoder = inCharset.newDecoder();
        CharsetEncoder outEncoder = outCharset.newEncoder();

        CharBuffer cb = inDecoder.decode(byteMapper);
        ByteBuffer outBuffer = null;
        try {
            outBuffer = outEncoder.encode(cb);

            RandomAccessFile outRandom = null;
View Full Code Here

     * Preallocated CharBuffer for decoding bytes
     */
    private CharBuffer cbuff = CharBuffer.wrap(carray);

    public String readLine(Charset charset, byte delimiter) throws IOException {
        CharsetDecoder decoder = charset.newDecoder();
        decoder.onMalformedInput(CodingErrorAction.REPLACE);
        decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
        int delim = delimiter&0xff;
        int rc;
        int offset = 0;
        StringBuilder sb = null;
        CoderResult res;
        while ((rc = read())!=-1) {
            if (rc == delim) {
                break;
            }
            barray[offset++] = (byte)rc;
            if (barray.length == offset) {
                bbuff.position(0);
                bbuff.limit(barray.length);
                cbuff.position(0);
                cbuff.limit(carray.length);
                res = decoder.decode(bbuff, cbuff, false);
                if (res.isError()) {
                    throw new IOException("Decoding error: " + res.toString());
                }
                offset = bbuff.remaining();
                switch (offset) {
                default:
                    System.arraycopy(barray, bbuff.position(), barray, 0, bbuff
                            .remaining());
                    break;
                case 2:
                    barray[1] = barray[barray.length - 1];
                    barray[0] = barray[barray.length - 2];
                    break;
                case 1:
                    barray[0] = barray[barray.length - 1];
                    break;
                case 0:
                }
                if (sb == null) {
                    sb = new StringBuilder(cbuff.position());
                }
                sb.append(carray, 0, cbuff.position());
            }
        }
        if (sb == null) {
            if (rc == -1 && offset == 0) {
                // We are at EOF with nothing read
                return null;
            }
            sb = new StringBuilder();
        }
        bbuff.position(0);
        bbuff.limit(offset);
        cbuff.position(0);
        cbuff.limit(carray.length);
        res = decoder.decode(bbuff, cbuff, true);
        if (res.isError()) {
            System.out.println("Error");
        }
        sb.append(carray, 0, cbuff.position());
        cbuff.position(0);
        res = decoder.flush(cbuff);
        if (res.isError()) {
            System.out.println("Error");
        }
        sb.append(carray, 0, cbuff.position());
        return sb.toString();
View Full Code Here

  /**
   * Load synonyms with the given {@link SynonymMap.Parser} class.
   */
  private SynonymMap loadSynonyms(ResourceLoader loader, String cname, boolean dedup, Analyzer analyzer) throws IOException, ParseException {
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder()
        .onMalformedInput(CodingErrorAction.REPORT)
        .onUnmappableCharacter(CodingErrorAction.REPORT);

    SynonymMap.Parser parser;
    Class<? extends SynonymMap.Parser> clazz = loader.findClass(cname, SynonymMap.Parser.class);
    try {
      parser = clazz.getConstructor(boolean.class, boolean.class, Analyzer.class).newInstance(dedup, expand, analyzer);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    File synonymFile = new File(synonyms);
    if (synonymFile.exists()) {
      decoder.reset();
      parser.parse(new InputStreamReader(loader.openResource(synonyms), decoder));
    } else {
      List<String> files = splitFileNames(synonyms);
      for (String file : files) {
        decoder.reset();
        parser.parse(new InputStreamReader(loader.openResource(file), decoder));
      }
    }
    return parser.build();
  }
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.