Java Code Examples for java.nio.charset.CharsetEncoder#flush()
The following examples show how to use
java.nio.charset.CharsetEncoder#flush() .
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: ChannelBuffers.java From simple-netty-source with Apache License 2.0 | 6 votes |
static ByteBuffer encodeString(CharBuffer src, Charset charset) { final CharsetEncoder encoder = CharsetUtil.getEncoder(charset); final ByteBuffer dst = ByteBuffer.allocate( (int) ((double) src.remaining() * encoder.maxBytesPerChar())); try { CoderResult cr = encoder.encode(src, dst, true); if (!cr.isUnderflow()) { cr.throwException(); } cr = encoder.flush(dst); if (!cr.isUnderflow()) { cr.throwException(); } } catch (CharacterCodingException x) { throw new IllegalStateException(x); } dst.flip(); return dst; }
Example 2
Source File: DefaultEncoder.java From beetl2.0 with BSD 3-Clause "New" or "Revised" License | 6 votes |
public void write(final char[] chars, final int len, final OutputStream out) throws IOException { if (chars != null && len != 0) { final CharsetEncoder encoder; final ByteBuffer bb; byte[] buffer = this.localBuffer.getByteBuffer((int)(len * this.expansionFactor)); if (buffer.length != 0) { (encoder = this.charsetEncoder).reset().encode(CharBuffer.wrap(chars, 0, len), bb = ByteBuffer.wrap(buffer), true); encoder.flush(bb); out.write(buffer, 0, bb.position()); } else { buffer = new byte[(int)(len * this.expansionFactor)]; (encoder = this.charsetEncoder).reset().encode(CharBuffer.wrap(chars, 0, len), bb = ByteBuffer.wrap(buffer), true); encoder.flush(bb); out.write(buffer, 0, bb.position()); } } }
Example 3
Source File: LogHandlerAccessor.java From trufflesqueak with MIT License | 6 votes |
@Override public void publish(final LogRecord record) { final String message = record.getMessage(); final CharsetEncoder encoder = ThreadLocalCoders.encoderFor(StandardCharsets.UTF_8); final ByteBuffer buffer = ByteBuffer.allocate((int) (message.length() * encoder.maxBytesPerChar())); encoder.encode(CharBuffer.wrap(message), buffer, true); encoder.flush(buffer); try { if (channel.position() + 65536 + buffer.position() >= GIG) { close(); initializeOutputStream(); } stream.write(Arrays.copyOf(buffer.array(), buffer.position())); stream.write((byte) 10); } catch (final IOException e) { e.printStackTrace(); } }
Example 4
Source File: Utf7ImeHelper.java From uiautomator-unicode-input-helper with Apache License 2.0 | 6 votes |
private static ByteBuffer encode(CharBuffer in, CharsetEncoder encoder) { int length = (int) (in.remaining() * (double) encoder.averageBytesPerChar()); ByteBuffer out = ByteBuffer.allocate(length); encoder.reset(); CoderResult flushResult = null; while (flushResult != CoderResult.UNDERFLOW) { CoderResult encodeResult = encoder.encode(in, out, true); if (encodeResult == CoderResult.OVERFLOW) { out = allocateMore(out); continue; } flushResult = encoder.flush(out); if (flushResult == CoderResult.OVERFLOW) { out = allocateMore(out); } } out.flip(); return out; }
Example 5
Source File: ZipCoder.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
byte[] getBytes(String s) { CharsetEncoder ce = encoder().reset(); char[] ca = s.toCharArray(); int len = (int)(ca.length * ce.maxBytesPerChar()); byte[] ba = new byte[len]; if (len == 0) return ba; ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca); CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); cr = ce.flush(bb); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); if (bb.position() == ba.length) // defensive copy? return ba; else return Arrays.copyOf(ba, bb.position()); }
Example 6
Source File: CharsetEncoderTest.java From j2objc with Apache License 2.0 | 6 votes |
public void testFlushWithoutEndOfInput() throws Exception { Charset cs = Charset.forName("UTF-32BE"); CharsetEncoder e = cs.newEncoder(); ByteBuffer bb = ByteBuffer.allocate(128); CoderResult cr = e.encode(CharBuffer.wrap(new char[] { 'x' }), bb, false); assertEquals(CoderResult.UNDERFLOW, cr); assertEquals(4, bb.position()); try { cr = e.flush(bb); fail(); } catch (IllegalStateException expected) { // You must call encode with endOfInput true before you can flush. } // We had a bug where we wouldn't reset inEnd before calling encode in implFlush. // That would result in flush outputting garbage. cr = e.encode(CharBuffer.wrap(new char[] { 'x' }), bb, true); assertEquals(CoderResult.UNDERFLOW, cr); assertEquals(8, bb.position()); cr = e.flush(bb); assertEquals(CoderResult.UNDERFLOW, cr); assertEquals(8, bb.position()); }
Example 7
Source File: Slices.java From aion with MIT License | 6 votes |
public static ByteBuffer encodeString(CharBuffer src, Charset charset) { CharsetEncoder encoder = getEncoder(charset); ByteBuffer dst = ByteBuffer.allocate((int) ((double) src.remaining() * encoder.maxBytesPerChar())); try { CoderResult cr = encoder.encode(src, dst, true); if (!cr.isUnderflow()) { cr.throwException(); } cr = encoder.flush(dst); if (!cr.isUnderflow()) { cr.throwException(); } } catch (CharacterCodingException x) { throw new IllegalStateException(x); } dst.flip(); return dst; }
Example 8
Source File: StringCoding.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
static byte[] encode(Charset cs, char[] ca, int off, int len) { CharsetEncoder ce = cs.newEncoder(); int en = scale(len, ce.maxBytesPerChar()); byte[] ba = new byte[en]; if (len == 0) return ba; boolean isTrusted = false; if (System.getSecurityManager() != null) { if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) { ca = Arrays.copyOfRange(ca, off, off + len); off = 0; } } ce.onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .reset(); if (ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba); return safeTrim(ba, blen, cs, isTrusted); } else { ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca, off, len); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { throw new Error(x); } return safeTrim(ba, bb.position(), cs, isTrusted); } }
Example 9
Source File: ZipCoder.java From jdk-1.7-annotated with Apache License 2.0 | 5 votes |
byte[] getBytes(String s) { CharsetEncoder ce = encoder().reset(); char[] ca = s.toCharArray(); int len = (int)(ca.length * ce.maxBytesPerChar()); byte[] ba = new byte[len]; if (len == 0) return ba; // UTF-8 only for now. Other ArrayDeocder only handles // CodingErrorAction.REPLACE mode. if (isUTF8 && ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba); if (blen == -1) // malformed throw new IllegalArgumentException("MALFORMED"); return Arrays.copyOf(ba, blen); } ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca); CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); cr = ce.flush(bb); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); if (bb.position() == ba.length) // defensive copy? return ba; else return Arrays.copyOf(ba, bb.position()); }
Example 10
Source File: ZipCoder.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
byte[] getBytes(String s) { CharsetEncoder ce = encoder().reset(); char[] ca = s.toCharArray(); int len = (int)(ca.length * ce.maxBytesPerChar()); byte[] ba = new byte[len]; if (len == 0) return ba; // UTF-8 only for now. Other ArrayDeocder only handles // CodingErrorAction.REPLACE mode. if (isUTF8 && ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba); if (blen == -1) // malformed throw new IllegalArgumentException("MALFORMED"); return Arrays.copyOf(ba, blen); } ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca); CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); cr = ce.flush(bb); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); if (bb.position() == ba.length) // defensive copy? return ba; else return Arrays.copyOf(ba, bb.position()); }
Example 11
Source File: StringCoding.java From hottub with GNU General Public License v2.0 | 5 votes |
static byte[] encode(Charset cs, char[] ca, int off, int len) { CharsetEncoder ce = cs.newEncoder(); int en = scale(len, ce.maxBytesPerChar()); byte[] ba = new byte[en]; if (len == 0) return ba; boolean isTrusted = false; if (System.getSecurityManager() != null) { if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) { ca = Arrays.copyOfRange(ca, off, off + len); off = 0; } } ce.onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .reset(); if (ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba); return safeTrim(ba, blen, cs, isTrusted); } else { ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca, off, len); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { throw new Error(x); } return safeTrim(ba, bb.position(), cs, isTrusted); } }
Example 12
Source File: DataBuffer.java From java-technology-stack with MIT License | 5 votes |
/** * Write the given {@code CharSequence} using the given {@code Charset}, * starting at the current writing position. * @param charSequence the char sequence to write into this buffer * @param charset the charset to encode the char sequence with * @return this buffer * @since 5.1.4 */ default DataBuffer write(CharSequence charSequence, Charset charset) { Assert.notNull(charSequence, "CharSequence must not be null"); Assert.notNull(charset, "Charset must not be null"); if (charSequence.length() != 0) { CharsetEncoder charsetEncoder = charset.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); CharBuffer inBuffer = CharBuffer.wrap(charSequence); int estimatedSize = (int) (inBuffer.remaining() * charsetEncoder.averageBytesPerChar()); ByteBuffer outBuffer = ensureCapacity(estimatedSize) .asByteBuffer(writePosition(), writableByteCount()); while (true) { CoderResult cr = (inBuffer.hasRemaining() ? charsetEncoder.encode(inBuffer, outBuffer, true) : CoderResult.UNDERFLOW); if (cr.isUnderflow()) { cr = charsetEncoder.flush(outBuffer); } if (cr.isUnderflow()) { break; } if (cr.isOverflow()) { writePosition(outBuffer.position()); int maximumSize = (int) (inBuffer.remaining() * charsetEncoder.maxBytesPerChar()); ensureCapacity(maximumSize); outBuffer = asByteBuffer(writePosition(), writableByteCount()); } } writePosition(outBuffer.position()); } return this; }
Example 13
Source File: ZipCoder.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
byte[] getBytes(String s) { CharsetEncoder ce = encoder().reset(); char[] ca = s.toCharArray(); int len = (int)(ca.length * ce.maxBytesPerChar()); byte[] ba = new byte[len]; if (len == 0) return ba; // UTF-8 only for now. Other ArrayDeocder only handles // CodingErrorAction.REPLACE mode. if (isUTF8 && ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba); if (blen == -1) // malformed throw new IllegalArgumentException("MALFORMED"); return Arrays.copyOf(ba, blen); } ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca); CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); cr = ce.flush(bb); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); if (bb.position() == ba.length) // defensive copy? return ba; else return Arrays.copyOf(ba, bb.position()); }
Example 14
Source File: DataBuffer.java From spring-analysis-note with MIT License | 5 votes |
/** * Write the given {@code CharSequence} using the given {@code Charset}, * starting at the current writing position. * @param charSequence the char sequence to write into this buffer * @param charset the charset to encode the char sequence with * @return this buffer * @since 5.1.4 */ default DataBuffer write(CharSequence charSequence, Charset charset) { Assert.notNull(charSequence, "CharSequence must not be null"); Assert.notNull(charset, "Charset must not be null"); if (charSequence.length() != 0) { CharsetEncoder charsetEncoder = charset.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); CharBuffer inBuffer = CharBuffer.wrap(charSequence); int estimatedSize = (int) (inBuffer.remaining() * charsetEncoder.averageBytesPerChar()); ByteBuffer outBuffer = ensureCapacity(estimatedSize) .asByteBuffer(writePosition(), writableByteCount()); while (true) { CoderResult cr = (inBuffer.hasRemaining() ? charsetEncoder.encode(inBuffer, outBuffer, true) : CoderResult.UNDERFLOW); if (cr.isUnderflow()) { cr = charsetEncoder.flush(outBuffer); } if (cr.isUnderflow()) { break; } if (cr.isOverflow()) { writePosition(writePosition() + outBuffer.position()); int maximumSize = (int) (inBuffer.remaining() * charsetEncoder.maxBytesPerChar()); ensureCapacity(maximumSize); outBuffer = asByteBuffer(writePosition(), writableByteCount()); } } writePosition(writePosition() + outBuffer.position()); } return this; }
Example 15
Source File: ZipCoder.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
byte[] getBytes(String s) { CharsetEncoder ce = encoder().reset(); char[] ca = s.toCharArray(); int len = (int)(ca.length * ce.maxBytesPerChar()); byte[] ba = new byte[len]; if (len == 0) return ba; // UTF-8 only for now. Other ArrayDeocder only handles // CodingErrorAction.REPLACE mode. if (isUTF8 && ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba); if (blen == -1) // malformed throw new IllegalArgumentException("MALFORMED"); return Arrays.copyOf(ba, blen); } ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca); CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); cr = ce.flush(bb); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); if (bb.position() == ba.length) // defensive copy? return ba; else return Arrays.copyOf(ba, bb.position()); }
Example 16
Source File: ZipCoder.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
byte[] getBytes(String s) { CharsetEncoder ce = encoder().reset(); char[] ca = s.toCharArray(); int len = (int)(ca.length * ce.maxBytesPerChar()); byte[] ba = new byte[len]; if (len == 0) return ba; // UTF-8 only for now. Other ArrayDeocder only handles // CodingErrorAction.REPLACE mode. if (isUTF8 && ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba); if (blen == -1) // malformed throw new IllegalArgumentException("MALFORMED"); return Arrays.copyOf(ba, blen); } ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca); CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); cr = ce.flush(bb); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); if (bb.position() == ba.length) // defensive copy? return ba; else return Arrays.copyOf(ba, bb.position()); }
Example 17
Source File: ZipCoder.java From Java8CN with Apache License 2.0 | 5 votes |
byte[] getBytes(String s) { CharsetEncoder ce = encoder().reset(); char[] ca = s.toCharArray(); int len = (int)(ca.length * ce.maxBytesPerChar()); byte[] ba = new byte[len]; if (len == 0) return ba; // UTF-8 only for now. Other ArrayDeocder only handles // CodingErrorAction.REPLACE mode. if (isUTF8 && ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba); if (blen == -1) // malformed throw new IllegalArgumentException("MALFORMED"); return Arrays.copyOf(ba, blen); } ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca); CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); cr = ce.flush(bb); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); if (bb.position() == ba.length) // defensive copy? return ba; else return Arrays.copyOf(ba, bb.position()); }
Example 18
Source File: StringCoding.java From AndroidComponentPlugin with Apache License 2.0 | 5 votes |
static byte[] encode(Charset cs, char[] ca, int off, int len) { CharsetEncoder ce = cs.newEncoder(); int en = scale(len, ce.maxBytesPerChar()); byte[] ba = new byte[en]; if (len == 0) return ba; boolean isTrusted = false; if (System.getSecurityManager() != null) { if (!(isTrusted = (cs.getClass().getClassLoader() == null))) { ca = Arrays.copyOfRange(ca, off, off + len); off = 0; } } ce.onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .reset(); if (ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba); return safeTrim(ba, blen, cs, isTrusted); } else { ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca, off, len); try { // Android-changed: Pass read-only buffer, so the encoder can't alter it CoderResult cr = ce.encode(cb.asReadOnlyBuffer(), bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { throw new Error(x); } return safeTrim(ba, bb.position(), cs, isTrusted); } }
Example 19
Source File: ZipCoder.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
byte[] getBytes(String s) { CharsetEncoder ce = encoder().reset(); char[] ca = s.toCharArray(); int len = (int)(ca.length * ce.maxBytesPerChar()); byte[] ba = new byte[len]; if (len == 0) return ba; // UTF-8 only for now. Other ArrayDeocder only handles // CodingErrorAction.REPLACE mode. if (isUTF8 && ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba); if (blen == -1) // malformed throw new IllegalArgumentException("MALFORMED"); return Arrays.copyOf(ba, blen); } ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca); CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); cr = ce.flush(bb); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); if (bb.position() == ba.length) // defensive copy? return ba; else return Arrays.copyOf(ba, bb.position()); }
Example 20
Source File: StringCoding.java From jdk1.8-source-analysis with Apache License 2.0 | 5 votes |
static byte[] encode(Charset cs, char[] ca, int off, int len) { CharsetEncoder ce = cs.newEncoder(); int en = scale(len, ce.maxBytesPerChar()); byte[] ba = new byte[en]; if (len == 0) return ba; boolean isTrusted = false; if (System.getSecurityManager() != null) { if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) { ca = Arrays.copyOfRange(ca, off, off + len); off = 0; } } ce.onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .reset(); if (ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba); return safeTrim(ba, blen, cs, isTrusted); } else { ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca, off, len); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { throw new Error(x); } return safeTrim(ba, bb.position(), cs, isTrusted); } }