Java 类java.nio.charset.CharsetDecoder 实例源码

项目:OpenJSharp    文件:ZipCoder.java   
String toString(byte[] ba, int length) {
    CharsetDecoder cd = decoder().reset();
    int len = (int)(length * cd.maxCharsPerByte());
    char[] ca = new char[len];
    if (len == 0)
        return new String(ca);
    ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = cd.decode(bb, cb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = cd.flush(cb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    return new String(ca, 0, cb.position());
}
项目:incubator-netbeans    文件:LineReader.java   
LineReader(CharsetDecoder decoder, InputStream stream) throws IOException {

        isr = new InputStreamReader(stream, decoder);
        try {
            br = new BufferedReader(isr);
        } catch (Throwable t) {
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
            throw new IOException(t);
        }
    }
项目:incubator-netbeans    文件:DatabaseConnectionConvertor.java   
static String decodePassword(byte[] bytes) throws CharacterCodingException {
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder(); // NOI18N
    ByteBuffer input = ByteBuffer.wrap(bytes);
    int outputLength = (int)(bytes.length * (double)decoder.maxCharsPerByte());
    if (outputLength == 0) {
        return ""; // NOI18N
    }
    char[] chars = new char[outputLength];
    CharBuffer output = CharBuffer.wrap(chars);
    CoderResult result = decoder.decode(input, output, true);
    if (!result.isError() && !result.isOverflow()) {
        result = decoder.flush(output);
    }
    if (result.isError() || result.isOverflow()) {
        throw new CharacterCodingException();
    } else {
        return new String(chars, 0, output.position());
    }
}
项目:hadoop-oss    文件:Text.java   
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;
}
项目:FirefoxData-android    文件:SessionInputBufferImpl.java   
/**
 * Creates new instance of SessionInputBufferImpl.
 *
 * @param metrics HTTP transport metrics.
 * @param buffersize buffer size. Must be a positive number.
 * @param minChunkLimit size limit below which data chunks should be buffered in memory
 *   in order to minimize native method invocations on the underlying network socket.
 *   The optimal value of this parameter can be platform specific and defines a trade-off
 *   between performance of memory copy operations and that of native method invocation.
 *   If negative default chunk limited will be used.
 * @param constraints Message constraints. If <code>null</code>
 *   {@link MessageConstraints#DEFAULT} will be used.
 * @param chardecoder chardecoder to be used for decoding HTTP protocol elements.
 *   If <code>null</code> simple type cast will be used for byte to char conversion.
 */
public SessionInputBufferImpl(
        final HttpTransportMetricsImpl metrics,
        final int buffersize,
        final int minChunkLimit,
        final MessageConstraints constraints,
        final CharsetDecoder chardecoder) {
    Args.notNull(metrics, "HTTP transport metrcis");
    Args.positive(buffersize, "Buffer size");
    this.metrics = metrics;
    this.buffer = new byte[buffersize];
    this.bufferpos = 0;
    this.bufferlen = 0;
    this.minChunkLimit = minChunkLimit >= 0 ? minChunkLimit : 512;
    this.constraints = constraints != null ? constraints : MessageConstraints.DEFAULT;
    this.linebuffer = new ByteArrayBuffer(buffersize);
    this.decoder = chardecoder;
}
项目:FirefoxData-android    文件:BHttpConnectionBase.java   
/**
 * Creates new instance of BHttpConnectionBase.
 *
 * @param buffersize buffer size. Must be a positive number.
 * @param fragmentSizeHint fragment size hint.
 * @param chardecoder decoder to be used for decoding HTTP protocol elements.
 *   If <code>null</code> simple type cast will be used for byte to char conversion.
 * @param charencoder encoder to be used for encoding HTTP protocol elements.
 *   If <code>null</code> simple type cast will be used for char to byte conversion.
 * @param constraints Message constraints. If <code>null</code>
 *   {@link MessageConstraints#DEFAULT} will be used.
 * @param incomingContentStrategy incoming content length strategy. If <code>null</code>
 *   {@link LaxContentLengthStrategy#INSTANCE} will be used.
 * @param outgoingContentStrategy outgoing content length strategy. If <code>null</code>
 *   {@link StrictContentLengthStrategy#INSTANCE} will be used.
 */
protected BHttpConnectionBase(
        final int buffersize,
        final int fragmentSizeHint,
        final CharsetDecoder chardecoder,
        final CharsetEncoder charencoder,
        final MessageConstraints constraints,
        final ContentLengthStrategy incomingContentStrategy,
        final ContentLengthStrategy outgoingContentStrategy) {
    super();
    Args.positive(buffersize, "Buffer size");
    final HttpTransportMetricsImpl inTransportMetrics = new HttpTransportMetricsImpl();
    final HttpTransportMetricsImpl outTransportMetrics = new HttpTransportMetricsImpl();
    this.inbuffer = new SessionInputBufferImpl(inTransportMetrics, buffersize, -1,
            constraints != null ? constraints : MessageConstraints.DEFAULT, chardecoder);
    this.outbuffer = new SessionOutputBufferImpl(outTransportMetrics, buffersize, fragmentSizeHint,
            charencoder);
    this.connMetrics = new HttpConnectionMetricsImpl(inTransportMetrics, outTransportMetrics);
    this.incomingContentStrategy = incomingContentStrategy != null ? incomingContentStrategy :
        LaxContentLengthStrategy.INSTANCE;
    this.outgoingContentStrategy = outgoingContentStrategy != null ? outgoingContentStrategy :
        StrictContentLengthStrategy.INSTANCE;
}
项目:lams    文件:Dictionary.java   
/**
 * Retrieves the CharsetDecoder for the given encoding.  Note, This isn't perfect as I think ISCII-DEVANAGARI and
 * MICROSOFT-CP1251 etc are allowed...
 *
 * @param encoding Encoding to retrieve the CharsetDecoder for
 * @return CharSetDecoder for the given encoding
 */
private CharsetDecoder getJavaEncoding(String encoding) {
  if ("ISO8859-14".equals(encoding)) {
    return new ISO8859_14Decoder();
  }
  String canon = CHARSET_ALIASES.get(encoding);
  if (canon != null) {
    encoding = canon;
  }
  Charset charset = Charset.forName(encoding);
  return charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE);
}
项目:boohee_v5.6    文件:c.java   
public static String a(ByteBuffer byteBuffer) {
    CharsetDecoder newDecoder = Charset.forName("UTF8").newDecoder();
    newDecoder.onMalformedInput(a);
    newDecoder.onUnmappableCharacter(a);
    try {
        byteBuffer.mark();
        String charBuffer = newDecoder.decode(byteBuffer).toString();
        byteBuffer.reset();
        return charBuffer;
    } catch (Throwable e) {
        throw new b((int) CloseFrame.NO_UTF8, e);
    }
}
项目:quorrabot    文件:Charsetfunctions.java   
public static String stringUtf8(ByteBuffer bytes) throws InvalidDataException {
    CharsetDecoder decode = Charset.forName("UTF8").newDecoder();
    decode.onMalformedInput(codingErrorAction);
    decode.onUnmappableCharacter(codingErrorAction);
    // decode.replaceWith( "X" );
    String s;
    try {
        bytes.mark();
        s = decode.decode(bytes).toString();
        bytes.reset();
    } catch (CharacterCodingException e) {
        throw new InvalidDataException(CloseFrame.NO_UTF8, e);
    }
    return s;
}
项目:incubator-netbeans    文件:BufferedCharSequence.java   
/**
 * Creates {@code BufferedCharSequence} for specified FileObject {@code fo}.
 *
 * @param fo FileObject containing character data.
 * @param decoder Charset decoder.
 * @param size file size.
 */
