Mercurial Hosting > luan
comparison src/org/eclipse/jetty/server/AbstractHttpConnection.java @ 985:8fef34f665e7
remove HttpOutput
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 16 Oct 2016 23:47:23 -0600 |
parents | 23ec25435b8c |
children | 83cc6e05a58f |
comparison
equal
deleted
inserted
replaced
984:7b0fa315e835 | 985:8fef34f665e7 |
---|---|
19 package org.eclipse.jetty.server; | 19 package org.eclipse.jetty.server; |
20 | 20 |
21 import java.io.IOException; | 21 import java.io.IOException; |
22 import java.io.InputStream; | 22 import java.io.InputStream; |
23 import java.io.PrintWriter; | 23 import java.io.PrintWriter; |
24 import java.io.Writer; | |
24 | 25 |
25 import javax.servlet.DispatcherType; | 26 import javax.servlet.DispatcherType; |
26 import javax.servlet.RequestDispatcher; | 27 import javax.servlet.RequestDispatcher; |
27 import javax.servlet.ServletInputStream; | 28 import javax.servlet.ServletInputStream; |
28 import javax.servlet.ServletOutputStream; | 29 import javax.servlet.ServletOutputStream; |
44 import org.eclipse.jetty.http.MimeTypes; | 45 import org.eclipse.jetty.http.MimeTypes; |
45 import org.eclipse.jetty.io.AbstractConnection; | 46 import org.eclipse.jetty.io.AbstractConnection; |
46 import org.eclipse.jetty.io.Buffer; | 47 import org.eclipse.jetty.io.Buffer; |
47 import org.eclipse.jetty.io.BufferCache.CachedBuffer; | 48 import org.eclipse.jetty.io.BufferCache.CachedBuffer; |
48 import org.eclipse.jetty.io.Buffers; | 49 import org.eclipse.jetty.io.Buffers; |
50 import org.eclipse.jetty.io.ByteArrayBuffer; | |
49 import org.eclipse.jetty.io.EndPoint; | 51 import org.eclipse.jetty.io.EndPoint; |
50 import org.eclipse.jetty.io.EofException; | 52 import org.eclipse.jetty.io.EofException; |
51 import org.eclipse.jetty.io.RuntimeIOException; | 53 import org.eclipse.jetty.io.RuntimeIOException; |
54 import org.eclipse.jetty.util.ByteArrayOutputStream2; | |
52 import org.eclipse.jetty.util.QuotedStringTokenizer; | 55 import org.eclipse.jetty.util.QuotedStringTokenizer; |
53 import org.eclipse.jetty.util.StringUtil; | 56 import org.eclipse.jetty.util.StringUtil; |
54 import org.eclipse.jetty.util.URIUtil; | 57 import org.eclipse.jetty.util.URIUtil; |
55 import org.slf4j.Logger; | 58 import org.slf4j.Logger; |
56 import org.slf4j.LoggerFactory; | 59 import org.slf4j.LoggerFactory; |
713 AbstractHttpConnection.this.earlyEOF(); | 716 AbstractHttpConnection.this.earlyEOF(); |
714 } | 717 } |
715 } | 718 } |
716 | 719 |
717 | 720 |
718 public final class Output extends HttpOutput | 721 public final class Output extends ServletOutputStream |
719 { | 722 { |
720 Output() | 723 private boolean _closed; |
721 { | 724 private ByteArrayBuffer _onebyte; |
722 super(AbstractHttpConnection.this); | 725 |
723 } | 726 // These are held here for reuse by Writer |
727 String _characterEncoding; | |
728 Writer _converter; | |
729 char[] _chars; | |
730 ByteArrayOutputStream2 _bytes; | |
731 | |
732 public final void reopen() | |
733 { | |
734 _closed = false; | |
735 } | |
736 | |
737 @Override | |
738 public final void write(byte[] b, int off, int len) throws IOException | |
739 { | |
740 write(new ByteArrayBuffer(b,off,len)); | |
741 } | |
742 | |
743 @Override | |
744 public final void write(byte[] b) throws IOException | |
745 { | |
746 write(new ByteArrayBuffer(b)); | |
747 } | |
748 | |
749 @Override | |
750 public final void write(int b) throws IOException | |
751 { | |
752 if (_onebyte==null) | |
753 _onebyte = new ByteArrayBuffer(1); | |
754 else | |
755 _onebyte.clear(); | |
756 _onebyte.put((byte)b); | |
757 write(_onebyte); | |
758 } | |
759 | |
760 private void write(Buffer buffer) throws IOException | |
761 { | |
762 if (_closed) | |
763 throw new IOException("Closed"); | |
764 if (!_generator.isOpen()) | |
765 throw new EofException(); | |
766 | |
767 // Block until we can add _content. | |
768 while (_generator.isBufferFull()) | |
769 { | |
770 _generator.blockForOutput(getMaxIdleTime()); | |
771 if (_closed) | |
772 throw new IOException("Closed"); | |
773 if (!_generator.isOpen()) | |
774 throw new EofException(); | |
775 } | |
776 | |
777 // Add the _content | |
778 _generator.addContent(buffer, HttpGenerator.MORE); | |
779 | |
780 // Have to flush and complete headers? | |
781 | |
782 if (_generator.isAllContentWritten()) | |
783 { | |
784 flush(); | |
785 close(); | |
786 } | |
787 else if (_generator.isBufferFull()) | |
788 commitResponse(HttpGenerator.MORE); | |
789 | |
790 // Block until our buffer is free | |
791 while (buffer.length() > 0 && _generator.isOpen()) | |
792 { | |
793 _generator.blockForOutput(getMaxIdleTime()); | |
794 } | |
795 } | |
796 | |
797 | |
724 | 798 |
725 /* ------------------------------------------------------------ */ | 799 /* ------------------------------------------------------------ */ |
726 /* | 800 /* |
727 * @see java.io.OutputStream#close() | 801 * @see java.io.OutputStream#close() |
728 */ | 802 */ |
729 @Override | 803 @Override |
730 public void close() throws IOException | 804 public void close() throws IOException |
731 { | 805 { |
732 if (isClosed()) | 806 if (_closed) |
733 return; | 807 return; |
734 | 808 |
735 if (!super._generator.isCommitted()) | 809 if (!_generator.isCommitted()) |
736 commitResponse(HttpGenerator.LAST); | 810 commitResponse(HttpGenerator.LAST); |
737 else | 811 else |
738 flushResponse(); | 812 flushResponse(); |
739 | 813 |
740 super.close(); | 814 _closed = true; |
741 } | 815 } |
742 | 816 |
743 | 817 |
744 /* ------------------------------------------------------------ */ | 818 /* ------------------------------------------------------------ */ |
745 /* | 819 /* |
746 * @see java.io.OutputStream#flush() | 820 * @see java.io.OutputStream#flush() |
747 */ | 821 */ |
748 @Override | 822 @Override |
749 public void flush() throws IOException | 823 public void flush() throws IOException |
750 { | 824 { |
751 if (!super._generator.isCommitted()) | 825 if (!_generator.isCommitted()) |
752 commitResponse(HttpGenerator.MORE); | 826 commitResponse(HttpGenerator.MORE); |
753 super.flush(); | 827 _generator.flush(getMaxIdleTime()); |
754 } | 828 } |
755 | 829 |
756 /* ------------------------------------------------------------ */ | 830 /* ------------------------------------------------------------ */ |
757 /* | 831 /* |
758 * @see javax.servlet.ServletOutputStream#print(java.lang.String) | 832 * @see javax.servlet.ServletOutputStream#print(java.lang.String) |
759 */ | 833 */ |
760 @Override | 834 @Override |
761 public void print(String s) throws IOException | 835 public void print(String s) throws IOException |
762 { | 836 { |
763 if (isClosed()) | 837 if (_closed) |
764 throw new IOException("Closed"); | 838 throw new IOException("Closed"); |
765 PrintWriter writer=getPrintWriter(null); | 839 PrintWriter writer = getPrintWriter(null); |
766 writer.print(s); | 840 writer.print(s); |
767 } | 841 } |
768 | 842 |
769 public void sendResponse(Buffer response) throws IOException | 843 public void sendResponse(Buffer response) throws IOException |
770 { | 844 { |
771 super._generator.sendResponse(response); | 845 _generator.sendResponse(response); |
772 } | 846 } |
773 | 847 |
774 public void sendContent(Object content) throws IOException | 848 public void sendContent(Object content) throws IOException |
775 { | 849 { |
776 Resource resource=null; | 850 Resource resource=null; |
777 | 851 |
778 if (isClosed()) | 852 if (_closed) |
779 throw new IOException("Closed"); | 853 throw new IOException("Closed"); |
780 | 854 |
781 if (super._generator.isWritten()) | 855 if (_generator.isWritten()) |
782 throw new IllegalStateException("!empty"); | 856 throw new IllegalStateException("!empty"); |
783 | 857 |
784 if (content instanceof Resource) | 858 if (content instanceof Resource) |
785 { | 859 { |
786 resource=(Resource)content; | 860 resource=(Resource)content; |
789 } | 863 } |
790 | 864 |
791 // Process content. | 865 // Process content. |
792 if (content instanceof Buffer) | 866 if (content instanceof Buffer) |
793 { | 867 { |
794 super._generator.addContent((Buffer) content, HttpGenerator.LAST); | 868 _generator.addContent((Buffer) content, HttpGenerator.LAST); |
795 commitResponse(HttpGenerator.LAST); | 869 commitResponse(HttpGenerator.LAST); |
796 } | 870 } |
797 else if (content instanceof InputStream) | 871 else if (content instanceof InputStream) |
798 { | 872 { |
799 InputStream in = (InputStream)content; | 873 InputStream in = (InputStream)content; |
800 | 874 |
801 try | 875 try |
802 { | 876 { |
803 int max = super._generator.prepareUncheckedAddContent(); | 877 int max = _generator.prepareUncheckedAddContent(); |
804 Buffer buffer = super._generator.getUncheckedBuffer(); | 878 Buffer buffer = _generator.getUncheckedBuffer(); |
805 | 879 |
806 int len=buffer.readFrom(in,max); | 880 int len=buffer.readFrom(in,max); |
807 | 881 |
808 while (len>=0) | 882 while (len>=0) |
809 { | 883 { |
810 super._generator.completeUncheckedAddContent(); | 884 _generator.completeUncheckedAddContent(); |
811 _out.flush(); | 885 _out.flush(); |
812 | 886 |
813 max = super._generator.prepareUncheckedAddContent(); | 887 max = _generator.prepareUncheckedAddContent(); |
814 buffer = super._generator.getUncheckedBuffer(); | 888 buffer = _generator.getUncheckedBuffer(); |
815 len=buffer.readFrom(in,max); | 889 len=buffer.readFrom(in,max); |
816 } | 890 } |
817 super._generator.completeUncheckedAddContent(); | 891 _generator.completeUncheckedAddContent(); |
818 _out.flush(); | 892 _out.flush(); |
819 } | 893 } |
820 finally | 894 finally |
821 { | 895 { |
822 if (resource!=null) | 896 if (resource!=null) |