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

下面列出了怎么用java.nio.charset.CharsetDecoder的API类实例代码及写法,或者点击链接到github查看源代码。


public static Object isCodepage( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {
  boolean bRC = false;
  if ( ArgList.length == 2 ) {
    try {
      if ( isNull( ArgList, new int[] { 0, 1 } ) ) {
        return null;
      } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {
        return Context.getUndefinedValue();
      }
      String strValueToCheck = Context.toString( ArgList[0] );
      String strCodePage = Context.toString( ArgList[1] );
      byte[] bytearray = strValueToCheck.getBytes();
      CharsetDecoder d = Charset.forName( strCodePage ).newDecoder();
      CharBuffer r = d.decode( ByteBuffer.wrap( bytearray ) );
      r.toString();
      bRC = true;
    } catch ( Exception e ) {
      bRC = false;
    }
  } else {
    throw Context.reportRuntimeError( "The function call isCodepage requires 2 arguments." );
  }
  return Boolean.valueOf( bRC );
}
 

/**
 * Utility method to find a CharsetDecoder in the
 * cache or create a new one if necessary.  Throws an
 * INTERNAL if the code set is unknown.
 */
protected CharsetDecoder getConverter(String javaCodeSetName) {

    CharsetDecoder result = null;
    try {
        result = cache.getByteToCharConverter(javaCodeSetName);

        if (result == null) {
            Charset tmpCharset = Charset.forName(javaCodeSetName);
            result = tmpCharset.newDecoder();
            cache.setConverter(javaCodeSetName, result);
        }

    } catch(IllegalCharsetNameException icne) {
        // This can only happen if one of our charset entries has
        // an illegal name.
        throw wrapper.invalidBtcConverterName( icne, javaCodeSetName ) ;
    }

    return result;
}
 
源代码3 项目: qpid-proton-j   文件: StringType.java

@Override
public String decode(DecoderImpl decoder, final ReadableBuffer buffer)
{
    CharsetDecoder charsetDecoder = decoder.getCharsetDecoder();
    try
    {
        return buffer.readString(charsetDecoder);
    }
    catch (CharacterCodingException e)
    {
        throw new IllegalArgumentException("Cannot parse String", e);
    }
    finally
    {
        charsetDecoder.reset();
    }
}
 
源代码4 项目: dragonwell8_jdk   文件: 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);
    // UTF-8 only for now. Other ArrayDeocder only handles
    // CodingErrorAction.REPLACE mode. ZipCoder uses
    // REPORT mode.
    if (isUTF8 && cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder)cd).decode(ba, 0, length, ca);
        if (clen == -1)    // malformed
            throw new IllegalArgumentException("MALFORMED");
        return new String(ca, 0, clen);
    }
    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());
}
 
源代码5 项目: uavstack   文件: JSON.java

@SuppressWarnings("unchecked")
public static <T> T parseObject(byte[] input, //
                                int off, //
                                int len, //
                                CharsetDecoder charsetDecoder, //
                                Type clazz, //
                                Feature... features) {
    charsetDecoder.reset();

    int scaleLength = (int) (len * (double) charsetDecoder.maxCharsPerByte());
    char[] chars = allocateChars(scaleLength);

    ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len);
    CharBuffer charByte = CharBuffer.wrap(chars);
    IOUtils.decode(charsetDecoder, byteBuf, charByte);

    int position = charByte.position();

    return (T) parseObject(chars, position, clazz, features);
}
 
源代码6 项目: p4ic4idea   文件: UnicodeHelper.java

/**
 * Try to determine whether a byte buffer's character encoding is that of the
 * passed-in charset. Uses inefficient
 * heuristics that will be revisited when we're more familiar with likely
 * usage patterns.
 * 
 * Note this has been heavily changed since inception and will
 * almost certainly disappear in the 10.x timeframe -- HR.
 */
public static boolean inferCharset(byte[] bytes, int bytesRead, Charset clientCharset) {
	ByteBuffer byteBuf = ByteBuffer.wrap(bytes, 0, bytesRead);
	CharBuffer charBuf = CharBuffer.allocate(byteBuf.capacity() * 2);
	
	if (clientCharset != null) {
		CharsetDecoder decoder = clientCharset.newDecoder();
		decoder.onMalformedInput(CodingErrorAction.REPORT);
		decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
		CoderResult coderResult = decoder.decode(byteBuf, charBuf, false);
		if (coderResult != null) {
			if (coderResult.isError()) {
				// Wasn't this one...
				return false;
			} else {
				return true;	// Still only *probably* true, dammit...
			}
		}
	}
	
	return true;
}
 

