Refine search
/** * @return the {@link CharsetDecoder} that should be used when converting * content from binary to character */ private CharsetDecoder getDecoder() { if (decoder == null) { Charset cs = Charsets.lookupCharset(encoding); decoder = cs.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); } return decoder; }
/** * Set {@link ByteChunk} content to CharChunk using given {@link Charset}. * @throws CharConversionException */ public void set(final ByteChunk byteChunk, final Charset encoding) throws CharConversionException { final int bufferStart = byteChunk.getStart(); final int bufferLength = byteChunk.getLength(); allocate(bufferLength, -1); final byte[] buffer = byteChunk.getBuffer(); if (!DEFAULT_HTTP_CHARSET.equals(encoding)) { final ByteBuffer bb = ByteBuffer.wrap(buffer, bufferStart, bufferLength); final CharBuffer cb = CharBuffer.wrap(buff, start, buff.length - start); final CharsetDecoder decoder = Charsets.getCharsetDecoder(encoding); final CoderResult cr = decoder.decode(bb, cb, true); if (cr != CoderResult.UNDERFLOW) { throw new CharConversionException("Decoding error"); } end = start + cb.position(); return; } // Default encoding: fast conversion for (int i = 0; i < bufferLength; i++) { buff[i] = (char) (buffer[i + bufferStart] & 0xff); } end = bufferLength; }
/** Create a converter, with bytes going to a byte buffer */ public C2BConverter(ByteChunk output, String encoding) throws IOException { this.bb=output; this.enc=encoding; encoder = Charsets.lookupCharset(enc).newEncoder(). onMalformedInput(CodingErrorAction.REPLACE). onUnmappableCharacter(CodingErrorAction.REPLACE); }
public static void encode(Charset charset, String string, OutputStream os) throws IOException { if (string.length() == 0) return; final CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); 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) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } os.write(ba, 0, bb.position()); }
/** * Return the {@link Charset}'s {@link CharsetDecoder}. * The <tt>Charsets</tt> class maintains the {@link CharsetDecoder} thread-local * cache. Be aware - this shouldn't be used by multiple threads. * * @param charset {@link Charset}. * @return the {@link Charset}'s {@link CharsetDecoder}. */ public static CharsetDecoder getCharsetDecoder(final Charset charset) { if (charset == null) { throw new IllegalArgumentException("Charset can not be null"); } final CharsetDecoder decoder = obtainCodecsCache().getDecoder(charset); decoder.reset(); return decoder; }
/** * Return the {@link Charset}'s {@link CharsetEncoder}. * The <tt>Charsets</tt> class maintains the {@link CharsetEncoder} thread-local * cache. Be aware - this shouldn't be used by multiple threads. * * @param charset {@link Charset}. * @return the {@link Charset}'s {@link CharsetEncoder}. */ public static CharsetEncoder getCharsetEncoder(final Charset charset) { if (charset == null) { throw new IllegalArgumentException("Charset can not be null"); } final CharsetEncoder encoder = obtainCodecsCache().getEncoder(charset); encoder.reset(); return encoder; }
@Test public void testBulkByteBufferGetWithEmptyBuffers() throws Exception { BuffersBuffer b = BuffersBuffer.create(mm); b.append(Buffers.wrap(mm, "Hello ")); b.append(BuffersBuffer.create(mm)); b.append(Buffers.wrap(mm, "world!")); ByteBuffer buffer = ByteBuffer.allocate(12); b.get(buffer); buffer.flip(); assertEquals("Hello world!", Charsets.getCharsetDecoder(Charsets.UTF8_CHARSET).decode(buffer).toString()); }
private Charset getCharset(final String charset) { String charsetLocal = charset; if (charsetLocal == null) { charsetLocal = httpResponsePacket.getCharacterEncoding(); } return charsetLocal == null ? Charsets.ASCII_CHARSET : Charsets.lookupCharset(charsetLocal); } }
public void testDecodersCache() { final CharsetDecoder decoder0 = Charsets.getCharsetDecoder(charsets[0]); final CharsetDecoder decoder1 = Charsets.getCharsetDecoder(charsets[1]); final CharsetDecoder decoder2 = Charsets.getCharsetDecoder(charsets[2]); final CharsetDecoder decoder3 = Charsets.getCharsetDecoder(charsets[3]); assertTrue("Decoder is not the same", decoder0 == Charsets.getCharsetDecoder(charsets[0])); assertTrue("Decoder is not the same", decoder1 == Charsets.getCharsetDecoder(charsets[1])); assertTrue("Decoder is not the same", decoder2 == Charsets.getCharsetDecoder(charsets[2])); assertTrue("Decoder is not the same", decoder3 == Charsets.getCharsetDecoder(charsets[3])); final CharsetDecoder decoder4 = Charsets.getCharsetDecoder(charsets[4]); assertTrue("Decoder should be different", decoder0 != Charsets.getCharsetDecoder(charsets[0])); assertTrue("Decoder is not the same", decoder4 == Charsets.getCharsetDecoder(charsets[4])); }
public void testEncodersCache() { final CharsetEncoder encoder0 = Charsets.getCharsetEncoder(charsets[0]); final CharsetEncoder encoder1 = Charsets.getCharsetEncoder(charsets[1]); final CharsetEncoder encoder2 = Charsets.getCharsetEncoder(charsets[2]); final CharsetEncoder encoder3 = Charsets.getCharsetEncoder(charsets[3]); assertTrue("Encoder is not the same", encoder0 == Charsets.getCharsetEncoder(charsets[0])); assertTrue("Encoder is not the same", encoder1 == Charsets.getCharsetEncoder(charsets[1])); assertTrue("Encoder is not the same", encoder2 == Charsets.getCharsetEncoder(charsets[2])); assertTrue("Encoder is not the same", encoder3 == Charsets.getCharsetEncoder(charsets[3])); final CharsetEncoder encoder4 = Charsets.getCharsetEncoder(charsets[4]); assertTrue("Encoder should be different", encoder0 != Charsets.getCharsetEncoder(charsets[0])); assertTrue("Encoder is not the same", encoder4 == Charsets.getCharsetEncoder(charsets[4])); }
/** * Set {@link ByteChunk} content to CharChunk using given {@link Charset}. * @throws CharConversionException */ public void set(final ByteChunk byteChunk, final Charset encoding) throws CharConversionException { final int bufferStart = byteChunk.getStart(); final int bufferLength = byteChunk.getLength(); allocate(bufferLength, -1); final byte[] buffer = byteChunk.getBuffer(); if (!DEFAULT_HTTP_CHARSET.equals(encoding)) { final ByteBuffer bb = ByteBuffer.wrap(buffer, bufferStart, bufferLength); final CharBuffer cb = CharBuffer.wrap(buff, start, buff.length - start); final CharsetDecoder decoder = Charsets.getCharsetDecoder(encoding); final CoderResult cr = decoder.decode(bb, cb, true); if (cr != CoderResult.UNDERFLOW) { throw new CharConversionException("Decoding error"); } end = start + cb.position(); return; } // Default encoding: fast conversion for (int i = 0; i < bufferLength; i++) { buff[i] = (char) (buffer[i + bufferStart] & 0xff); } end = bufferLength; }
/** Create a converter, with bytes going to a byte buffer */ public C2BConverter(ByteChunk output, String encoding) throws IOException { this.bb=output; this.enc=encoding; encoder = Charsets.lookupCharset(enc).newEncoder(). onMalformedInput(CodingErrorAction.REPLACE). onUnmappableCharacter(CodingErrorAction.REPLACE); }
public static void encode(Charset charset, String string, OutputStream os) throws IOException { if (string.length() == 0) return; final CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); 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) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } os.write(ba, 0, bb.position()); }
/** * Return the {@link Charset}'s {@link CharsetDecoder}. * The <tt>Charsets</tt> class maintains the {@link CharsetDecoder} thread-local * cache. Be aware - this shouldn't be used by multiple threads. * * @param charset {@link Charset}. * @return the {@link Charset}'s {@link CharsetDecoder}. */ public static CharsetDecoder getCharsetDecoder(final Charset charset) { if (charset == null) { throw new IllegalArgumentException("Charset can not be null"); } final CharsetDecoder decoder = obtainCodecsCache().getDecoder(charset); decoder.reset(); return decoder; }
/** * Return the {@link Charset}'s {@link CharsetEncoder}. * The <tt>Charsets</tt> class maintains the {@link CharsetEncoder} thread-local * cache. Be aware - this shouldn't be used by multiple threads. * * @param charset {@link Charset}. * @return the {@link Charset}'s {@link CharsetEncoder}. */ public static CharsetEncoder getCharsetEncoder(final Charset charset) { if (charset == null) { throw new IllegalArgumentException("Charset can not be null"); } final CharsetEncoder encoder = obtainCodecsCache().getEncoder(charset); encoder.reset(); return encoder; }
/** * Set the request URI encoding used by this <tt>HttpHandler</tt>. * @param requestURIEncoding the request URI encoding used by this <tt>HttpHandler</tt>. */ public void setRequestURIEncoding(final String requestURIEncoding) { this.requestURIEncoding = Charsets.lookupCharset(requestURIEncoding); }
/** * @return the {@link CharsetDecoder} that should be used when converting * content from binary to character */ private CharsetDecoder getDecoder() { if (decoder == null) { Charset cs = Charsets.lookupCharset(encoding); decoder = cs.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); } return decoder; }
/** * Set {@link ByteChunk} content to CharChunk using given {@link Charset}. * @throws CharConversionException */ public void set(final ByteChunk byteChunk, final Charset encoding) throws CharConversionException { final int bufferStart = byteChunk.getStart(); final int bufferLength = byteChunk.getLength(); allocate(bufferLength, -1); final byte[] buffer = byteChunk.getBuffer(); if (!DEFAULT_HTTP_CHARSET.equals(encoding)) { final ByteBuffer bb = ByteBuffer.wrap(buffer, bufferStart, bufferLength); final CharBuffer cb = CharBuffer.wrap(buff, start, buff.length - start); final CharsetDecoder decoder = Charsets.getCharsetDecoder(encoding); final CoderResult cr = decoder.decode(bb, cb, true); if (cr != CoderResult.UNDERFLOW) { throw new CharConversionException("Decoding error"); } end = start + cb.position(); return; } // Default encoding: fast conversion for (int i = 0; i < bufferLength; i++) { buff[i] = (char) (buffer[i + bufferStart] & 0xff); } end = bufferLength; }
/** Create a converter, with bytes going to a byte buffer */ public C2BConverter(ByteChunk output, String encoding) throws IOException { this.bb=output; this.enc=encoding; encoder = Charsets.lookupCharset(enc).newEncoder(). onMalformedInput(CodingErrorAction.REPLACE). onUnmappableCharacter(CodingErrorAction.REPLACE); }
public static void encode(Charset charset, String string, OutputStream os) throws IOException { if (string.length() == 0) return; final CharsetEncoder ce = Charsets.getCharsetEncoder(charset); int en = scale(string.length(), ce.maxBytesPerChar()); byte[] ba = new byte[en]; ce.reset(); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(string); 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) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } os.write(ba, 0, bb.position()); }