/** * Create a text response with known length. */ public static Response newFixedLengthResponse(IStatus status, String mimeType, String txt) { ContentType contentType = new ContentType(mimeType); if (txt == null) { return newFixedLengthResponse(status, mimeType, new ByteArrayInputStream(new byte[0]), 0); } else { byte[] bytes; try { CharsetEncoder newEncoder = Charset.forName(contentType.getEncoding()).newEncoder(); if (!newEncoder.canEncode(txt)) { contentType = contentType.tryUTF8(); } bytes = txt.getBytes(contentType.getEncoding()); } catch (UnsupportedEncodingException e) { NanoHTTPD.LOG.log(Level.SEVERE, "encoding problem, responding nothing", e); bytes = new byte[0]; } return newFixedLengthResponse(status, contentType.getContentTypeHeader(), new ByteArrayInputStream(bytes), bytes.length); } }
private void doWriteText(CharBuffer buffer, boolean finalFragment) throws IOException { CharsetEncoder encoder = B2CConverter.UTF_8.newEncoder(); do { CoderResult cr = encoder.encode(buffer, bb, true); if (cr.isError()) { cr.throwException(); } bb.flip(); if (buffer.hasRemaining()) { doWriteBytes(bb, false); } else { doWriteBytes(bb, finalFragment); } } while (buffer.hasRemaining()); // Reset - bb will be cleared in doWriteBytes() cb.clear(); }
/** * Converts the provided String to bytes using the * UTF-8 encoding. If <code>replace</code> is true, then * malformed input is replaced with the * substitution character, which is U+FFFD. Otherwise the * method throws a MalformedInputException. * @return ByteBuffer: bytes stores at ByteBuffer.array() * and length is ByteBuffer.limit() */ public static ByteBuffer encode(String string, boolean replace) throws CharacterCodingException { CharsetEncoder encoder = ENCODER_FACTORY.get(); if (replace) { encoder.onMalformedInput(CodingErrorAction.REPLACE); encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); } ByteBuffer bytes = encoder.encode(CharBuffer.wrap(string.toCharArray())); if (replace) { encoder.onMalformedInput(CodingErrorAction.REPORT); encoder.onUnmappableCharacter(CodingErrorAction.REPORT); } return bytes; }
/** * Called by CodeModel to store the specified file. * The callee must allocate a storage to store the specified file. * * <p> * The returned stream will be closed before the next file is * stored. So the callee can assume that only one OutputStream * is active at any given time. * * @param pkg * The package of the file to be written. * @param fileName * File name without the path. Something like * "Foo.java" or "Bar.properties" */ public Writer openSource( JPackage pkg, String fileName ) throws IOException { final OutputStreamWriter bw = encoding != null ? new OutputStreamWriter(openBinary(pkg,fileName), encoding) : new OutputStreamWriter(openBinary(pkg,fileName)); // create writer try { return new UnicodeEscapeWriter(bw) { // can't change this signature to Encoder because // we can't have Encoder in method signature private final CharsetEncoder encoder = EncoderFactory.createEncoder(bw.getEncoding()); @Override protected boolean requireEscaping(int ch) { // control characters if( ch<0x20 && " \t\r\n".indexOf(ch)==-1 ) return true; // check ASCII chars, for better performance if( ch<0x80 ) return false; return !encoder.canEncode((char)ch); } }; } catch( Throwable t ) { return new UnicodeEscapeWriter(bw); } }
/** * Returns a new byte array containing the characters of the specified * string encoded using the given charset. * * It is equivalent to <code>input.getBytes(charset)</code> except it has * workaround for the bug ID 61917. * * @see https://code.google.com/p/android/issues/detail?id=61917 */ //@formatter:off /* * The original code is available from * https://android.googlesource.com/platform/libcore/+/master/libdvm/src/main/java/java/lang/String.java */ //@formatter:on public static byte[] getBytes(String input, Charset charset) { CharBuffer chars = CharBuffer.wrap(input.toCharArray()); // @formatter:off CharsetEncoder encoder = charset.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); // @formatter:on ByteBuffer buffer; buffer = encode(chars.asReadOnlyBuffer(), encoder); byte[] bytes = new byte[buffer.limit()]; buffer.get(bytes); return bytes; }
private IoBuffer encodeProperties ( final IoSession session, final Map<String, String> properties ) throws CharacterCodingException { final IoBuffer data = IoBuffer.allocate ( 0 ); data.setAutoExpand ( true ); data.putInt ( properties.size () ); final CharsetEncoder encoder = getCharsetEncoder ( session ); for ( final Map.Entry<String, String> entry : properties.entrySet () ) { final String key = entry.getKey (); final String value = entry.getValue (); data.putString ( key, encoder ); data.put ( (byte)0x00 ); data.putString ( value, encoder ); data.put ( (byte)0x00 ); } data.flip (); return data; }
private void encodeProperties ( final IoBuffer data, final Map<String, String> properties ) throws ProtocolCodecException { final CharsetEncoder encoder = this.defaultCharset.newEncoder (); data.putUnsignedShort ( properties.size () ); for ( final Map.Entry<String, String> entry : properties.entrySet () ) { try { data.putPrefixedString ( entry.getKey (), encoder ); data.putPrefixedString ( entry.getValue (), encoder ); } catch ( final CharacterCodingException e ) { throw new ProtocolCodecException ( e ); } } }
public DefaultManagedHttpClientConnection( final String id, final int buffersize, final int fragmentSizeHint, final CharsetDecoder chardecoder, final CharsetEncoder charencoder, final MessageConstraints constraints, final ContentLengthStrategy incomingContentStrategy, final ContentLengthStrategy outgoingContentStrategy, final HttpMessageWriterFactory<HttpRequest> requestWriterFactory, final HttpMessageParserFactory<HttpResponse> responseParserFactory) { super(buffersize, fragmentSizeHint, chardecoder, charencoder, constraints, incomingContentStrategy, outgoingContentStrategy, requestWriterFactory, responseParserFactory); this.id = id; this.attributes = new ConcurrentHashMap<String, Object>(); }
public void test_error_0() throws Exception { Charset charset = Charset.forName("UTF-8"); CharsetEncoder charsetEncoder = new MockCharsetEncoder2(charset); Exception error = null; char[] chars = "abc".toCharArray(); try { encode(charsetEncoder, chars, 0, chars.length); } catch (Exception ex) { error = ex; } Assert.assertNotNull(error); }
/** * Creates new instance of SessionOutputBufferImpl. * * @param metrics HTTP transport metrics. * @param buffersize buffer size. Must be a positive number. * @param fragementSizeHint fragment size hint defining a minimal size of a fragment * that should be written out directly to the socket bypassing the session buffer. * Value {@code 0} disables fragment buffering. * @param charencoder charencoder to be used for encoding HTTP protocol elements. * If {@code null} simple type cast will be used for char to byte conversion. */ public SessionOutputBufferImpl( final HttpTransportMetricsImpl metrics, final int buffersize, final int fragementSizeHint, final CharsetEncoder charencoder) { super(); Args.positive(buffersize, "Buffer size"); Args.notNull(metrics, "HTTP transport metrcis"); this.metrics = metrics; this.buffer = new ByteArrayBuffer(buffersize); this.fragementSizeHint = fragementSizeHint >= 0 ? fragementSizeHint : 0; this.encoder = charencoder; }
static byte[] encode(Charset cs, char[] ca, int off, int len) { CharsetEncoder ce = cs.newEncoder(); int en = scale(len, ce.maxBytesPerChar()); byte[] ba = new byte[en]; if (len == 0) return ba; boolean isTrusted = false; if (System.getSecurityManager() != null) { if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) { ca = Arrays.copyOfRange(ca, off, off + len); off = 0; } } ce.onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .reset(); if (ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba); return safeTrim(ba, blen, cs, isTrusted); } else { ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca, off, len); 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) { throw new Error(x); } return safeTrim(ba, bb.position(), cs, isTrusted); } }
/** * Check if encode can handle the chars in this string. * */ protected boolean canEncode(String s) { final CharsetEncoder encoder = Charset.forName(System.getProperty("file.encoding")).newEncoder(); char[] chars = s.toCharArray(); for (int i=0; i<chars.length; i++) { if(!encoder.canEncode(chars[i])) { return false; } } return true; }
public CharsetEncoder newEncoder() { // Need to force the replacement byte to 0x3f // because JIS_X_0208_Encoder defines its own // alternative 2 byte substitution to permit it // to exist as a self-standing Encoder byte[] replacementBytes = { (byte)0x3f }; return new Encoder(this).replaceWith(replacementBytes); }
public static CharsetEncoder getCharsetEncoder ( final IoSession session ) { final Object charset = session.getAttribute ( ATTR_CHARSET, Charset.forName ( DEFAULT_CHARSET_NAME ) ); if ( charset instanceof Charset ) { return ( (Charset)charset ).newEncoder (); } else { return Charset.forName ( DEFAULT_CHARSET_NAME ).newEncoder (); } }
private static boolean isValidForCharset(String id, String charsetName) { CharsetEncoder encoder = null; try { Charset charset = Charset.forName(charsetName); encoder = charset.newEncoder(); } catch (UnsupportedCharsetException e) { Log.w("Unsupported charset: " + charsetName); } if (encoder != null && !encoder.canEncode(id)) { Log.v("Invalid id (contains invalid characters): " + id); return false; } return true; }
byte[] getBytes(String s) { CharsetEncoder ce = encoder().reset(); char[] ca = s.toCharArray(); int len = (int)(ca.length * ce.maxBytesPerChar()); byte[] ba = new byte[len]; if (len == 0) return ba; // UTF-8 only for now. Other ArrayDeocder only handles // CodingErrorAction.REPLACE mode. if (isUTF8 && ce instanceof ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.length, ba); if (blen == -1) // malformed throw new IllegalArgumentException("MALFORMED"); return Arrays.copyOf(ba, blen); } ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca); CoderResult cr = ce.encode(cb, bb, true); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); cr = ce.flush(bb); if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString()); if (bb.position() == ba.length) // defensive copy? return ba; else return Arrays.copyOf(ba, bb.position()); }
public CharsetEncoder newEncoder() { initc2b(); return new DoubleByte.Encoder(this, c2b, c2bIndex); }
public CharsetEncoder newEncoder() { return new Encoder(this); }
/** * Creates new instance of DefaultBHttpClientConnection. * * @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} 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} simple type cast will be used for char to byte conversion. * @param constraints Message constraints. If {@code null} * {@link ru.radiomayak.http.config.MessageConstraints#DEFAULT} will be used. * @param incomingContentStrategy incoming content length strategy. If {@code null} * {@link LaxContentLengthStrategy#INSTANCE} will be used. * @param outgoingContentStrategy outgoing content length strategy. If {@code null} * {@link StrictContentLengthStrategy#INSTANCE} will be used. * @param requestWriterFactory request writer factory. If {@code null} * {@link DefaultHttpRequestWriterFactory#INSTANCE} will be used. * @param responseParserFactory response parser factory. If {@code null} * {@link DefaultHttpResponseParserFactory#INSTANCE} will be used. */ public DefaultBHttpClientConnection( final int buffersize, final int fragmentSizeHint, final CharsetDecoder chardecoder, final CharsetEncoder charencoder, final ru.radiomayak.http.config.MessageConstraints constraints, final ru.radiomayak.http.entity.ContentLengthStrategy incomingContentStrategy, final ru.radiomayak.http.entity.ContentLengthStrategy outgoingContentStrategy, final HttpMessageWriterFactory<HttpRequest> requestWriterFactory, final HttpMessageParserFactory<HttpResponse> responseParserFactory) { super(buffersize, fragmentSizeHint, chardecoder, charencoder, constraints, incomingContentStrategy, outgoingContentStrategy); this.requestWriter = (requestWriterFactory != null ? requestWriterFactory : DefaultHttpRequestWriterFactory.INSTANCE).create(getSessionOutputBuffer()); this.responseParser = (responseParserFactory != null ? responseParserFactory : DefaultHttpResponseParserFactory.INSTANCE).create(getSessionInputBuffer(), constraints); }
public static byte[] encode(CharsetEncoder encoder, char[] chars, int off, int len) throws Exception { Method method = SerializeWriter.class.getDeclaredMethod("encode", CharsetEncoder.class, char[].class, int.class, int.class); method.setAccessible(true); return (byte[]) method.invoke(null, encoder, chars, off, len); }
static byte[] encode(Charset cs, byte coder, byte[] val) { if (cs == UTF_8) { return encodeUTF8(coder, val); } else if (cs == ISO_8859_1) { return encode8859_1(coder, val); } else if (cs == US_ASCII) { return encodeASCII(coder, val); } CharsetEncoder ce = cs.newEncoder(); // fastpath for ascii compatible if (coder == LATIN1 && (((ce instanceof ArrayEncoder) && ((ArrayEncoder)ce).isASCIICompatible() && !hasNegatives(val, 0, val.length)))) { return Arrays.copyOf(val, val.length); } int len = val.length >> coder; // assume LATIN1=0/UTF16=1; int en = scale(len, ce.maxBytesPerChar()); byte[] ba = new byte[en]; if (len == 0) { return ba; } boolean isTrusted = cs.getClass().getClassLoader0() == null || System.getSecurityManager() == null; ce.onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .reset(); if (ce instanceof ArrayEncoder) { if (!isTrusted) { val = Arrays.copyOf(val, val.length); } int blen = (coder == LATIN1 ) ? ((ArrayEncoder)ce).encodeFromLatin1(val, 0, len, ba) : ((ArrayEncoder)ce).encodeFromUTF16(val, 0, len, ba); if (blen != -1) { return safeTrim(ba, blen, isTrusted); } } char[] ca = (coder == LATIN1 ) ? StringLatin1.toChars(val) : StringUTF16.toChars(val); ByteBuffer bb = ByteBuffer.wrap(ba); CharBuffer cb = CharBuffer.wrap(ca, 0, len); 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) { throw new Error(x); } return safeTrim(ba, bb.position(), isTrusted); }
public CharsetEncoder newEncoder() { return new UTF_32Coder.Encoder(this, UTF_32Coder.LITTLE, false); }
public CharsetEncoder newEncoder() { return new SingleByte.Encoder(this, c2b, c2bIndex); }
public CharsetEncoder newEncoder() { return new UTF_32Coder.Encoder(this, UTF_32Coder.LITTLE, true); }
public CharsetEncoder newEncoder() { return new UTF_32Coder.Encoder(this, UTF_32Coder.BIG, false); }
@Override public CharsetEncoder newEncoder() { return new ProxyEncoder (delegates.get(0).newEncoder()); }
public CharsetEncoder getFontCharsetEncoder() { return encoder; }
public CharsetEncoder newEncoder() { return jis0212.newEncoder(); }
public CharsetEncoder getEncoder() { return encoder; }