public WriterOutputStream(Writer out) { this.writer = out; decoder = DEFAULT_CHARSET.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); }
private static CharsetDecoder configureReplaceCodingErrorActions(CharsetDecoder decoder) { return decoder.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE); }
InputStream in = ...; CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder(); decoder.onMalformedInput(CodingErrorAction.IGNORE); Reader reader = new InputStreamReader(in, decoder);
/** * Creates charset decoder block. * * @param charset The charset encoding to decode bytes from. */ public CharsetDecoderBlock(Charset charset) { charsetDecoder = charset.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); isEndOfInput = false; leftover = null; }
public String decode(String encodedFolderName) throws CharacterCodingException { CharsetDecoder decoder = modifiedUtf7Charset.newDecoder().onMalformedInput(CodingErrorAction.REPORT); ByteBuffer byteBuffer = ByteBuffer.wrap(encodedFolderName.getBytes(asciiCharset)); CharBuffer charBuffer = decoder.decode(byteBuffer); return charBuffer.toString(); } }
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; }
/** * Returns a new {@link CharsetDecoder} for the {@link Charset} with specified error actions. * * @param charset The specified charset * @param malformedInputAction The decoder's action for malformed-input errors * @param unmappableCharacterAction The decoder's action for unmappable-character errors * @return The decoder for the specified {@code charset} */ public static CharsetDecoder decoder(Charset charset, CodingErrorAction malformedInputAction, CodingErrorAction unmappableCharacterAction) { checkNotNull(charset, "charset"); CharsetDecoder d = charset.newDecoder(); d.onMalformedInput(malformedInputAction).onUnmappableCharacter(unmappableCharacterAction); return d; }
CharsetDecoder decoder = charset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.IGNORE); decoder.reset(); CharBuffer cbuf = decoder.decode(bbuf);
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; }
public XMLLightweightParser(Charset charset) { encoder = charset.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); }
CharsetDecoder UTF8Decoder = Charset.forName("UTF8").newDecoder().onMalformedInput(CodingErrorAction.REPORT);
/** * 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 = InternalThreadLocalMap.get().charsetDecoderCache(); 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; }
public RCFileCat() { super(); decoder = Charset.forName("UTF-8").newDecoder(). onMalformedInput(CodingErrorAction.REPLACE). onUnmappableCharacter(CodingErrorAction.REPLACE); }
FileInputStream input; String result = null; try { input = new FileInputStream(new File("invalid.txt")); CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder(); decoder.onMalformedInput(CodingErrorAction.IGNORE); InputStreamReader reader = new InputStreamReader(input, decoder); BufferedReader bufferedReader = new BufferedReader( reader ); StringBuilder sb = new StringBuilder(); String line = bufferedReader.readLine(); while( line != null ) { sb.append( line ); line = bufferedReader.readLine(); } bufferedReader.close(); result = sb.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch( IOException e ) { e.printStackTrace(); } System.out.println(result);
/** * 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 = InternalThreadLocalMap.get().charsetDecoderCache(); 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; }
public FastUTF8Decoder() { decoder = StandardCharsets.UTF_8.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); byteBuffer = ByteBuffer.allocate(4); charBuffer = CharBuffer.allocate(4); }
/** * 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); }
/** * 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 = InternalThreadLocalMap.get().charsetDecoderCache(); 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; }
/** * Returns a cached thread-local {@link java.nio.charset.CharsetDecoder} for the specified <tt>charset</tt>. */ private static CharsetDecoder getDecoder(Charset charset) { requireNonNull(charset, "charset is null"); 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; } }
public String name() { // Get the first line of script as the name String firstLine = this.gremlin.split("\r\n|\r|\n", 2)[0]; final Charset charset = Charset.forName(CHARSET); final byte[] bytes = firstLine.getBytes(charset); if (bytes.length <= MAX_NAME_LENGTH) { return firstLine; } /* * Reference https://stackoverflow.com/questions/3576754/truncating-strings-by-bytes */ CharsetDecoder decoder = charset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.IGNORE); decoder.reset(); ByteBuffer buffer = ByteBuffer.wrap(bytes, 0, MAX_NAME_LENGTH); try { return decoder.decode(buffer).toString(); } catch (CharacterCodingException e) { throw new HugeException("Failed to decode truncated bytes of " + "gremlin first line", e); } }