/** * Creates a new input stream that will encode the characters from {@code reader} into bytes using * the given character set. Malformed input and unmappable characters will be replaced. * * @param reader input source * @param charset character set used for encoding chars to bytes * @param bufferSize size of internal input and output buffers * @throws IllegalArgumentException if bufferSize is non-positive */ ReaderInputStream(Reader reader, Charset charset, int bufferSize) { this( reader, charset .newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE), bufferSize); }
public NioEscapeHandler(String charsetName) { // this(Charset.forName(charsetName)); this.encoder = Charset.forName(charsetName).newEncoder(); }
@Override protected CharsetEncoder initialValue() { return Charset.forName("UTF-8").newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); } };
/** * Constructor. * * @param cs the input character sequence * @param charset the character set name to use * @param bufferSize the buffer size to use. * @throws IllegalArgumentException if the buffer is not large enough to hold a complete character */ public CharSequenceInputStream(final CharSequence cs, final Charset charset, final int bufferSize) { super(); this.encoder = charset.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); // Ensure that buffer is long enough to hold a complete character final float maxBytesPerChar = encoder.maxBytesPerChar(); if (bufferSize < maxBytesPerChar) { throw new IllegalArgumentException("Buffer size " + bufferSize + " is less than maxBytesPerChar " + maxBytesPerChar); } this.bbuf = ByteBuffer.allocate(bufferSize); this.bbuf.flip(); this.cbuf = CharBuffer.wrap(cs); this.mark_cbuf = NO_MARK; this.mark_bbuf = NO_MARK; }
protected static byte[] charBufferToBytes(CharBuffer charBuffer, String characterSet) throws CharacterCodingException { Charset charset = Charset.forName(characterSet); CharsetEncoder encoder = charset.newEncoder(); ByteBuffer byteBuffer = encoder.encode(charBuffer); return BufferTools.copyBuffer(byteBuffer.array(), 0, byteBuffer.limit()); } }
/** * Get the bytes of the String in UTF-8 encoded form. */ public static byte[] encode(CharSequence string) { try { ByteBuffer bytes = CHARSET.newEncoder().encode(CharBuffer.wrap(string)); byte[] bytesCopy = new byte[bytes.limit()]; System.arraycopy(bytes.array(), 0, bytesCopy, 0, bytes.limit()); return bytesCopy; } catch (CharacterCodingException e) { throw new IllegalArgumentException("Encoding failed", e); } }
out = new StringBuilder(i + (m - i) * 3); out.append(s.substring(0, i)); enc = StandardCharsets.UTF_8.newEncoder(); buf = CharBuffer.allocate(1); escaped = true; buf.rewind(); try { ByteBuffer bytes = enc.encode(buf); while (bytes.hasRemaining()) { byte b = bytes.get();
private static final Writer writerForFile(File f) throws IOException { if (CHARSET == null) return new FileWriter(f); FileOutputStream fileStream = new FileOutputStream(f); CharsetEncoder ce = CHARSET.newEncoder(); ce.onUnmappableCharacter(CodingErrorAction.REPORT); return new OutputStreamWriter(fileStream, ce); }
// 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) { }
OutputStreamWriter char_output = new OutputStreamWriter( new FileOutputStream("some_output.utf8"), Charset.forName("UTF-8").newEncoder() ); InputStreamReader char_input = new InputStreamReader( new FileInputStream("some_input.utf8"), Charset.forName("UTF-8").newDecoder() );
@Override protected CharsetEncoder initialValue() { return Charset.forName("UTF-8").newEncoder(). onMalformedInput(CodingErrorAction.REPORT). onUnmappableCharacter(CodingErrorAction.REPORT); } };
Charset utf8 = Charset.forName("UTF-8"); CharsetEncoder encoder = utf8.newEncoder(); CharBuffer input = //allocate in some way, or pass as parameter ByteBuffer output = ByteBuffer.allocate(10); int limit = input.limit(); while(input.position() < limit) { output.clear(); input.mark(); input.limit(Math.max(input.position() + 2, input.capacity())); if (Character.isHighSurrogate(input.get()) && !Character.isLowSurrogate(input.get())) { //Malformed surrogate pair; do something! } input.limit(input.position()); input.reset(); encoder.encode(input, output, false); int encodedLen = output.position(); }
/** * Construct a new {@link ReaderInputStream}. * * @param reader the target {@link Reader} * @param charset the charset encoding * @param bufferSize the size of the input buffer in number of characters */ public ReaderInputStream(final Reader reader, final Charset charset, final int bufferSize) { this(reader, charset.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE), bufferSize); }
/** * Get the bytes of the String in UTF-8 encoded form. */ public static byte[] encode(CharSequence string) { try { ByteBuffer bytes = CHARSET.newEncoder().encode(CharBuffer.wrap(string)); byte[] bytesCopy = new byte[bytes.limit()]; System.arraycopy(bytes.array(), 0, bytesCopy, 0, bytes.limit()); return bytesCopy; } catch (CharacterCodingException e) { throw new IllegalArgumentException("Encoding failed", e); } }
/** * @return a buffer with position set to 0 */ public static ByteBuffer toUTF8(CharBuffer chars) { try { final CharsetEncoder utf8Encoder = UTF8.newEncoder(); return utf8Encoder.encode(chars); } catch (CharacterCodingException e) { // Should never happen. throw new RuntimeException(e); } }
/** * Returns a new {@code ByteBuffer} containing the bytes encoding the characters from * {@code buffer}. * This method uses {@code CodingErrorAction.REPLACE}. * * <p>Applications should generally create a {@link CharsetEncoder} using {@link #newEncoder} * for performance. * * @param buffer * the character buffer containing the content to be encoded. * @return the result of the encoding. */ public final ByteBuffer encode(CharBuffer buffer) { try { return newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE).encode( buffer); } catch (CharacterCodingException ex) { throw new Error(ex.getMessage(), ex); } }
@Override public Writer openSource(JPackage pkg, String fileName) throws IOException { final Writer bw = new OutputStreamWriter(openBinary(pkg, fileName), encoding); return new UnicodeEscapeWriter(bw) { private final CharsetEncoder encoder = Charset.forName(encoding).newEncoder(); @Override protected boolean requireEscaping(int ch) { // control characters if (ch < 0x20 && " \t\r\n".indexOf(ch) == -1) { return true; } // ASCII chars if (ch < 0x80) { return false; } return !encoder.canEncode((char) ch); } }; }
@Override protected CharsetEncoder initialValue() { return Charset.forName("UTF-8").newEncoder(). onMalformedInput(CodingErrorAction.REPORT). onUnmappableCharacter(CodingErrorAction.REPORT); } };
protected static byte[] charBufferToBytes(CharBuffer charBuffer, String characterSet) throws CharacterCodingException { Charset charset = Charset.forName(characterSet); CharsetEncoder encoder = charset.newEncoder(); ByteBuffer byteBuffer = encoder.encode(charBuffer); return BufferTools.copyBuffer(byteBuffer.array(), 0, byteBuffer.limit()); } }
/** * Creates a new input stream that will encode the characters from {@code reader} into bytes using * the given character set. Malformed input and unmappable characters will be replaced. * * @param reader input source * @param charset character set used for encoding chars to bytes * @param bufferSize size of internal input and output buffers * @throws IllegalArgumentException if bufferSize is non-positive */ ReaderInputStream(Reader reader, Charset charset, int bufferSize) { this( reader, charset .newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE), bufferSize); }