/** * Constructs a new {@link WriterOutputStream}. * * @param writer the target {@link Writer} * @param charset the charset encoding * @param bufferSize the size of the output buffer in number of characters * @param writeImmediately If {@code true} the output buffer will be flushed after each * write operation, i.e. all available data will be written to the * underlying {@link Writer} immediately. If {@code false}, the * output buffer will only be flushed when it overflows or when * {@link #flush()} or {@link #close()} is called. */ public WriterOutputStream(final Writer writer, final Charset charset, final int bufferSize, final boolean writeImmediately) { this(writer, charset.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .replaceWith("?"), bufferSize, writeImmediately); }
private static CharsetDecoder getDecoder(final Charset charset) { final CharsetDecoder decoder = charset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); decoder.replaceWith("?"); return decoder; }
private CharsetDecoder newDecoder() { if (!useReplacement) { return this.charset.newDecoder() .onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT); } else { return charset.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .replaceWith(REPLACEMENT_STRING); } }
static String inputStreamToString(InputStream is) throws IOException { CharsetDecoder decoder = Charsets.US_ASCII.newDecoder(); decoder.onMalformedInput(CodingErrorAction.IGNORE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); decoder.replaceWith("?"); CharBuffer buffer = decoder.decode( ByteBuffer.wrap(StreamUtil.inputStream2Bytes(is))); String t = buffer.toString(); t = (t.length()<(MAX_BODY_CHARS+1))? t: t.substring(MAX_BODY_CHARS); if(t.isEmpty()) { return ""; } return t.replaceAll("\\p{C}", "?") + "..."; } }
decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); decoder.replaceWith("?");
.replaceWith(REPLACEMENT_CHARACTER);
@Override protected void implReplaceWith(String replace) { if (buffer != null || !initialized) { this.replace = replace; } else { currentDecoder.replaceWith(replace); } }
private static CharsetDecoder getDecoder(final Charset charset) { final CharsetDecoder decoder = charset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); decoder.replaceWith("?"); return decoder; }
/** @see WriterOutputStream */ public WriterOutputStream(Writer delegate, Charset charset) { this( delegate, ( charset .newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) ).replaceWith("?") ); }
WriterOutputStream(final Writer writer, final Charset charset, final int bufferSize, final boolean writeImmediately) { decoderIn = ByteBuffer.allocate(128); this.writer = writer; decoder = charset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); decoder.replaceWith("?"); this.writeImmediately = writeImmediately; decoderOut = CharBuffer.allocate(bufferSize); }
public WriterOutputStream(Writer writer, Charset charset, int bufferSize, boolean writeImmediately) { this(writer, charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).replaceWith("?"), bufferSize, writeImmediately); }
private CharsetDecoder newDecoder() { if (!useReplacement) { return this.charset.newDecoder() .onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT); } else { return charset.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .replaceWith(REPLACEMENT_STRING); } }
private CharsetDecoder newDecoder() { if (!useReplacement) { return this.charset.newDecoder() .onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT); } else { return charset.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .replaceWith(REPLACEMENT_STRING); } }
/** * Constructs a new {@link WriterOutputStream}. * * @param writer the target {@link Writer} * @param charset the charset encoding * @param bufferSize the size of the output buffer in number of characters */ public WriterOutputStream(final Writer writer, final Charset charset, final int bufferSize) { this(writer, charset.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .replaceWith("?"), bufferSize); }
private static synchronized int decodeCharFromBytes(byte[] inBytes, int inLen) { charsetDecoder.reset(); charsetDecoder.onMalformedInput(CodingErrorAction.REPLACE); charsetDecoder.replaceWith(invalidKeyStr); ByteBuffer in = ByteBuffer.wrap(inBytes, 0, inLen); CharBuffer out = CharBuffer.allocate(1); charsetDecoder.decode(in, out, false); if (out.position() == 0) { return -1; } return out.get(0); }
/** * Constructs a new {@link WriterOutputStream}. * * @param writer the target {@link Writer} * @param charset the charset encoding * @param bufferSize the size of the output buffer in number of characters */ public AppendableOutputStream(final Appendable writer, final Charset charset, final int bufferSize) { this(writer, charset.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .replaceWith("?"), bufferSize); }
/** * Construct a new instance using the given charset decoder. * * @param writer the writer to write to * @param decoder the charset decoder */ public WriterOutputStream(final Writer writer, final CharsetDecoder decoder) { this.writer = writer; this.decoder = decoder; decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); decoder.replaceWith("?"); inputBuffer = ByteBuffer.allocate(256); outputBuffer = CharBuffer.allocate(256); }
CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder(); utf8Decoder.onMalformedInput(CodingErrorAction.REPLACE); utf8Decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); utf8Decoder.replaceWith("?"); // Read stress file Path path = Paths.get("<path>/UTF-8-test.txt"); byte[] data = Files.readAllBytes(path); ByteBuffer input = ByteBuffer.wrap(data); // UTF-8 decoding CharBuffer output = utf8Decoder.decode(input); // Char buffer to string String outputString = output.toString(); System.out.println(outputString);
/** * Constructs output stream for target writer, using UTF-8 decoder. * * @param writer target writer. */ public WriterOutputStream(Writer writer) { this.writer = writer; this.decoder = Charset.forName("UTF-8").newDecoder(); this.decoder.onMalformedInput(CodingErrorAction.REPLACE); this.decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); this.decoder.replaceWith("?"); this.bytesBuffer = ByteBuffer.allocate(BYTES_BUFFER_SIZE); this.charactersBuffer = CharBuffer.allocate(CHARACTERS_BUFFER_SIZE); }
/** * Constructs a new {@link WriterOutputStream}. * * @param writer the target {@link Writer} * @param charset the charset encoding * @param bufferSize the size of the output buffer in number of characters * @param writeImmediately If <tt>true</tt> the output buffer will be flushed after each * write operation, i.e. all available data will be written to the * underlying {@link Writer} immediately. If <tt>false</tt>, the * output buffer will only be flushed when it overflows or when * {@link #flush()} or {@link #close()} is called. */ public WriterOutputStream(Writer writer, Charset charset, int bufferSize, boolean writeImmediately) { this(writer, charset.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .replaceWith("?"), bufferSize, writeImmediately); }