/vertx-template-engines/vertx-web-templ-rocker/src/main/java/io/vertx/ext/web/templ/rocker/impl/VertxBufferOutput.java

https://github.com/vert-x3/vertx-web · Java · 64 lines · 34 code · 12 blank · 18 comment · 0 complexity · 9ab1e889dc4ca0f02d80d7e23a143793 MD5 · raw file

  1. /*
  2. * Copyright 2014 Red Hat, Inc.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Apache License v2.0 which accompanies this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. *
  11. * The Apache License v2.0 is available at
  12. * http://www.opensource.org/licenses/apache2.0.php
  13. *
  14. * You may elect to redistribute this code under either of these licenses.
  15. */
  16. package io.vertx.ext.web.templ.rocker.impl;
  17. import com.fizzed.rocker.ContentType;
  18. import com.fizzed.rocker.runtime.AbstractRockerOutput;
  19. import io.vertx.core.buffer.Buffer;
  20. import java.nio.charset.Charset;
  21. /**
  22. * @author <a href="mailto:xianguang.zhou@outlook.com">Xianguang Zhou</a>
  23. */
  24. public class VertxBufferOutput extends AbstractRockerOutput<VertxBufferOutput> {
  25. public static final VertxBufferOutputFactory FACTORY = new VertxBufferOutputFactory();
  26. private static int maxRuntimeSize = 0;
  27. private final Buffer buffer;
  28. public VertxBufferOutput(ContentType contentType, String charsetName) {
  29. super(contentType, charsetName, 0);
  30. this.buffer = Buffer.buffer(maxRuntimeSize);
  31. }
  32. public VertxBufferOutput(ContentType contentType, Charset charset) {
  33. super(contentType, charset, 0);
  34. this.buffer = Buffer.buffer(maxRuntimeSize);
  35. }
  36. public Buffer getBuffer() {
  37. maxRuntimeSize = Math.max(maxRuntimeSize, buffer.length());
  38. return buffer;
  39. }
  40. @Override
  41. public VertxBufferOutput w(String string) {
  42. buffer.appendString(string, charset.name());
  43. byteLength = buffer.length();
  44. return this;
  45. }
  46. @Override
  47. public VertxBufferOutput w(byte[] bytes) {
  48. buffer.appendBytes(bytes);
  49. byteLength = buffer.length();
  50. return this;
  51. }
  52. }