/** * Creates a new input stream that will encode the characters from {@code reader} into bytes using * the given character set encoder. * * @param reader input source * @param encoder character set encoder 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, CharsetEncoder encoder, int bufferSize) { this.reader = checkNotNull(reader); this.encoder = checkNotNull(encoder); checkArgument(bufferSize > 0, "bufferSize must be positive: %s", bufferSize); encoder.reset(); charBuffer = CharBuffer.allocate(bufferSize); charBuffer.flip(); byteBuffer = ByteBuffer.allocate(bufferSize); }
/** * Creates a new input stream that will encode the characters from {@code reader} into bytes using * the given character set encoder. * * @param reader input source * @param encoder character set encoder 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, CharsetEncoder encoder, int bufferSize) { this.reader = checkNotNull(reader); this.encoder = checkNotNull(encoder); checkArgument(bufferSize > 0, "bufferSize must be positive: %s", bufferSize); encoder.reset(); charBuffer = CharBuffer.allocate(bufferSize); charBuffer.flip(); byteBuffer = ByteBuffer.allocate(bufferSize); }
/** * Creates a new input stream that will encode the characters from {@code reader} into bytes using * the given character set encoder. * * @param reader input source * @param encoder character set encoder 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, CharsetEncoder encoder, int bufferSize) { this.reader = checkNotNull(reader); this.encoder = checkNotNull(encoder); checkArgument(bufferSize > 0, "bufferSize must be positive: %s", bufferSize); encoder.reset(); charBuffer = CharBuffer.allocate(bufferSize); charBuffer.flip(); byteBuffer = ByteBuffer.allocate(bufferSize); }
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); } } }
this.encoder.reset(); this.cbuf.rewind(); this.bbuf.rewind();
/** * Returns a cached thread-local {@link CharsetEncoder} for the specified {@link Charset}. * * @param charset The specified charset * @return The encoder for the specified {@code charset} */ public static CharsetEncoder encoder(Charset charset) { checkNotNull(charset, "charset"); Map<Charset, CharsetEncoder> map = InternalThreadLocalMap.get().charsetEncoderCache(); CharsetEncoder e = map.get(charset); if (e != null) { e.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE); return e; } e = encoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE); map.put(charset, e); return e; }
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); } } }
/** * Creates a new input stream that will encode the characters from {@code reader} into bytes using * the given character set encoder. * * @param reader input source * @param encoder character set encoder 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, CharsetEncoder encoder, int bufferSize) { this.reader = checkNotNull(reader); this.encoder = checkNotNull(encoder); checkArgument(bufferSize > 0, "bufferSize must be positive: %s", bufferSize); encoder.reset(); charBuffer = CharBuffer.allocate(bufferSize); charBuffer.flip(); byteBuffer = ByteBuffer.allocate(bufferSize); }
/** * Returns a cached thread-local {@link CharsetEncoder} for the specified * <tt>charset</tt>. */ public static CharsetEncoder getEncoder(Charset charset) { if (charset == null) { throw new NullPointerException("charset"); } Map<Charset, CharsetEncoder> map = encoders.get(); CharsetEncoder e = map.get(charset); if (e != null) { e.reset(); e.onMalformedInput(CodingErrorAction.REPLACE); e.onUnmappableCharacter(CodingErrorAction.REPLACE); return e; } e = charset.newEncoder(); e.onMalformedInput(CodingErrorAction.REPLACE); e.onUnmappableCharacter(CodingErrorAction.REPLACE); map.put(charset, e); return e; }
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); } } }
/** * Returns a cached thread-local {@link CharsetEncoder} for the specified {@link Charset}. * * @param charset The specified charset * @return The encoder for the specified {@code charset} */ public static CharsetEncoder encoder(Charset charset) { checkNotNull(charset, "charset"); Map<Charset, CharsetEncoder> map = InternalThreadLocalMap.get().charsetEncoderCache(); CharsetEncoder e = map.get(charset); if (e != null) { e.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE); return e; } e = encoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE); map.put(charset, e); return e; }
os.write(VALUE_SEPARATOR); encoder.reset(); bBuf.clear().limit(LINE_LENGTH_LIMIT - out.length - 2);
/** * Returns a cached thread-local {@link CharsetEncoder} for the specified {@link Charset}. * * @param charset The specified charset * @return The encoder for the specified {@code charset} */ public static CharsetEncoder encoder(Charset charset) { checkNotNull(charset, "charset"); Map<Charset, CharsetEncoder> map = InternalThreadLocalMap.get().charsetEncoderCache(); CharsetEncoder e = map.get(charset); if (e != null) { e.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE); return e; } e = encoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE); map.put(charset, e); return e; }
public static byte[] userRealmPasswordDigest(MessageDigest messageDigest, String username, String realm, char[] password) { CharsetEncoder latin1Encoder = StandardCharsets.ISO_8859_1.newEncoder(); latin1Encoder.reset(); boolean bothLatin1 = latin1Encoder.canEncode(username); latin1Encoder.reset(); if (bothLatin1) { for (char c: password) { bothLatin1 = bothLatin1 && latin1Encoder.canEncode(c); } } Charset chosenCharset = StandardCharsets.UTF_8; // bothLatin1 ? StandardCharsets.ISO_8859_1 : StandardCharsets.UTF_8; ByteStringBuilder urp = new ByteStringBuilder(); // username:realm:password urp.append(username.getBytes(chosenCharset)); urp.append(':'); if (realm != null) { urp.append(realm.getBytes((chosenCharset))); } else { urp.append(""); } urp.append(':'); urp.append(new String(password).getBytes((chosenCharset))); return messageDigest.digest(urp.toArray()); }
ByteBuffer out = ByteBuffer.allocate(length); reset();
private void append(String str) throws CharacterCodingException { int n = str.length(); if (n > chArr.length) { chArr = new char[n + 256]; chBuf = CharBuffer.wrap(chArr); } str.getChars(0, n, chArr, 0); chBuf.position(0).limit(n); utf8.reset(); for (;;) { CoderResult cr = utf8.encode(chBuf, binBuf, true); if (cr.isOverflow()) { grow(); } else if (cr.isUnderflow()) { break; } else { cr.throwException(); } } }
public ByteBuffer getBytes(String charsetName) { if (!isEnd) { throw new IllegalStateException("The conact has not been end."); } final CharsetEncoder encoder = Charset.forName(charsetName).newEncoder().onMalformedInput( CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE); final CharBuffer cb = CharBuffer.wrap(this.value, 0, this.value.length); try { encoder.reset(); final int len = (int) (this.length * (double) encoder.maxBytesPerChar()); final ByteBuffer bb = ByteBuffer.allocate(len); CoderResult cr = encoder.encode(cb, bb, true); if (!cr.isUnderflow()) { cr.throwException(); } cr = encoder.flush(bb); if (!cr.isUnderflow()) { cr.throwException(); } return bb; } catch (Exception x) { throw new Error(x); } }
/** * Returns a cached thread-local {@link CharsetEncoder} for the specified {@link Charset}. * * @param charset The specified charset * @return The encoder for the specified {@code charset} */ public static CharsetEncoder encoder(Charset charset) { checkNotNull(charset, "charset"); Map<Charset, CharsetEncoder> map = InternalThreadLocalMap.get().charsetEncoderCache(); CharsetEncoder e = map.get(charset); if (e != null) { e.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE); return e; } e = encoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE); map.put(charset, e); return e; }
private static CodePoints encodableCodePoints(CharsetEncoder encoder) { CodePoints points = new CodePoints(); int start = 0; boolean inRange = false; int current = 0; int previousCount = 0; int[] buffer = new int[1]; for (; current <= Character.MAX_CODE_POINT; ++current) { encoder.reset(); buffer[0] = current; String s = new String(buffer, 0, 1); if (encoder.canEncode(s)) { if (!inRange) { inRange = true; start = current; } } else if (inRange) { inRange = false; CodePointRange range = new CodePointRange(start, current - 1, previousCount); points.add(range); previousCount += range.size(); } } if (inRange) points.add(new CodePointRange(start, current - 1, previousCount)); return points; }