Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.core.io;
  
  import java.io.*;
  
Efficient alternative to java.io.StringWriter, based on using segmented internal buffer. Initial input buffer is also recyclable.

This class is most useful when serializing JSON content as a String: if so, instance of this class can be given as the writer to JsonGenerator.

 
 public final class SegmentedStringWriter
     extends Writer
 {
     final protected TextBuffer _buffer;
 
     {
         super();
          = new TextBuffer(br);
     }
 
     /*
     /**********************************************************
     /* java.io.Writer implementation
     /**********************************************************
      */
 
     @Override
     public Writer append(char c)
     {
         write(c);
         return this;
     }
 
     @Override
     public Writer append(CharSequence csq)
     {
 	String str = csq.toString();
 	.append(str, 0, str.length());
         return this;
     }
 
     @Override
     public Writer append(CharSequence csqint startint end)
     {
 	String str = csq.subSequence(startend).toString();
 	.append(str, 0, str.length());
         return this;
     }
 
     @Override public void close() { } // NOP
 
     @Override public void flush() { } // NOP
 
     @Override
     public void write(char[] cbuf) {
         .append(cbuf, 0, cbuf.length);
     }
 
     @Override
     public void write(char[] cbufint offint len) {
         .append(cbufofflen);
     }
 
     @Override
     public void write(int c) {
         .append((charc);
     }
 
     @Override
     public void write(String str) { .append(str, 0, str.length()); }
 
     @Override
     public void write(String strint offint len) {
         .append(strofflen);
     }
 
     /*
     /**********************************************************
     /* Extended API
     /**********************************************************
      */

    
Main access method that will construct a String that contains all the contents, release all internal buffers we may have, and return result String. Note that the method is not idempotent -- if called second time, will just return an empty String.
 
     public String getAndClear()
     {
         String result = .contentsAsString();
         .releaseBuffers();
        return result;
    }
New to GrepCode? Check out our FAQ X