@Override
public void inform(ResourceLoader loader) throws IOException {
  if (userDictionaryPath != null) {
    try (InputStream stream = loader.openResource(userDictionaryPath)) {
      String encoding = userDictionaryEncoding;
      if (encoding == null) {
        encoding = IOUtils.UTF_8;
      }
      CharsetDecoder decoder = Charset.forName(encoding).newDecoder()
        .onMalformedInput(CodingErrorAction.REPORT)
        .onUnmappableCharacter(CodingErrorAction.REPORT);
      Reader reader = new InputStreamReader(stream, decoder);
      userDictionary = UserDictionary.open(reader);
    }
  } else {
    userDictionary = null;
  }
}
 
源代码8 项目: getty   文件: CharsetUtil.java

/**
 * Returns a cached thread-local {@link CharsetDecoder} for the specified {@link Charset}.
 *
 * @param charset The specified charset
 * @return The decoder for the specified {@code charset}
 */
public static CharsetDecoder decoder(Charset charset) {
    checkNotNull(charset, "charset");

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

    d = decoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
    map.put(charset, d);
    return d;
}
 
源代码9 项目: openjdk-jdk9   文件: IBM942C.java

public CharsetDecoder newDecoder() {
    return new DoubleByte.Decoder(this,
                                  IBM942.b2c,
                                  b2cSB,
                                  0x40,
                                  0xfc);
}
 
源代码10 项目: database   文件: ConfigFromImpl.java

@Override
public ConfigFrom propertyFile(CharsetDecoder decoder, String... filenames) {
  for (String filename : filenames) {
    if (filename != null) {
      propertyFile(decoder, new File(filename));
    }
  }
  return this;
}
 
源代码11 项目: SI   文件: Message.java

public String getPayloadTracingString() {
	if (null == payload || 0 == payload.length)
		return "no payload";
	boolean text = true;
	for (byte b:payload) {
		if (' ' > b) {
			switch(b) {
			case '\t':
			case '\n':
			case '\r':
				continue;
			}
			text = false;
			break;
		}
	}
	if (text) {
		CharsetDecoder decoder = CoAP.UTF8_CHARSET.newDecoder();
		decoder.onMalformedInput(CodingErrorAction.REPORT);
		decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
		ByteBuffer in = ByteBuffer.wrap(payload);
		CharBuffer out = CharBuffer.allocate(24);
		CoderResult result = decoder.decode(in, out, true);
		decoder.flush(out);
		out.flip();
		if (CoderResult.OVERFLOW == result) {
			return "\"" + out +  "\".. " + payload.length + " bytes";
		} else if (!result.isError()){
			return "\"" + out + "\"" ;
		}
	}
	return Utils.toHexText(payload, 256);
}
 
源代码12 项目: openjdk-8-source   文件: ZipCoder.java

private CharsetDecoder decoder() {
    if (dec == null) {
        dec = cs.newDecoder()
          .onMalformedInput(CodingErrorAction.REPORT)
          .onUnmappableCharacter(CodingErrorAction.REPORT);
    }
    return dec;
}
 
源代码13 项目: jdk8u-jdk   文件: ZipCoder.java

private CharsetDecoder decoder() {
    if (dec == null) {
        dec = cs.newDecoder()
          .onMalformedInput(CodingErrorAction.REPORT)
          .onUnmappableCharacter(CodingErrorAction.REPORT);
    }
    return dec;
}
 
源代码14 项目: yajsync   文件: TextDecoder.java

public static TextDecoder newStrict(Charset charset)
{
    CharsetDecoder encoder = charset.newDecoder().
        onMalformedInput(CodingErrorAction.REPORT).
        onUnmappableCharacter(CodingErrorAction.REPORT);
    TextDecoder instance = new TextDecoder(encoder);
    return instance;
}
 
源代码15 项目: openjdk-jdk9   文件: IBM949C.java

public CharsetDecoder newDecoder() {
    return new DoubleByte.Decoder(this,
                                  IBM949.b2c,
                                  b2cSB,
                                  0xa1,
                                  0xfe);
}
 
源代码16 项目: openjdk-jdk9   文件: CodeSetCache.java

