List of usage examples for io.netty.util CharsetUtil encoder
public static CharsetEncoder encoder(Charset charset)
From source file:com.lambdaworks.redis.codec.StringCodec.java
License:Apache License
public void encode(String str, ByteBuf target) { if (str == null) { return;/*from w w w .j a va2 s .c o m*/ } if (utf8) { ByteBufUtil.writeUtf8(target, str); return; } if (ascii) { ByteBufUtil.writeAscii(target, str); return; } CharsetEncoder encoder = CharsetUtil.encoder(charset); int length = (int) ((double) str.length() * encoder.maxBytesPerChar()); target.ensureWritable(length); try { final ByteBuffer dstBuf = target.nioBuffer(0, length); final int pos = dstBuf.position(); CoderResult cr = encoder.encode(CharBuffer.wrap(str), dstBuf, true); if (!cr.isUnderflow()) { cr.throwException(); } cr = encoder.flush(dstBuf); if (!cr.isUnderflow()) { cr.throwException(); } target.writerIndex(target.writerIndex() + dstBuf.position() - pos); } catch (CharacterCodingException x) { throw new IllegalStateException(x); } }
From source file:com.lambdaworks.redis.codec.StringCodec.java
License:Apache License
@Override public int estimateSize(Object keyOrValue) { if (keyOrValue instanceof String) { CharsetEncoder encoder = CharsetUtil.encoder(charset); return (int) (encoder.averageBytesPerChar() * ((String) keyOrValue).length()); }//from w ww .j a v a 2 s .c o m return 0; }
From source file:com.lambdaworks.redis.codec.StringCodec.java
License:Apache License
/** * Compatibility implementation./* w ww. jav a2 s . com*/ * * @param key * @return */ private ByteBuffer encodeAndAllocateBuffer(String key) { if (key == null) { return ByteBuffer.wrap(EMPTY); } CharsetEncoder encoder = CharsetUtil.encoder(charset); ByteBuffer buffer = ByteBuffer.allocate((int) (encoder.maxBytesPerChar() * key.length())); ByteBuf byteBuf = Unpooled.wrappedBuffer(buffer); byteBuf.clear(); encode(key, byteBuf); buffer.limit(byteBuf.writerIndex()); return buffer; }
From source file:ratpack.groovy.template.internal.MarkupTemplateRenderer.java
License:Apache License
@Override public void render(Context ctx, MarkupTemplate template) throws Exception { String contentType = template.getContentType(); contentType = contentType == null ? ctx.get(MimeTypes.class).getContentType(template.getName()) : contentType;//from ww w .j a va 2 s.c o m try { Template compiledTemplate = engine.createTemplateByPath(template.getName()); Writable boundTemplate = compiledTemplate.make(template.getModel()); ByteBuf byteBuf = byteBufAllocator.directBuffer(); try { OutputStream outputStream = new ByteBufOutputStream(byteBuf); Writer writer = new OutputStreamWriter(outputStream, CharsetUtil.encoder(StandardCharsets.UTF_8)); boundTemplate.writeTo(writer); } catch (Exception e) { byteBuf.release(); throw e; } ctx.getResponse().send(contentType, byteBuf); } catch (IOException e) { ctx.error(e); } }
From source file:ratpack.groovy.template.Markup.java
License:Apache License
@Override public void render(Context context) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(out, CharsetUtil.encoder(getEncoding())); MarkupBuilder markupBuilder = new MarkupBuilder(writer); ClosureUtil.configureDelegateFirst(markupBuilder, markupBuilder, getDefinition()); context.getResponse().contentType(getContentType()).send(out.toByteArray()); }