Package java.nio.charset

Examples of java.nio.charset.CharsetDecoder


      @Override
      public Object answer()
          throws Throwable
      {
        Charset charset = Charset.forName("UTF-8");
        CharsetDecoder decoder = charset.newDecoder();
        responseStr.append(decoder.decode((ByteBuffer)EasyMock.getCurrentArguments()[0]));
        return responseStr.length();
      }
    });
    EasyMock.replay(chunkedWritableByteChannel);
View Full Code Here


      @Override
      public Object answer()
          throws Throwable
      {
        Charset charset = Charset.forName("UTF-8");
        CharsetDecoder decoder = charset.newDecoder();
        responseStr.append(decoder.decode((ByteBuffer)EasyMock.getCurrentArguments()[0]));
        return responseStr.length();
      }
    });
    EasyMock.replay(chunkedWritableByteChannel);
View Full Code Here

        return dst;
    }

    public static String decodeString(ByteBuffer src, Charset charset)
    {
        final CharsetDecoder decoder = 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) {
View Full Code Here

        if (charset == null) {
            throw new NullPointerException("charset");
        }

        Map<Charset, CharsetDecoder> map = decoders.get();
        CharsetDecoder d = map.get(charset);
        if (d != null) {
            d.reset();
            d.onMalformedInput(CodingErrorAction.REPLACE);
            d.onUnmappableCharacter(CodingErrorAction.REPLACE);
            return d;
        }

        d = charset.newDecoder();
        d.onMalformedInput(CodingErrorAction.REPLACE);
        d.onUnmappableCharacter(CodingErrorAction.REPLACE);
        map.put(charset, d);
        return d;
    }
View Full Code Here

    return decode(ByteBuffer.wrap(utf8, start, length), replace);
  }
 
  private static String decode(ByteBuffer utf8, boolean replace)
    throws CharacterCodingException {
    CharsetDecoder decoder = DECODER_FACTORY.get();
    if (replace) {
      decoder.onMalformedInput(
          java.nio.charset.CodingErrorAction.REPLACE);
      decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    }
    String str = decoder.decode(utf8).toString();
    // set decoder back to its default value: REPORT
    if (replace) {
      decoder.onMalformedInput(CodingErrorAction.REPORT);
      decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    }
    return str;
  }
View Full Code Here

            {
                return "";
            }
            else
            {
                CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
                ByteBuffer encodedString = _data.slice();
                encodedString.limit(length);
                _data.position(_data.position()+length);
                CharBuffer string = decoder.decode(encodedString.buf());
               
                return string.toString();
            }

View Full Code Here

   * @param stream the stream to wrap in a reader
   * @param charSet the expected charset
   * @return a wrapping reader
   */
  public static Reader getDecodingReader(InputStream stream, Charset charSet) {
    final CharsetDecoder charSetDecoder = charSet.newDecoder()
        .onMalformedInput(CodingErrorAction.REPORT)
        .onUnmappableCharacter(CodingErrorAction.REPORT);
    return new BufferedReader(new InputStreamReader(stream, charSetDecoder));
  }
View Full Code Here

    for (int i = 0; i < n; i++) {
      random().nextBytes(buffer);
      int size = 1 + random().nextInt(50);
      // This test is turning random bytes into a string,
      // this is asking for trouble.
      CharsetDecoder decoder = IOUtils.CHARSET_UTF_8.newDecoder()
          .onUnmappableCharacter(CodingErrorAction.REPLACE)
          .onMalformedInput(CodingErrorAction.REPLACE);
      String s = decoder.decode(ByteBuffer.wrap(buffer, 0, size)).toString();
      array.append(s);
      builder.append(s);
    }

    for (int i = 0; i < n; i++) {
      random().nextBytes(buffer);
      int size = 1 + random().nextInt(50);
      // This test is turning random bytes into a string,
      // this is asking for trouble.
      CharsetDecoder decoder = IOUtils.CHARSET_UTF_8.newDecoder()
          .onUnmappableCharacter(CodingErrorAction.REPLACE)
          .onMalformedInput(CodingErrorAction.REPLACE);
      String s = decoder.decode(ByteBuffer.wrap(buffer, 0, size)).toString();
      array.append((CharSequence)s);
      builder.append(s);
    }

    for (int i = 0; i < n; i++) {
      random().nextBytes(buffer);
      int size = 1 + random().nextInt(50);
      // This test is turning random bytes into a string,
      // this is asking for trouble.
      CharsetDecoder decoder = IOUtils.CHARSET_UTF_8.newDecoder()
          .onUnmappableCharacter(CodingErrorAction.REPLACE)
          .onMalformedInput(CodingErrorAction.REPLACE);
      String s = decoder.decode(ByteBuffer.wrap(buffer, 0, size)).toString();
      for (int j = 0; j < s.length(); j++) {
        array.append(s.charAt(j));
      }
      builder.append(s);
    }
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

      random.nextBytes(buffer);
      int size = 1 + random.nextInt(buffer.length);

      // This test is turning random bytes into a string,
      // this is asking for trouble.
      CharsetDecoder decoder = IOUtils.CHARSET_UTF_8.newDecoder()
          .onUnmappableCharacter(CodingErrorAction.REPLACE)
          .onMalformedInput(CodingErrorAction.REPLACE);
      uniqueValues[i] = decoder.decode(ByteBuffer.wrap(buffer, 0, size)).toString();
      // we cannot have empty path components, so eliminate all prefix as well
      // as middle consecutive delimiter chars.
      uniqueValues[i] = uniqueValues[i].replaceAll("/+", "/");
      if (uniqueValues[i].startsWith("/")) {
        uniqueValues[i] = uniqueValues[i].substring(1);
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.