title: Spring Cloud Gateway: Modified Response Body is Truncated
body: <p>I've been experimenting with Spring Cloud Gateway a bit and I'm trying to modify the response body.  Using a response decorator, I'm able to see that the body is modified, however, the buffer size is still the size of the original response.  Is there a way to expand the buffer size to the size of the new response body?</p>
  
  <pre><code>public class ModifyBodyGatewayFilterImpl implements GatewayFilter {
  
      @Override
      public Mono&lt;Void&gt; filter(ServerWebExchange exchange, GatewayFilterChain chain) {
  
          logger.info("\n\nexchange.getAttributes():\n {}\n\n", exchange.getAttributes());
  
          ServerHttpResponse response = exchange.getResponse();
          DataBufferFactory dataBufferFactory = response.bufferFactory();
  
          ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(response) {
  
              @Override
              public Mono&lt;Void&gt; writeWith(Publisher&lt;? extends DataBuffer&gt; body) {
  
                  Flux&lt;? extends DataBuffer&gt; flux = (Flux&lt;? extends DataBuffer&gt;) body;
  
                  Flux&lt;? extends DataBuffer&gt; f = flux.flatMap( dataBuffer  -&gt; {
  
                      byte[] origRespContent = new byte[dataBuffer.readableByteCount()];
                      dataBuffer.read(origRespContent);
  
                      System.out.println("content::: " + (new String(origRespContent)));
  
                      //alocating a new buffer size does not help.
                      DataBuffer b = dataBufferFactory.allocateBuffer(256);
                      b.write("0123456789abcdefg".getBytes());
  
                      return Flux.just(b);
                  });
  
                  return super.writeWith(f);
              }
          };
  
          ServerWebExchange swe = exchange.mutate().response(decoratedResponse).build();
          return chain.filter(swe);
      }
  }
  </code></pre>
  
  <p><strong>Example:</strong>  Expected re-written response is <code>0123456789abcdefg</code> If original content is 11 bytes <code>&lt;p&gt;test&lt;/p&gt;</code>, then the re-written response is truncated to <code>0123456789a</code>.</p>