Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.undertow.servlet.spec;
  
Real servlet print writer functionality, that is not limited by extending java.io.PrintWriter

Author(s):
Stuart Douglas
 
 public class ServletPrintWriter {
 
     private static final char[] EMPTY_CHAR = {};
 
     private final ServletOutputStreamImpl outputStream;
     private final String charset;
     private CharsetEncoder charsetEncoder;
     private boolean error = false;
     private boolean closed = false;
     private char[] underflow;
 
     public ServletPrintWriter(final ServletOutputStreamImpl outputStreamfinal String charsetthrows UnsupportedEncodingException {
         this. = charset;
         this. = outputStream;
 
         //for some known charset we get optimistic and hope that
         //only ascii will be output
         //in this case we can avoid creating the encoder altogether
         if (!charset.equalsIgnoreCase("utf-8") &&
                 !charset.equalsIgnoreCase("iso-8859-1")) {
             createEncoder();
         }
     }
 
     private void createEncoder() {
         this. = Charset.forName(this.).newEncoder();
         //replace malformed and unmappable with question marks
     }
 
     public void flush() {
         try {
             .flush();
         } catch (IOException e) {
              = true;
         }
     }
 
     public void close() {
 
             return;
         }
         if () {
             return;
         }
          = true;
         try {
             boolean done = false;
             CharBuffer buffer;
             if ( == null) {
                 buffer = CharBuffer.wrap();
             } else {
                 buffer = CharBuffer.wrap();
                  = null;
             }
             if ( != null) {
                 do {
                     ByteBuffer out = .underlyingBuffer();
                     if (out == null) {
                         //servlet output stream has already been closed
                          = true;
                         return;
                     }
                     CoderResult result = .encode(bufferouttrue);
                     if (result.isOverflow()) {
                         .flushInternal();
                         if (out.remaining() == 0) {
                             .close();
                              = true;
                             return;
                         }
                     } else {
                         done = true;
                     }
                 } while (!done);
             }
             .close();
        } catch (IOException e) {
             = true;
        }
    }
    public boolean checkError() {
        return ;
    }
    public void write(final CharBuffer input) {
        ByteBuffer buffer = .underlyingBuffer();
        if (buffer == null) {
            //stream has been closed
             = true;
            return;
        }
        try {
            if (!buffer.hasRemaining()) {
                .flushInternal();
                if (!buffer.hasRemaining()) {
                     = true;
                    return;
                }
            }
            if ( == null) {
                //fast path, basically we are hoping this is ascii only
                int remaining = buffer.remaining();
                boolean ok = true;
                //so we have a pure ascii buffer, just write it out and skip all the encoder cost
                while (input.hasRemaining()) {
                    if (!buffer.hasRemaining()) {
                        .flushInternal();
                    }
                    char c = input.get();
                    if (c > 127) {
                        ok = false;
                        input.position(input.position() - 1); //push the character back
                        break;
                    }
                    buffer.put((bytec);
                }
                .updateWritten(remaining - buffer.remaining());
                if (ok) {
                    return;
                }
                createEncoder();
            }
            final CharBuffer cb;
            if ( == null) {
                cb = input;
            } else {
                char[] newArray = new char[. + input.remaining()];
                System.arraycopy(, 0, newArray, 0, .);
                input.get(newArray.input.remaining());
                cb = CharBuffer.wrap(newArray);
                 = null;
            }
            int last = -1;
            while (cb.hasRemaining()) {
                int remaining = buffer.remaining();
                CoderResult result = .encode(cbbufferfalse);
                .updateWritten(remaining - buffer.remaining());
                if (result.isOverflow() || !buffer.hasRemaining()) {
                    .flushInternal();
                    if (!buffer.hasRemaining()) {
                         = true;
                        return;
                    }
                }
                if (result.isUnderflow()) {
                     = new char[cb.remaining()];
                    cb.get();
                    return;
                }
                if (result.isError()) {
                     = true;
                    return;
                }
                if (result.isUnmappable()) {
                    //this should not happen
                     = true;
                    return;
                }
                if (last == cb.remaining()) {
                     = new char[cb.remaining()];
                    cb.get();
                    return;
                }
                last = cb.remaining();
            }
        } catch (IOException e) {
             = true;
        }
    }
    public void write(final int c) {
        final CharBuffer cb = CharBuffer.wrap(Character.toString((charc));
        write(cb);
    }
    public void write(final char[] buffinal int offfinal int len) {
        final CharBuffer cb = CharBuffer.wrap(bufofflen);
        write(cb);
    }
    public void write(final char[] buf) {
        final CharBuffer cb = CharBuffer.wrap(buf);
        write(cb);
    }
    public void write(final String sfinal int offfinal int len) {
        final CharBuffer cb = CharBuffer.wrap(soffoff + len);
        write(cb);
    }
    public void write(final String s) {
        final CharBuffer cb = CharBuffer.wrap(s);
        write(cb);
    }
    public void print(final boolean b) {
        final CharBuffer cb = CharBuffer.wrap(Boolean.toString(b));
        write(cb);
    }
    public void print(final char c) {
        final CharBuffer cb = CharBuffer.wrap(Character.toString(c));
        write(cb);
    }
    public void print(final int i) {
        final CharBuffer cb = CharBuffer.wrap(Integer.toString(i));
        write(cb);
    }
    public void print(final long l) {
        final CharBuffer cb = CharBuffer.wrap(Long.toString(l));
        write(cb);
    }
    public void print(final float f) {
        final CharBuffer cb = CharBuffer.wrap(Float.toString(f));
        write(cb);
    }
    public void print(final double d) {
        final CharBuffer cb = CharBuffer.wrap(Double.toString(d));
        write(cb);
    }
    public void print(final char[] s) {
        final CharBuffer cb = CharBuffer.wrap(s);
        write(cb);
    }
    public void print(final String s) {
        final CharBuffer cb = CharBuffer.wrap(s == null ? "null" : s);
        write(cb);
    }
    public void print(final Object obj) {
        final CharBuffer cb = CharBuffer.wrap(obj == null ? "null" : obj.toString());
        write(cb);
    }
    public void println() {
        print('\n');
    }
    public void println(final boolean b) {
        print(b);
        print('\n');
    }
    public void println(final char c) {
        print(c);
        print('\n');
    }
    public void println(final int i) {
        print(i);
        print('\n');
    }
    public void println(final long l) {
        print(l);
        print('\n');
    }
    public void println(final float f) {
        print(f);
        print('\n');
    }
    public void println(final double d) {
        print(d);
        print('\n');
    }
    public void println(final char[] s) {
        print(s);
        print('\n');
    }
    public void println(final String s) {
        print(s);
        print('\n');
    }
    public void println(final Object obj) {
        print(obj);
        print('\n');
    }
    public void printf(final String formatfinal Object... args) {
        print(String.format(formatargs));
    }
    public void printf(final Locale lfinal String formatfinal Object... args) {
        print(String.format(lformatargs));
    }
    public void format(final String formatfinal Object... args) {
        printf(formatargs);
    }
    public void format(final Locale lfinal String formatfinal Object... args) {
        printf(lformatargs);
    }
    public void append(final CharSequence csq) {
        if (csq == null)
            write("null");
        else
            write(csq.toString());
    }
    public void append(final CharSequence csqfinal int startfinal int end) {
        CharSequence cs = (csq == null ? "null" : csq);
        write(cs.subSequence(startend).toString());
    }
    public void append(final char c) {
        write(c);
    }
New to GrepCode? Check out our FAQ X