String toString(byte[] ba, int length) { CharsetDecoder cd = decoder().reset(); int len = (int)(length * cd.maxCharsPerByte()); char[] ca = new char[len]; if (len == 0) return new String(ca); ByteBuffer bb = ByteBuffer.wrap(ba, 0, length); CharBuffer cb = CharBuffer.wrap(ca); CoderResult cr = cd.decode(bb, cb, true); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); cr = cd.flush(cb); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); return new String(ca, 0, cb.position()); }
LineReader(CharsetDecoder decoder, InputStream stream) throws IOException { isr = new InputStreamReader(stream, decoder); try { br = new BufferedReader(isr); } catch (Throwable t) { if (isr != null) { try { isr.close(); } catch (IOException ex) { Exceptions.printStackTrace(ex); } } throw new IOException(t); } }
static String decodePassword(byte[] bytes) throws CharacterCodingException { CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder(); // NOI18N ByteBuffer input = ByteBuffer.wrap(bytes); int outputLength = (int)(bytes.length * (double)decoder.maxCharsPerByte()); if (outputLength == 0) { return ""; // NOI18N } char[] chars = new char[outputLength]; CharBuffer output = CharBuffer.wrap(chars); CoderResult result = decoder.decode(input, output, true); if (!result.isError() && !result.isOverflow()) { result = decoder.flush(output); } if (result.isError() || result.isOverflow()) { throw new CharacterCodingException(); } else { return new String(chars, 0, output.position()); } }
private static String decode(ByteBuffer utf8, boolean replace) throws CharacterCodingException { CharsetDecoder decoder = DECODER_FACTORY.get(); if (replace) { decoder.onMalformedInput( java.nio.charset.CodingErrorAction.REPLACE); decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); } String str = decoder.decode(utf8).toString(); // set decoder back to its default value: REPORT if (replace) { decoder.onMalformedInput(CodingErrorAction.REPORT); decoder.onUnmappableCharacter(CodingErrorAction.REPORT); } return str; }
/** * Creates new instance of SessionInputBufferImpl. * * @param metrics HTTP transport metrics. * @param buffersize buffer size. Must be a positive number. * @param minChunkLimit size limit below which data chunks should be buffered in memory * in order to minimize native method invocations on the underlying network socket. * The optimal value of this parameter can be platform specific and defines a trade-off * between performance of memory copy operations and that of native method invocation. * If negative default chunk limited will be used. * @param constraints Message constraints. If <code>null</code> * {@link MessageConstraints#DEFAULT} will be used. * @param chardecoder chardecoder to be used for decoding HTTP protocol elements. * If <code>null</code> simple type cast will be used for byte to char conversion. */ public SessionInputBufferImpl( final HttpTransportMetricsImpl metrics, final int buffersize, final int minChunkLimit, final MessageConstraints constraints, final CharsetDecoder chardecoder) { Args.notNull(metrics, "HTTP transport metrcis"); Args.positive(buffersize, "Buffer size"); this.metrics = metrics; this.buffer = new byte[buffersize]; this.bufferpos = 0; this.bufferlen = 0; this.minChunkLimit = minChunkLimit >= 0 ? minChunkLimit : 512; this.constraints = constraints != null ? constraints : MessageConstraints.DEFAULT; this.linebuffer = new ByteArrayBuffer(buffersize); this.decoder = chardecoder; }
/** * Creates new instance of BHttpConnectionBase. * * @param buffersize buffer size. Must be a positive number. * @param fragmentSizeHint fragment size hint. * @param chardecoder decoder to be used for decoding HTTP protocol elements. * If <code>null</code> simple type cast will be used for byte to char conversion. * @param charencoder encoder to be used for encoding HTTP protocol elements. * If <code>null</code> simple type cast will be used for char to byte conversion. * @param constraints Message constraints. If <code>null</code> * {@link MessageConstraints#DEFAULT} will be used. * @param incomingContentStrategy incoming content length strategy. If <code>null</code> * {@link LaxContentLengthStrategy#INSTANCE} will be used. * @param outgoingContentStrategy outgoing content length strategy. If <code>null</code> * {@link StrictContentLengthStrategy#INSTANCE} will be used. */ protected BHttpConnectionBase( final int buffersize, final int fragmentSizeHint, final CharsetDecoder chardecoder, final CharsetEncoder charencoder, final MessageConstraints constraints, final ContentLengthStrategy incomingContentStrategy, final ContentLengthStrategy outgoingContentStrategy) { super(); Args.positive(buffersize, "Buffer size"); final HttpTransportMetricsImpl inTransportMetrics = new HttpTransportMetricsImpl(); final HttpTransportMetricsImpl outTransportMetrics = new HttpTransportMetricsImpl(); this.inbuffer = new SessionInputBufferImpl(inTransportMetrics, buffersize, -1, constraints != null ? constraints : MessageConstraints.DEFAULT, chardecoder); this.outbuffer = new SessionOutputBufferImpl(outTransportMetrics, buffersize, fragmentSizeHint, charencoder); this.connMetrics = new HttpConnectionMetricsImpl(inTransportMetrics, outTransportMetrics); this.incomingContentStrategy = incomingContentStrategy != null ? incomingContentStrategy : LaxContentLengthStrategy.INSTANCE; this.outgoingContentStrategy = outgoingContentStrategy != null ? outgoingContentStrategy : StrictContentLengthStrategy.INSTANCE; }
/** * Retrieves the CharsetDecoder for the given encoding. Note, This isn't perfect as I think ISCII-DEVANAGARI and * MICROSOFT-CP1251 etc are allowed... * * @param encoding Encoding to retrieve the CharsetDecoder for * @return CharSetDecoder for the given encoding */ private CharsetDecoder getJavaEncoding(String encoding) { if ("ISO8859-14".equals(encoding)) { return new ISO8859_14Decoder(); } String canon = CHARSET_ALIASES.get(encoding); if (canon != null) { encoding = canon; } Charset charset = Charset.forName(encoding); return charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE); }
public static String a(ByteBuffer byteBuffer) { CharsetDecoder newDecoder = Charset.forName("UTF8").newDecoder(); newDecoder.onMalformedInput(a); newDecoder.onUnmappableCharacter(a); try { byteBuffer.mark(); String charBuffer = newDecoder.decode(byteBuffer).toString(); byteBuffer.reset(); return charBuffer; } catch (Throwable e) { throw new b((int) CloseFrame.NO_UTF8, e); } }
public static String stringUtf8(ByteBuffer bytes) throws InvalidDataException { CharsetDecoder decode = Charset.forName("UTF8").newDecoder(); decode.onMalformedInput(codingErrorAction); decode.onUnmappableCharacter(codingErrorAction); // decode.replaceWith( "X" ); String s; try { bytes.mark(); s = decode.decode(bytes).toString(); bytes.reset(); } catch (CharacterCodingException e) { throw new InvalidDataException(CloseFrame.NO_UTF8, e); } return s; }
/** * Creates {@code BufferedCharSequence} for specified FileObject {@code fo}. * * @param fo FileObject containing character data. * @param decoder Charset decoder. * @param size file size. */ public BufferedCharSequence(final FileObject fo, CharsetDecoder decoder, long size) { this.source = new Source(fo, size); this.decoder = decoder; this.sink = new Sink(this.source); LOG.log(Level.FINER, "<init> {0}; decoder = {1}; {2}", // NOI18N new Object[]{this.source, this.decoder, this.sink}); }
private CharsetDecoder decoder() { if (dec == null) { dec = cs.newDecoder() .onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT); } return dec; }
public static CharsetDecoder getUTF8Decoder() { CharsetDecoder decoder = (CharsetDecoder) decoderLocal.get(); if (decoder != null) { return decoder; } decoder = new UTF8Decoder(); decoderLocal.set(decoder); return decoder; }
private String getRealCharsetName (CharsetDecoder decoder) { if (decoder instanceof ContentBaseEncodingQuery.SniffingDecoder) { decoder = ((ContentBaseEncodingQuery.SniffingDecoder)decoder).decoder; assertNotNull(decoder); } return decoder.charset().name(); }
@Test public void testRoundTrip() throws CharacterCodingException { final CharsetDecoder dec = charset.newDecoder(); final CharsetEncoder enc = charset.newEncoder(); final byte[] b1 = new byte[256]; for (int i = 0; i < b1.length; i++) b1[i] = (byte) i; final ByteBuffer bb1 = ByteBuffer.wrap(b1); final CharBuffer cb = dec.decode(bb1); final ByteBuffer bb2 = enc.encode(cb); final byte[] b2 = bb2.array(); assertTrue(Arrays.equals(b1, b2)); }
public static final <T> T parseObject(byte[] input, int off, int len, CharsetDecoder charsetDecoder, Type clazz, Feature... features) { charsetDecoder.reset(); char[] chars = ThreadLocalCache.getChars((int) (((double) len) * ((double) charsetDecoder.maxCharsPerByte()))); ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len); CharBuffer charByte = CharBuffer.wrap(chars); IOUtils.decode(charsetDecoder, byteBuf, charByte); return parseObject(chars, charByte.position(), clazz, features); }
protected Object convert ( final Object value ) throws CharacterCodingException { if ( ! ( value instanceof byte[] ) ) { throw new IllegalArgumentException ( String.format ( "Unable to process data of type: %s. This transformer can only handle byte[]", value.getClass () ) ); } final byte[] data = (byte[])value; final CharsetDecoder decoder = this.charset.newDecoder (); final CharBuffer result = decoder.decode ( ByteBuffer.wrap ( data ) ); return result.toString (); }
private CharSequence decode(ByteBuffer buf) { // Decode a byte buffer into a CharBuffer CharBuffer isodcb = null; CharsetDecoder isodecoder = charset.newDecoder(); try { isodcb = isodecoder.decode(buf); } catch (CharacterCodingException e) { log.error(e); } return (CharSequence)isodcb; }
/** * Creates an CharsetDecoder which tests the encoding * * @return CharsetDecoder */ private static CharsetDecoder createCharsetDecoder() { Charset charset = Charset.forName(encoding); CharsetDecoder decoder = charset.newDecoder(); decoder.reset(); return decoder; }
private CharsetDecoder decoder() { CharsetDecoder dec = decTL.get(); if (dec == null) { dec = cs.newDecoder() .onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT); decTL.set(dec); } return dec; }
public CharsetDecoder newDecoder() { return new Decoder(this, CoderHolder.DEC0208, null); }
public CharsetDecoder newDecoder() { return new SingleByte.Decoder(this, b2c); }
@Override @DefinedBy(Api.COMPILER) public Reader openReader(boolean ignoreEncodingErrors) throws IOException { CharsetDecoder decoder = fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors); return new InputStreamReader(openInputStream(), decoder); }
public CharsetDecoder newDecoder() { return new Decoder(this); }
public CharsetDecoder newDecoder() { return new UTF_32Coder.Decoder(this, UTF_32Coder.LITTLE); }
public CharsetDecoder newDecoder() { initb2c(); return new DoubleByte.Decoder(this, b2c, b2cSB, 0x40, 0xfc); }
public CharsetDecoder newDecoder() { throw new Error("Decoder is not implemented for X11SunUnicode_0 Charset"); }
protected CharsetDecoder initialValue() { return Charset.forName("UTF-8").newDecoder(). onMalformedInput(CodingErrorAction.REPORT). onUnmappableCharacter(CodingErrorAction.REPORT); }
static char[] decode(Charset cs, byte[] ba, int off, int len) { // (1)We never cache the "external" cs, the only benefit of creating // an additional StringDe/Encoder object to wrap it is to share the // de/encode() method. These SD/E objects are short-lifed, the young-gen // gc should be able to take care of them well. But the best approash // is still not to generate them if not really necessary. // (2)The defensive copy of the input byte/char[] has a big performance // impact, as well as the outgoing result byte/char[]. Need to do the // optimization check of (sm==null && classLoader0==null) for both. // (3)getClass().getClassLoader0() is expensive // (4)There might be a timing gap in isTrusted setting. getClassLoader0() // is only chcked (and then isTrusted gets set) when (SM==null). It is // possible that the SM==null for now but then SM is NOT null later // when safeTrim() is invoked...the "safe" way to do is to redundant // check (... && (isTrusted || SM == null || getClassLoader0())) in trim // but it then can be argued that the SM is null when the opertaion // is started... CharsetDecoder cd = cs.newDecoder(); int en = scale(len, cd.maxCharsPerByte()); char[] ca = new char[en]; if (len == 0) return ca; boolean isTrusted = false; if (System.getSecurityManager() != null) { if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) { ba = Arrays.copyOfRange(ba, off, off + len); off = 0; } } cd.onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .reset(); if (cd instanceof ArrayDecoder) { int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca); return safeTrim(ca, clen, cs, isTrusted); } else { ByteBuffer bb = ByteBuffer.wrap(ba, off, len); CharBuffer cb = CharBuffer.wrap(ca); try { CoderResult cr = cd.decode(bb, cb, true); if (!cr.isUnderflow()) cr.throwException(); cr = cd.flush(cb); if (!cr.isUnderflow()) cr.throwException(); } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } return safeTrim(ca, cb.position(), cs, isTrusted); } }
@Override public CharsetDecoder newDecoder() { throw new UnsupportedOperationException(); }
public CharsetDecoder newDecoder() { return new SniffingDecoder (this); }