comparison src/org/eclipse/jetty/server/AbstractHttpConnection.java @ 1010:2712133d5bce

simplify Buffer code
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 23 Oct 2016 22:23:50 -0600
parents 945568ee77ac
children 6939226e0ac4
comparison
equal deleted inserted replaced
1009:c3a04bded909 1010:2712133d5bce
815 throw new IOException("Closed"); 815 throw new IOException("Closed");
816 PrintWriter writer = getPrintWriter(null); 816 PrintWriter writer = getPrintWriter(null);
817 writer.print(s); 817 writer.print(s);
818 } 818 }
819 819
820 public void sendContent(Object content) throws IOException 820 public final void sendContent(InputStream in) throws IOException
821 { 821 {
822 Resource resource=null;
823
824 if (_closed) 822 if (_closed)
825 throw new IOException("Closed"); 823 throw new IOException("Closed");
826 824
827 if (_generator.isWritten()) 825 if (_generator.isWritten())
828 throw new IllegalStateException("!empty"); 826 throw new IllegalStateException("!empty");
829 827
830 if (content instanceof Resource) 828 try
831 { 829 {
832 resource=(Resource)content; 830 int max = _generator.prepareUncheckedAddContent();
833 _responseFields.putDateField(HttpHeaders.LAST_MODIFIED_BUFFER, resource.lastModified()); 831 Buffer buffer = _generator.getUncheckedBuffer();
834 content=resource.getInputStream(); 832
835 } 833 int len = buffer.readFrom(in,max);
836 834
837 // Process content. 835 while (len>=0)
838 if (content instanceof Buffer) 836 {
839 {
840 _generator.addContent((Buffer) content, HttpGenerator.LAST);
841 commitResponse(HttpGenerator.LAST);
842 }
843 else if (content instanceof InputStream)
844 {
845 InputStream in = (InputStream)content;
846
847 try
848 {
849 int max = _generator.prepareUncheckedAddContent();
850 Buffer buffer = _generator.getUncheckedBuffer();
851
852 int len=buffer.readFrom(in,max);
853
854 while (len>=0)
855 {
856 _generator.completeUncheckedAddContent();
857 _out.flush();
858
859 max = _generator.prepareUncheckedAddContent();
860 buffer = _generator.getUncheckedBuffer();
861 len=buffer.readFrom(in,max);
862 }
863 _generator.completeUncheckedAddContent(); 837 _generator.completeUncheckedAddContent();
864 _out.flush(); 838 _out.flush();
865 } 839
866 finally 840 max = _generator.prepareUncheckedAddContent();
867 { 841 buffer = _generator.getUncheckedBuffer();
868 if (resource!=null) 842 len=buffer.readFrom(in,max);
869 resource.release(); 843 }
870 else 844 _generator.completeUncheckedAddContent();
871 in.close(); 845 _out.flush();
872 } 846 }
873 } 847 finally
874 else 848 {
875 throw new IllegalArgumentException("unknown content type?"); 849 in.close();
850 }
876 } 851 }
877 } 852 }
878 853
879 854
880 private final class HttpInput extends ServletInputStream 855 private final class HttpInput extends ServletInputStream