/** * Fills the byte output buffer from the input char buffer. * * @throws CharacterCodingException * an error encoding data */ private void fillBuffer() throws CharacterCodingException { this.bbuf.compact(); final CoderResult result = this.encoder.encode(this.cbuf, this.bbuf, true); if (result.isError()) { result.throwException(); } this.bbuf.flip(); }
/** * Create a copy of {@code value} into this instance using the encoding type of {@code charset}. * The copy will start at index {@code start} and copy {@code length} bytes. */ public AsciiString(CharSequence value, Charset charset, int start, int length) { CharBuffer cbuf = CharBuffer.wrap(value, start, start + length); CharsetEncoder encoder = CharsetUtil.encoder(charset); ByteBuffer nativeBuffer = ByteBuffer.allocate((int) (encoder.maxBytesPerChar() * length)); encoder.encode(cbuf, nativeBuffer, true); final int offset = nativeBuffer.arrayOffset(); this.value = Arrays.copyOfRange(nativeBuffer.array(), offset, offset + nativeBuffer.position()); this.offset = 0; this.length = this.value.length; }
/** * 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); } }
public int encode(char[] chars, int offset, int len, byte[] bytes) { ce.reset(); ByteBuffer bb = ByteBuffer.wrap(bytes); CharBuffer cb = CharBuffer.wrap(chars, offset, len); try { CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) cr.throwException(); cr = ce.flush(bb); if (!cr.isUnderflow()) cr.throwException(); return bb.position(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new RuntimeException("Encode error: " + x.getMessage(), x); } } }
private static void writeEntry(OutputStream os, Attributes.Name name, String value, CharsetEncoder encoder, ByteBuffer bBuf) throws IOException { String nameString = name.getName(); os.write(nameString.getBytes(StandardCharsets.US_ASCII)); os.write(VALUE_SEPARATOR); encoder.reset(); bBuf.clear().limit(LINE_LENGTH_LIMIT - nameString.length() - 2); CharBuffer cBuf = CharBuffer.wrap(value); while (true) { CoderResult r = encoder.encode(cBuf, bBuf, true); if (CoderResult.UNDERFLOW == r) { r = encoder.flush(bBuf); } os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position()); os.write(LINE_SEPARATOR); if (CoderResult.UNDERFLOW == r) { break; } os.write(' '); bBuf.clear().limit(LINE_LENGTH_LIMIT - 1); } } }
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 void write(final ByteBuffer buffer, final FileChannel fileChannel) { try { buffer.clear(); encoder.reset(); final CoderResult coderResult = encoder.encode(CharBuffer.wrap(builder), buffer, false); if (CoderResult.UNDERFLOW != coderResult) { coderResult.throwException(); } buffer.flip(); do { fileChannel.write(buffer); } while (buffer.remaining() > 0); } catch (final Exception ex) { LangUtil.rethrowUnchecked(ex); } } }
/** * Fills the internal char buffer from the reader. * * @throws IOException * If an I/O error occurs */ private void fillBuffer() throws IOException { if (!endOfInput && (lastCoderResult == null || lastCoderResult.isUnderflow())) { encoderIn.compact(); final int position = encoderIn.position(); // We don't use Reader#read(CharBuffer) here because it is more efficient // to write directly to the underlying char array (the default implementation // copies data to a temporary char array). final int c = reader.read(encoderIn.array(), position, encoderIn.remaining()); if (c == EOF) { endOfInput = true; } else { encoderIn.position(position+c); } encoderIn.flip(); } encoderOut.compact(); lastCoderResult = encoder.encode(encoderIn, encoderOut, endOfInput); encoderOut.flip(); }
private void advance() throws IOException { assert !endOfInput; assert !bbuf.hasRemaining() : "advance() should be called when output byte buffer is empty. bbuf: " + bbuf + ", as string: " + bbuf.asCharBuffer().toString(); assert cbuf.remaining() < 2; // given that bbuf.capacity = 3 x cbuf.capacity, the only time that we should have a // remaining char is if the last char read was the 1st half of a surrogate pair if (cbuf.remaining() == 0) { cbuf.clear(); } else { cbuf.compact(); } int n = reader.read(cbuf); // read #1 cbuf.flip(); CoderResult result; endOfInput = n == -1; bbuf.clear(); result = encoder.encode(cbuf, bbuf, endOfInput); checkEncodeResult(result); if (endOfInput) { result = encoder.flush(bbuf); checkEncodeResult(result); } bbuf.flip(); }
private void _encodeLoop(CharBuffer inputCharBuffer, boolean endOfInput) throws IOException { while (inputCharBuffer.hasRemaining()) { CoderResult coderResult = _charsetEncoder.encode( inputCharBuffer, _outputByteBuffer, endOfInput); if (coderResult.isError()) { coderResult.throwException(); } if (coderResult.isUnderflow()) { if (_autoFlush) { _flushBuffer(); } if ((_inputCharBuffer != inputCharBuffer) && inputCharBuffer.hasRemaining()) { _inputCharBuffer.put(inputCharBuffer.get()); } break; } // Must be overflow, no need to check _flushBuffer(); } }
final ByteBuffer dstBuf = dst.internalNioBuffer(dst.readerIndex(), length); final int pos = dstBuf.position(); CoderResult cr = encoder.encode(src, dstBuf, true); if (!cr.isUnderflow()) { cr.throwException(); cr = encoder.flush(dstBuf); if (!cr.isUnderflow()) { cr.throwException();
/** * 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); } }
/** * Create a copy of {@code value} into a this instance using the encoding type of {@code charset}. * The copy will start at index {@code start} and copy {@code length} bytes. */ public AsciiString(char[] value, Charset charset, int start, int length) { CharBuffer cbuf = CharBuffer.wrap(value, start, length); CharsetEncoder encoder = CharsetUtil.encoder(charset); ByteBuffer nativeBuffer = ByteBuffer.allocate((int) (encoder.maxBytesPerChar() * length)); encoder.encode(cbuf, nativeBuffer, true); final int bufferOffset = nativeBuffer.arrayOffset(); this.value = Arrays.copyOfRange(nativeBuffer.array(), bufferOffset, bufferOffset + nativeBuffer.position()); this.offset = 0; this.length = this.value.length; }
/** * 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); } }
private void convert(CharBuffer chars) throws IOException { while (true) { CoderResult result = encoder.encode(chars, bytes, false); if (result.isOverflow()) { // Make room and try again. flushBytes(false); continue; } else if (result.isError()) { result.throwException(); } break; } }
/** * Fills the internal char buffer from the reader. * * @throws java.io.IOException If an I/O error occurs */ private void fillBuffer() throws IOException { if (!endOfInput && (lastCoderResult == null || lastCoderResult.isUnderflow())) { encoderIn.compact(); int position = encoderIn.position(); // We don't use Reader#read(CharBuffer) here because it is more efficient // to write directly to the underlying char array (the default implementation // copies data to a temporary char array). int c = reader.read(encoderIn.array(), position, encoderIn.remaining()); if (c == -1) { endOfInput = true; } else { encoderIn.position(position + c); } encoderIn.flip(); } encoderOut.compact(); lastCoderResult = encoder.encode(encoderIn, encoderOut, endOfInput); encoderOut.flip(); }
final ByteBuffer dstBuf = dst.internalNioBuffer(dst.readerIndex(), length); final int pos = dstBuf.position(); CoderResult cr = encoder.encode(src, dstBuf, true); if (!cr.isUnderflow()) { cr.throwException(); cr = encoder.flush(dstBuf); if (!cr.isUnderflow()) { cr.throwException();
// 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) { }