Java Code Examples for java.nio.charset.CharsetDecoder#decode()

The following examples show how to use java.nio.charset.CharsetDecoder#decode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnicodeHelper.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: ScriptAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static Object isCodepage( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
  Object 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 undefinedValue;
      }
      String strValueToCheck = (String) ArgList[0];
      String strCodePage = (String) 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 new RuntimeException( "The function call isCodepage requires 2 arguments." );
  }
  return Boolean.valueOf( bRC );
}
 
Example 3
Source File: NIOJISAutoDetectTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 4
Source File: ZipCoder.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
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());
}
 
Example 5
Source File: ZipCoder.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 6
Source File: ZipCoder.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 7
Source File: UnicodeHelper.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: ZipCoder.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 9
Source File: ZipCoder.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example 10
Source File: IOUtils.java    From uavstack with Apache License 2.0 6 votes vote down vote up
public static void decode(CharsetDecoder charsetDecoder, ByteBuffer byteBuf, CharBuffer charByte) {
    try {
        CoderResult cr = charsetDecoder.decode(byteBuf, charByte, true);

        if (!cr.isUnderflow()) {
            cr.throwException();
        }

        cr = charsetDecoder.flush(charByte);

        if (!cr.isUnderflow()) {
            cr.throwException();
        }
    } catch (CharacterCodingException x) {
        // Substitution is always enabled,
        // so this shouldn't happen
        throw new JSONException("utf8 decode error, " + x.getMessage(), x);
    }
}
 
Example 11
Source File: NIOJISAutoDetectTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static String detectingCharset(byte[] bytes) throws Exception {
    //----------------------------------------------------------------
    // Test special public methods of CharsetDecoder while we're here
    //----------------------------------------------------------------
    CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
    check(cd.isAutoDetecting(), "isAutodecting()");
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
    check(! cd.isCharsetDetected(), "isCharsetDetected");
    try {
        cd.detectedCharset();
        fail("no IllegalStateException");
    } catch (IllegalStateException e) {}
    cd.decode(ByteBuffer.wrap(bytes));
    check(cd.isCharsetDetected(), "isCharsetDetected");
    Charset cs = cd.detectedCharset();
    check(cs != null, "cs != null");
    check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
    return cs.name();
}
 
Example 12
Source File: AuthenticationServlet.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private LoginContext login(BufferedReader body) throws IOException, LoginException {
  try {
    Subject subject = new Subject();

    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);

    LoginContext context = new LoginContext("Wave", subject, callbackHandler, configuration);

    // If authentication fails, login() will throw a LoginException.
    context.login();
    return context;
  } catch (CharacterCodingException cce) {
    throw new LoginException("Character coding exception (not utf-8): "
        + cce.getLocalizedMessage());
  } catch (UnsupportedEncodingException uee) {
    throw new LoginException("ad character encoding specification: " + uee.getLocalizedMessage());
  }
}
 
Example 13
Source File: StringUtil.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
public static String utf8ToString(byte[] utf8Data, int offset, int length)
    throws CharacterCodingException
{
    // NOTE: Using the String(..., UTF8) constructor would be wrong.  That method will
    // ignore UTF-8 errors in the input.
    CharsetDecoder decoder = UTF8.newDecoder();
    CharBuffer result = decoder.decode(ByteBuffer.wrap(utf8Data, offset, length));
    return result.toString();
}
 
Example 14
Source File: LocaleHelper.java    From pentaho-metadata with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param aString
 * @param charsetTarget
 * @return true if the provided string is completely within the target character set.
 */
public static boolean isWithinCharset( String aString, String charsetTarget ) {
  byte[] stringBytes = aString.getBytes();
  CharsetDecoder decoder = Charset.forName( charsetTarget ).newDecoder();
  try {
    decoder.decode( ByteBuffer.wrap( stringBytes ) );
    return true;
  } catch ( CharacterCodingException ignored ) {
    //ignored
  }
  return false;
}
 
Example 15
Source File: NIOJISAutoDetectTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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));
}
 
Example 16
Source File: IOUtils.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
private static CoderResult decode(CharBuffer charBuffer, ByteBuffer buffer) {
    final CharsetDecoder charsetDecoder = threadLocalCharsetDecoder.get();
    try {
        final CoderResult coderResult = charsetDecoder.decode(buffer, charBuffer, true);
        charsetDecoder.flush(charBuffer);
        return coderResult;
    } finally {
        ((Buffer) buffer).clear();
        charsetDecoder.reset();
    }
}
 
Example 17
Source File: BufferToCharsKt.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void decodeString(CharsetDecoder decoder, ByteBuffer src, CharBuffer dst) {
  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);
  }
}
 
Example 18
Source File: ValidationUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static boolean isValidUTF8( byte[] input ) {

	    CharsetDecoder cs = Charset.forName("UTF-8").newDecoder();

	    try {
	        cs.decode(ByteBuffer.wrap(input));
	        return true;
	    }
	    catch(CharacterCodingException e){
	        return false;
	    }       
	}
 
Example 19
Source File: NIOJISAutoDetectTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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));
}
 
Example 20
Source File: StringCoding.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
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);
    }
}