/**
 * Stores the given CharsetDecoder in the thread local cache,
 * and returns the same converter.
 */
CharsetDecoder setConverter(Object key, CharsetDecoder converter) {
    Map btcMap = ((Map[])converterCaches.get())[BTC_CACHE_MAP];

    btcMap.put(key, converter);

    return converter;
}
 
源代码17 项目: secretshare   文件: BigIntUtilities.java

public static boolean isValidUTF8(byte[] input)
{

    CharsetDecoder cs = Charset.forName(UTF8).newDecoder();

    try
    {
        cs.decode(ByteBuffer.wrap(input));
        return true;
    }
    catch (CharacterCodingException e)
    {
        return false;
    }
}
 
源代码18 项目: jdk8u-jdk   文件: NIOJISAutoDetectTest.java

static void test(String expectedCharset, byte[] input) throws Exception {
    Charset cs = Charset.forName("x-JISAutoDetect");
    CharsetDecoder autoDetect = cs.newDecoder();

    Charset cs2 = Charset.forName(expectedCharset);
    CharsetDecoder decoder = cs2.newDecoder();

    ByteBuffer bb = ByteBuffer.allocate(128);
    CharBuffer charOutput = CharBuffer.allocate(128);
    CharBuffer charExpected = CharBuffer.allocate(128);

    bb.put(input);
    bb.flip();
    bb.mark();

    CoderResult result = autoDetect.decode(bb, charOutput, true);
    checkCoderResult(result);
    charOutput.flip();
    String actual = charOutput.toString();

    bb.reset();

    result = decoder.decode(bb, charExpected, true);
    checkCoderResult(result);
    charExpected.flip();
    String expected = charExpected.toString();

    check(actual.equals(expected),
          String.format("actual=%s expected=%s", actual, expected));
}
 
源代码19 项目: rxjava-extras   文件: StringsTest.java

@Test
public void testPropagateErrorInTheMiddleOfMultibyte() {
    Observable<byte[]> src = Observable.just(new byte[] { (byte) 0xc2 });
    Observable<byte[]> err = Observable.error(new IOException());
    CharsetDecoder charsetDecoder = Charset.forName("UTF-8").newDecoder();
    try {
        decode(Observable.concat(src, err), charsetDecoder).toList().toBlocking().single();
        fail();
    } catch (RuntimeException e) {
        assertEquals(IOException.class, e.getCause().getClass());
    }
}
 
源代码20 项目: JDKSourceCode1.8   文件: CodeSetCache.java

/**
 * Stores the given CharsetDecoder in the thread local cache,
 * and returns the same converter.
 */
CharsetDecoder setConverter(Object key, CharsetDecoder converter) {
    Map btcMap = ((Map[])converterCaches.get())[BTC_CACHE_MAP];

    btcMap.put(key, converter);

    return converter;
}
 
源代码21 项目: jdk8u-jdk   文件: IBM943C.java

public CharsetDecoder newDecoder() {
    return new DoubleByte.Decoder(this,
                                  IBM943.b2c,
                                  b2cSB,
                                  0x40,
                                  0xfc);
}
 
源代码22 项目: jdk8u-jdk   文件: 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;
}
 
源代码23 项目: hottub   文件: MS950_HKSCS_XP.java

public CharsetDecoder newDecoder() {
    return new Decoder(this);
}
 
源代码24 项目: jdk8u-jdk   文件: IBM942C_OLD.java

public CharsetDecoder newDecoder() {
    return new Decoder(this);
}
 
源代码25 项目: jdk8u-jdk   文件: ISO2022_CN_GB.java

public CharsetDecoder newDecoder() {
    return new ISO2022_CN.Decoder(this);
}
 
源代码26 项目: jdk8u-dev-jdk   文件: JIS_X_0201_OLD.java

public CharsetDecoder newDecoder() {
    return new Decoder(this);
}
 
源代码27 项目: jdk8u-jdk   文件: EUC_JP.java

public CharsetDecoder newDecoder() {
    return new Decoder(this);
}
 
源代码28 项目: hottub   文件: PCK_OLD.java

public CharsetDecoder newDecoder() {
    return new Decoder(this);
}
 
源代码29 项目: jdk8u_jdk   文件: MS932_0213.java

public CharsetDecoder newDecoder() {
    return new Decoder(this);
}
 
源代码30 项目: hottub   文件: MS932_OLD.java

public CharsetDecoder newDecoder() {
    return new Decoder(this);
}