public BufferedCharSequence(final FileObject fo,
        CharsetDecoder decoder, long size) {
    this.source = new Source(fo, size);
    this.decoder = decoder;
    this.sink = new Sink(this.source);
    LOG.log(Level.FINER, "<init> {0}; decoder = {1}; {2}", // NOI18N
            new Object[]{this.source, this.decoder, this.sink});
}
项目:OpenJSharp    文件:ZipCoder.java   
private CharsetDecoder decoder() {
    if (dec == null) {
        dec = cs.newDecoder()
          .onMalformedInput(CodingErrorAction.REPORT)
          .onUnmappableCharacter(CodingErrorAction.REPORT);
    }
    return dec;
}
项目:boohee_v5.6    文件:ThreadLocalCache.java   
public static CharsetDecoder getUTF8Decoder() {
    CharsetDecoder decoder = (CharsetDecoder) decoderLocal.get();
    if (decoder != null) {
        return decoder;
    }
    decoder = new UTF8Decoder();
    decoderLocal.set(decoder);
    return decoder;
}
项目:incubator-netbeans    文件:FileEncodingQueryTest.java   
private String getRealCharsetName (CharsetDecoder decoder) {
    if (decoder instanceof ContentBaseEncodingQuery.SniffingDecoder) {
        decoder = ((ContentBaseEncodingQuery.SniffingDecoder)decoder).decoder;
        assertNotNull(decoder);
    }
    return decoder.charset().name();
}
项目:truevfs    文件:OctetCharsetTestSuite.java   
@Test
public void testRoundTrip() throws CharacterCodingException {
    final CharsetDecoder dec = charset.newDecoder();
    final CharsetEncoder enc = charset.newEncoder();
    final byte[] b1 = new byte[256];
    for (int i = 0; i < b1.length; i++)
        b1[i] = (byte) i;
    final ByteBuffer bb1 = ByteBuffer.wrap(b1);
    final CharBuffer cb = dec.decode(bb1);
    final ByteBuffer bb2 = enc.encode(cb);
    final byte[] b2 = bb2.array();
    assertTrue(Arrays.equals(b1, b2));
}
项目:boohee_v5.6    文件:JSON.java   
public static final <T> T parseObject(byte[] input, int off, int len, CharsetDecoder charsetDecoder, Type clazz, Feature... features) {
    charsetDecoder.reset();
    char[] chars = ThreadLocalCache.getChars((int) (((double) len) * ((double) charsetDecoder.maxCharsPerByte())));
    ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len);
    CharBuffer charByte = CharBuffer.wrap(chars);
    IOUtils.decode(charsetDecoder, byteBuf, charByte);
    return parseObject(chars, charByte.position(), clazz, features);
}
项目:neoscada    文件:StringTransformer.java   
protected Object convert ( final Object value ) throws CharacterCodingException
{
    if ( ! ( value instanceof byte[] ) )
    {
        throw new IllegalArgumentException ( String.format ( "Unable to process data of type: %s. This transformer can only handle byte[]", value.getClass () ) );
    }

    final byte[] data = (byte[])value;
    final CharsetDecoder decoder = this.charset.newDecoder ();
    final CharBuffer result = decoder.decode ( ByteBuffer.wrap ( data ) );
    return result.toString ();
}
项目:defense-solutions-proofs-of-concept    文件:RegexTextInboundAdapter.java   
private CharSequence decode(ByteBuffer buf)
{
    // Decode a byte buffer into a CharBuffer
    CharBuffer isodcb = null;
    CharsetDecoder isodecoder = charset.newDecoder();
    try {
        isodcb = isodecoder.decode(buf);
    } catch (CharacterCodingException e) {
        log.error(e);
    }
    return (CharSequence)isodcb;
}
项目:gretl    文件:FileStylingDefinition.java   
/**
 * Creates an CharsetDecoder which tests the encoding
 *
 * @return      CharsetDecoder
 */
