@SuppressWarnings("ObjectToString") public static String decodeString(ByteBuffer src, Charset charset) { CharsetDecoder decoder = getDecoder(charset); CharBuffer dst = CharBuffer.allocate((int) ((double) src.remaining() * decoder.maxCharsPerByte())); try { CoderResult cr = decoder.decode(src, dst, true); if (!cr.isUnderflow()) { cr.throwException(); } cr = decoder.flush(dst); if (!cr.isUnderflow()) { cr.throwException(); } } catch (CharacterCodingException x) { throw new IllegalStateException(x); } return dst.flip().toString(); }
/** * Tests whether the given argument is legal as this encoder's replacement byte * array. The given byte array is legal if and only if it can be decoded into * characters. */ public boolean isLegalReplacement(byte[] replacement) { if (decoder == null) { decoder = charset.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPORT); decoder.onUnmappableCharacter(CodingErrorAction.REPORT); } ByteBuffer in = ByteBuffer.wrap(replacement); CharBuffer out = CharBuffer.allocate((int) (replacement.length * decoder.maxCharsPerByte())); CoderResult result = decoder.decode(in, out, true); return !result.isError(); }
public CharBuffer decodeUnsafely(byte[] byteS, int start, int len) { // Prepare buffers if (byteBuffer.capacity() < len) { byteBuffer = ByteBuffer.allocate(len * 2); } byteBuffer.clear(); byteBuffer.put(byteS, start, len); byteBuffer.flip(); int maxChars = (int) (byteBuffer.capacity() * decoder.maxCharsPerByte()); if (charBuffer.capacity() < maxChars) { charBuffer = CharBuffer.allocate(maxChars); } charBuffer.clear(); // Decode UTF-8 decoder.reset(); decoder.decode(byteBuffer, charBuffer, true); decoder.flush(charBuffer); charBuffer.flip(); return charBuffer; } }
public CharBuffer decodeUnsafely(byte[] byteS, int start, int len) { // Prepare buffers if (byteBuffer.capacity() < len) { byteBuffer = ByteBuffer.allocate(len * 2); } byteBuffer.clear(); byteBuffer.put(byteS, start, len); byteBuffer.flip(); int maxChars = (int) (byteBuffer.capacity() * decoder.maxCharsPerByte()); if (charBuffer.capacity() < maxChars) { charBuffer = CharBuffer.allocate(maxChars); } charBuffer.clear(); // Decode UTF-8 decoder.reset(); decoder.decode(byteBuffer, charBuffer, true); decoder.flush(charBuffer); charBuffer.flip(); return charBuffer; } }
static String decodeString(ByteBuffer src, Charset charset) { final CharsetDecoder decoder = CharsetUtil.getDecoder(charset); final CharBuffer dst = CharBuffer.allocate( (int) ((double) src.remaining() * decoder.maxCharsPerByte())); try { CoderResult cr = decoder.decode(src, dst, true); if (!cr.isUnderflow()) { cr.throwException(); } cr = decoder.flush(dst); if (!cr.isUnderflow()) { cr.throwException(); } } catch (CharacterCodingException x) { throw new IllegalStateException(x); } return dst.flip().toString(); }
@SuppressWarnings("unchecked") public static <T> T parseObject(byte[] input, // int off, // int len, // CharsetDecoder charsetDecoder, // Type clazz, // Feature... features) { charsetDecoder.reset(); int scaleLength = (int) (len * (double) charsetDecoder.maxCharsPerByte()); char[] chars = allocateChars(scaleLength); ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len); CharBuffer charByte = CharBuffer.wrap(chars); IOUtils.decode(charsetDecoder, byteBuf, charByte); int position = charByte.position(); return (T) parseObject(chars, position, clazz, features); }
throw new IllegalArgumentException("replacement.isEmpty()"); if (replacement.length() > maxCharsPerByte()) { throw new IllegalArgumentException("replacement length > maxCharsPerByte: " + replacement.length() + " > " + maxCharsPerByte());
int outBufLen = (int)Math.ceil(charsetDecoder.maxCharsPerByte() * (data.length + 1));
/** * Construct a new instance. * * @param writer the writer to decode into * @param decoder the charset decoder to use * @param bufferSize the buffer size to use */ public WriterOutputStream(final Writer writer, final CharsetDecoder decoder, int bufferSize) { if (writer == null) { throw msg.nullParameter("writer"); } if (decoder == null) { throw msg.nullParameter("decoder"); } if (bufferSize < 1) { throw msg.parameterOutOfRange("bufferSize"); } this.writer = writer; this.decoder = decoder; byteBuffer = ByteBuffer.allocate(bufferSize); chars = new char[(int) ((float)bufferSize * decoder.maxCharsPerByte() + 0.5f)]; }
final int maxLength = (int) ((double) len * decoder.maxCharsPerByte()); CharBuffer dst = CHAR_BUFFERS.get(); if (dst.length() < maxLength) {
public static Object parse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, int features) { charsetDecoder.reset(); int scaleLength = (int) (len * (double) charsetDecoder.maxCharsPerByte()); char[] chars = allocateChars(scaleLength); ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len); CharBuffer charBuf = CharBuffer.wrap(chars); IOUtils.decode(charsetDecoder, byteBuf, charBuf); int position = charBuf.position(); DefaultJSONParser parser = new DefaultJSONParser(chars, position, ParserConfig.getGlobalInstance(), features); Object value = parser.parse(); parser.handleResovleTask(value); parser.close(); return value; }
final int maxLength = (int) ((double) len * decoder.maxCharsPerByte()); CharBuffer dst = CHAR_BUFFERS.get(); if (dst.length() < maxLength) {
public InputStreamReader(InputStream in, CharsetDecoder cd, int bufferSize) { this.cd = cd; this.rbc = Channels.newChannel(in); this.bb = ByteBuffer.allocate(bufferSize); this.cb = CharBuffer.allocate((int)(bufferSize * (double)cd.maxCharsPerByte())); this.cb.limit(0); }
public InputStreamReader(InputStream in, CharsetDecoder cd, int bufferSize) { this.cd = cd; this.rbc = Channels.newChannel(in); this.bb = ByteBuffer.allocate(bufferSize); this.cb = CharBuffer.allocate((int)(bufferSize * (double)cd.maxCharsPerByte())); this.cb.limit(0); }
private static String decode(CharsetDecoder decoder, byte[] in) { ByteBuffer inBuf = ByteBuffer.wrap(in); CharBuffer outBuf = CharBuffer.allocate(inBuf.capacity() * Math.round(decoder.maxCharsPerByte() + 0.5f)); decoder.decode(inBuf, outBuf, true); decoder.flush(outBuf); decoder.reset(); return outBuf.flip().toString(); }
private static String decode(CharsetDecoder decoder, byte[] in) { ByteBuffer inBuf = ByteBuffer.wrap(in); CharBuffer outBuf = CharBuffer.allocate(inBuf.capacity() * Math.round(decoder.maxCharsPerByte() + 0.5f)); decoder.decode(inBuf, outBuf, true); decoder.flush(outBuf); decoder.reset(); return outBuf.flip().toString(); }
/** * Constructor, call the superclass constructor with the * Charset object and pass alon the chars/byte values * from the delegate decoder. */ PicaDecoder(Charset cs, CharsetDecoder baseDecoder) { // base decoder only needed for size hints super(cs, baseDecoder.averageCharsPerByte(), baseDecoder.maxCharsPerByte()); }
/** * Call the superclass constructor with the Charset object and pass * along the chars/byte values from the decoder. */ Decoder(Charset cs, CharsetDecoder decoder) { super(cs, decoder.averageCharsPerByte(), decoder.maxCharsPerByte()); this.decoder = decoder; }
private ProxyDecoder (final CharsetDecoder defaultDecoder) { super (ProxyCharset.this, defaultDecoder.averageCharsPerByte(), defaultDecoder.maxCharsPerByte()); this.currentDecoder = defaultDecoder; initialized = true; }
public DefaultDecoder(String encoding, Buffers buffers) { this.charsetDecoder = newDecoder(encoding); this.expansionFactor = (double) this.charsetDecoder.maxCharsPerByte(); this.buffers = buffers; }