// Create the encoder and decoder for ISO-8859-1 Charset charset = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = charset.newDecoder(); CharsetEncoder encoder = charset.newEncoder(); try { // Convert a string to ISO-LATIN-1 bytes in a ByteBuffer // The new ByteBuffer is ready to be read. ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("a string")); // Convert ISO-LATIN-1 bytes in a ByteBuffer to a character ByteBuffer and then to a string. // The new ByteBuffer is ready to be read. CharBuffer cbuf = decoder.decode(bbuf); String s = cbuf.toString(); } catch (CharacterCodingException e) { }
/** * 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; } }
private String decode() { if (null == decoder) { decoder = UTF8.newDecoder(); } snapshot(); try { if (null == chars || chars.remaining() < buffer.remaining()) { chars = CharBuffer.allocate(buffer.remaining()); } else { chars.rewind(); } decoder.reset(); CoderResult cr = decoder.decode(buffer, chars, true); if (cr.isUnderflow()) { decoder.flush(chars); } chars.flip(); return chars.toString(); } finally { reset(); } }
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); }
/** * 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); }
InputStream in = ...; CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder(); decoder.onMalformedInput(CodingErrorAction.IGNORE); Reader reader = new InputStreamReader(in, decoder);
CharsetDecoder decoder = encoding.newDecoder().onUnmappableCharacter( CodingErrorAction.REPORT); internalIn2 = new InputStreamReader(internalIn, decoder);
/** * @see org.apache.tools.zip.ZipEncoding#decode(byte[]) */ public String decode(final byte[] data) throws IOException { return this.charset.newDecoder() .onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT) .decode(ByteBuffer.wrap(data)).toString(); } }
boolean utf16 = decoder.charset().name().startsWith("UTF-16"); decoder.reset(); int expectedLength = (int) (remaining() * decoder.averageCharsPerByte()) + 1; CharBuffer out = CharBuffer.allocate(expectedLength); for (;;) { CoderResult cr; if (hasRemaining()) { cr = decoder.decode(buf(), out, true); } else { cr = decoder.flush(out); if (cr.isUnderflow()) { break; if (cr.isOverflow()) { CharBuffer o = CharBuffer.allocate(out.capacity() + expectedLength); out.flip(); o.put(out); cr.throwException();
/** * transfer ByteBuffer to String * * @param buffer buffer * @param charsetName charsetName * @return String * @throws IOException IOException */ public static String byteBufferToString(ByteBuffer buffer, String charsetName) throws IOException { Charset charset = null; CharsetDecoder decoder = null; CharBuffer charBuffer = null; charset = Charset.forName(charsetName); decoder = charset.newDecoder(); charBuffer = decoder.decode(buffer.asReadOnlyBuffer()); return charBuffer.toString(); }
if (_bb == null) { _bb = ByteBuffer.allocate(6); _cb = CharBuffer.allocate(1); _dc = Charset.forName("UTF-8").newDecoder(); } else { _bb.clear(); _cb.clear(); _bb.put((byte) b); _dc.reset(); _bb.flip(); CoderResult result = _dc.decode(_bb, _cb, true); _cb.flip(); if (result.isError() || !_cb.hasRemaining()) return REPLACEMENT;
final Charset inputCharset = Charset.forName(context.getProperty(INPUT_CHARSET).evaluateAttributeExpressions(flowFile).getValue()); final Charset outputCharset = Charset.forName(context.getProperty(OUTPUT_CHARSET).evaluateAttributeExpressions(flowFile).getValue()); final CharBuffer charBuffer = CharBuffer.allocate(MAX_BUFFER_SIZE); final CharsetDecoder decoder = inputCharset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); decoder.replaceWith("?"); final CharsetEncoder encoder = outputCharset.newEncoder(); encoder.onMalformedInput(CodingErrorAction.REPLACE); encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); encoder.replaceWith("?".getBytes(outputCharset));
CharsetDecoder cd = Charset.forName(charsetName).newDecoder(); cd.onMalformedInput(CodingErrorAction.REPLACE); cd.onUnmappableCharacter(CodingErrorAction.REPLACE); int lengthEstimate = Math.ceil(cd.averageCharsPerByte()*inBytes.length) + 1; ByteBuffer inBuf = ByteBuffer.wrap(inBytes); CharBuffer outBuf = CharBuffer.allocate(lengthEstimate); StringBuilder out = new StringBuilder(lengthEstimate); CoderResult cr; while (true) { cr = cd.decode(inBuf, outBuf, true); out.append(outBuf); outBuf.clear(); if (cr.isUnderflow()) break; if (!cr.isOverflow()) cr.throwException(); } cr = cd.flush(outBuf); if (!cr.isUnderflow()) cr.throwException(); out.append(outBuf);
CharsetDecoder cd = Charset.forName("UTF-8").newDecoder(); ByteBuffer in = ByteBuffer.wrap(bytes); CharBuffer out = CharBuffer.allocate(1); int p = 0; while (in.hasRemaining()) { cd.decode(in, out, true); char c = out.array()[0]; int nBytes = in.position() - p; p = in.position(); out.position(0); }
CharsetDecoder decoder = Charset.forName(encoding).newDecoder(); CharBuffer cb = CharBuffer.allocate(100); decoder.decode(ByteBuffer.wrap(buffer1), cb, false); decoder.decode(ByteBuffer.wrap(buffer2), cb, false); ... decoder.decode(ByteBuffer.wrap(bufferN), cb, true); cb.position(0); return cb.toString();
buf.flip(); this.byteBuf = new byte[1]; // single byte this.charBuf = CharBuffer.allocate(2); // two chars for surrogate pairs charBuf.flip(); this.fileSize = file.length(); this.decoder = charset.newDecoder(); this.position = 0; this.syncPosition = 0; if (charset.name().startsWith("UTF-8")) { } else if (charset.name().startsWith("UTF-16")) { this.maxCharWidth = (int) Math.ceil(charset.newEncoder().maxBytesPerChar()); "Unexpected value for decode error policy: " + decodeErrorPolicy); decoder.onMalformedInput(errorAction); decoder.onUnmappableCharacter(errorAction);
if (!"UTF-16".equals(charset.name())) { return; final byte[] bytes = TEST_STRING_2.getBytes(charset); final CharsetDecoder charsetDecoder2 = charset.newDecoder(); final ByteBuffer bb2 = ByteBuffer.allocate(16); final CharBuffer cb2 = CharBuffer.allocate(TEST_STRING_2.length()); final int len = bytes.length; for (int i = 0; i < len; i++) { bb2.flip(); try { charsetDecoder2.decode(bb2, cb2, i == (len - 1)); } catch ( final IllegalArgumentException e){ throw new UnsupportedOperationException("UTF-16 requested when runninng on an IBM JDK with broken UTF-16 support. " + cb2.rewind(); if (!TEST_STRING_2.equals(cb2.toString())){ throw new UnsupportedOperationException("UTF-16 requested when runninng on an IBM JDK with broken UTF-16 support. " + "Please find a JDK that supports UTF-16 if you intend to use UF-16 with WriterOutputStream");
public class Test { public static void main(String[] args) throws CharacterCodingException { ByteBuffer buffer = ByteBuffer.allocate(8); buffer.putInt(0x80); buffer.putInt(0x81); buffer.position(0); Charset charset = Charset.forName("IBM1098"); CharsetDecoder decoder = charset.newDecoder(); decoder.decode(buffer); } }
public static void main(String[] args) throws IOException { final File file = //some file try (final FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel()) { final StringBuilder stringBuilder = new StringBuilder(); final ByteBuffer byteBuffer = ByteBuffer.allocate(1024); final CharsetDecoder charsetDecoder = Charset.forName("UTF-8").newDecoder(); while (fileChannel.read(byteBuffer) > 0) { byteBuffer.flip(); stringBuilder.append(charsetDecoder.decode(byteBuffer)); byteBuffer.clear(); } } }