private static CharsetDecoder createCharsetDecoder() {
    Charset charset = Charset.forName(encoding);
    CharsetDecoder decoder = charset.newDecoder();
    decoder.reset();

    return decoder;
}
项目:OpenJSharp    文件:ZipCoder.java   
private CharsetDecoder decoder() {
    CharsetDecoder dec = decTL.get();
    if (dec == null) {
        dec = cs.newDecoder()
          .onMalformedInput(CodingErrorAction.REPORT)
          .onUnmappableCharacter(CodingErrorAction.REPORT);
        decTL.set(dec);
    }
    return dec;
}
项目:jdk8u-jdk    文件:MSISO2022JP.java   
public CharsetDecoder newDecoder() {
    return new Decoder(this, CoderHolder.DEC0208, null);
}
项目:OpenJSharp    文件:IBM1098.java   
public CharsetDecoder newDecoder() {
    return new SingleByte.Decoder(this, b2c);
}
项目:openjdk-jdk10    文件:PathFileObject.java   
@Override @DefinedBy(Api.COMPILER)
public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
    CharsetDecoder decoder = fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
    return new InputStreamReader(openInputStream(), decoder);
}
项目:GitHub    文件:IBM437.java   
public CharsetDecoder newDecoder() {
    return new Decoder(this);
}
项目:openjdk-jdk10    文件:MS932DB.java   
public CharsetDecoder newDecoder() {
    return new Decoder(this);
}
项目:OpenJSharp    文件:UTF_32LE.java   
public CharsetDecoder newDecoder() {
    return new UTF_32Coder.Decoder(this, UTF_32Coder.LITTLE);
}
项目:openjdk-jdk10    文件:IBM943C_OLD.java   
public CharsetDecoder newDecoder() {
    return new Decoder(this);
}
项目:OpenJSharp    文件:SJIS.java   
public CharsetDecoder newDecoder() {
    initb2c();
    return new  DoubleByte.Decoder(this, b2c, b2cSB, 0x40, 0xfc);
}
项目:openjdk-jdk10    文件:X11SunUnicode_0.java   
public CharsetDecoder newDecoder() {
    throw new Error("Decoder is not implemented for X11SunUnicode_0 Charset");
}
项目:openjdk-jdk10    文件:EUC_CN_OLD.java   
public CharsetDecoder newDecoder() {
    return new Decoder(this);
}
项目:OpenJSharp    文件:IBM852.java   
public CharsetDecoder newDecoder() {
    return new SingleByte.Decoder(this, b2c);
}
项目:OpenJSharp    文件:IBM858.java   
public CharsetDecoder newDecoder() {
    return new SingleByte.Decoder(this, b2c);
}
项目:jdk8u-jdk    文件:JIS_X_0208_OLD.java   
public CharsetDecoder newDecoder() {
    return new Decoder(this);
}
项目:spark_deep    文件:Text.java   
protected CharsetDecoder initialValue() {
  return Charset.forName("UTF-8").newDecoder().
         onMalformedInput(CodingErrorAction.REPORT).
         onUnmappableCharacter(CodingErrorAction.REPORT);
}
项目:openjdk-jdk10    文件:Big5_HKSCS_2001.java   
public CharsetDecoder newDecoder() {
    return new Decoder(this);
}
项目:OpenJSharp    文件:IBM285.java   
public CharsetDecoder newDecoder() {
    return new SingleByte.Decoder(this, b2c);
}
项目:jdk8u-jdk    文件:EUC_JP_Open.java   
public CharsetDecoder newDecoder() {
    return new Decoder(this);
}
项目:OpenJSharp    文件:StringCoding.java   
static char[] decode(Charset cs, byte[] ba, int off, int len) {
    // (1)We never cache the "external" cs, the only benefit of creating
    // an additional StringDe/Encoder object to wrap it is to share the
    // de/encode() method. These SD/E objects are short-lifed, the young-gen
    // gc should be able to take care of them well. But the best approash
    // is still not to generate them if not really necessary.
    // (2)The defensive copy of the input byte/char[] has a big performance
    // impact, as well as the outgoing result byte/char[]. Need to do the
    // optimization check of (sm==null && classLoader0==null) for both.
    // (3)getClass().getClassLoader0() is expensive
    // (4)There might be a timing gap in isTrusted setting. getClassLoader0()
    // is only chcked (and then isTrusted gets set) when (SM==null). It is
    // possible that the SM==null for now but then SM is NOT null later
    // when safeTrim() is invoked...the "safe" way to do is to redundant
    // check (... && (isTrusted || SM == null || getClassLoader0())) in trim
    // but it then can be argued that the SM is null when the opertaion
    // is started...
    CharsetDecoder cd = cs.newDecoder();
    int en = scale(len, cd.maxCharsPerByte());
    char[] ca = new char[en];
    if (len == 0)
        return ca;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ba =  Arrays.copyOfRange(ba, off, off + len);
            off = 0;
        }
    }
    cd.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
        return safeTrim(ca, clen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
        CharBuffer cb = CharBuffer.wrap(ca);
        try {
            CoderResult cr = cd.decode(bb, cb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = cd.flush(cb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            // Substitution is always enabled,
            // so this shouldn't happen
            throw new Error(x);
        }
        return safeTrim(ca, cb.position(), cs, isTrusted);
    }
}
项目:incubator-netbeans    文件:ProjectCustomizer.java   
@Override
public CharsetDecoder newDecoder() {
    throw new UnsupportedOperationException();
}
项目:jdk8u-jdk    文件:StringCoding.java   
static char[] decode(Charset cs, byte[] ba, int off, int len) {
    // (1)We never cache the "external" cs, the only benefit of creating
    // an additional StringDe/Encoder object to wrap it is to share the
    // de/encode() method. These SD/E objects are short-lifed, the young-gen
    // gc should be able to take care of them well. But the best approash
    // is still not to generate them if not really necessary.
    // (2)The defensive copy of the input byte/char[] has a big performance
    // impact, as well as the outgoing result byte/char[]. Need to do the
    // optimization check of (sm==null && classLoader0==null) for both.
    // (3)getClass().getClassLoader0() is expensive
    // (4)There might be a timing gap in isTrusted setting. getClassLoader0()
    // is only chcked (and then isTrusted gets set) when (SM==null). It is
    // possible that the SM==null for now but then SM is NOT null later
    // when safeTrim() is invoked...the "safe" way to do is to redundant
    // check (... && (isTrusted || SM == null || getClassLoader0())) in trim
    // but it then can be argued that the SM is null when the opertaion
    // is started...
    CharsetDecoder cd = cs.newDecoder();
    int en = scale(len, cd.maxCharsPerByte());
    char[] ca = new char[en];
    if (len == 0)
        return ca;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ba =  Arrays.copyOfRange(ba, off, off + len);
            off = 0;
        }
    }
    cd.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
        return safeTrim(ca, clen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
        CharBuffer cb = CharBuffer.wrap(ca);
        try {
            CoderResult cr = cd.decode(bb, cb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = cd.flush(cb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            // Substitution is always enabled,
            // so this shouldn't happen
            throw new Error(x);
        }
        return safeTrim(ca, cb.position(), cs, isTrusted);
    }
}
项目:incubator-netbeans    文件:FileEncodingQueryTest.java   
public CharsetDecoder newDecoder() {
    return new SniffingDecoder (this);
}