comparison src/org/eclipse/jetty/http/HttpGenerator.java @ 1038:b71ad168fe34

rename Buffer.length() to remaining()
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 03 Nov 2016 22:16:11 -0600
parents b87f97f6418a
children a7319f14ba1e
comparison
equal deleted inserted replaced
1037:3c4c7cc7904f 1038:b71ad168fe34
143 return; 143 return;
144 } 144 }
145 _last = last; 145 _last = last;
146 146
147 // Handle any unfinished business? 147 // Handle any unfinished business?
148 if (_content!=null && _content.length()>0 || _bufferChunked) 148 if (_content!=null && _content.remaining()>0 || _bufferChunked)
149 { 149 {
150 if (_endp.isOutputShutdown()) 150 if (_endp.isOutputShutdown())
151 throw new EofException(); 151 throw new EofException();
152 flushBuffer(); 152 flushBuffer();
153 if (_content != null && _content.length()>0) 153 if (_content != null && _content.remaining()>0)
154 { 154 {
155 if (_bufferChunked) 155 if (_bufferChunked)
156 { 156 {
157 Buffer nc = _buffers.getBuffer(_content.length()+CHUNK_SPACE+content.length()); 157 Buffer nc = _buffers.getBuffer(_content.remaining()+CHUNK_SPACE+content.remaining());
158 nc.put(_content); 158 nc.put(_content);
159 nc.put(HttpTokens.CRLF); 159 nc.put(HttpTokens.CRLF);
160 BufferUtil.putHexInt(nc, content.length()); 160 BufferUtil.putHexInt(nc, content.remaining());
161 nc.put(HttpTokens.CRLF); 161 nc.put(HttpTokens.CRLF);
162 nc.put(content); 162 nc.put(content);
163 content=nc; 163 content=nc;
164 } 164 }
165 else 165 else
166 { 166 {
167 Buffer nc = _buffers.getBuffer(_content.length()+content.length()); 167 Buffer nc = _buffers.getBuffer(_content.remaining()+content.remaining());
168 nc.put(_content); 168 nc.put(_content);
169 nc.put(content); 169 nc.put(content);
170 content=nc; 170 content=nc;
171 } 171 }
172 } 172 }
173 } 173 }
174 174
175 _content = content; 175 _content = content;
176 _contentWritten += content.length(); 176 _contentWritten += content.remaining();
177 177
178 // Handle the _content 178 // Handle the _content
179 if (_head) 179 if (_head)
180 { 180 {
181 content.clear(); 181 content.clear();
182 _content=null; 182 _content=null;
183 } 183 }
184 else if (_endp != null && (_buffer==null || _buffer.length()==0) && _content.length() > 0 && (_last || isCommitted() && _content.length()>1024)) 184 else if (_endp != null && (_buffer==null || _buffer.remaining()==0) && _content.remaining() > 0 && (_last || isCommitted() && _content.remaining()>1024))
185 { 185 {
186 _bypass = true; 186 _bypass = true;
187 } 187 }
188 else if (!_bufferChunked) 188 else if (!_bufferChunked)
189 { 189 {
192 _buffer = _buffers.getBuffer(); 192 _buffer = _buffers.getBuffer();
193 193
194 // Copy _content to buffer; 194 // Copy _content to buffer;
195 int len=_buffer.put(_content); 195 int len=_buffer.put(_content);
196 _content.skip(len); 196 _content.skip(len);
197 if (_content.length() == 0) 197 if (_content.remaining() == 0)
198 _content = null; 198 _content = null;
199 } 199 }
200 } 200 }
201 201
202 /* ------------------------------------------------------------ */ 202 /* ------------------------------------------------------------ */
213 if (_last || _state==STATE_END) 213 if (_last || _state==STATE_END)
214 return -1; 214 return -1;
215 215
216 // Handle any unfinished business? 216 // Handle any unfinished business?
217 Buffer content = _content; 217 Buffer content = _content;
218 if (content != null && content.length()>0 || _bufferChunked) 218 if (content != null && content.remaining()>0 || _bufferChunked)
219 { 219 {
220 flushBuffer(); 220 flushBuffer();
221 if (content != null && content.length()>0 || _bufferChunked) 221 if (content != null && content.remaining()>0 || _bufferChunked)
222 throw new IllegalStateException("FULL"); 222 throw new IllegalStateException("FULL");
223 } 223 }
224 224
225 // we better check we have a buffer 225 // we better check we have a buffer
226 if (_buffer == null) 226 if (_buffer == null)
227 _buffer = _buffers.getBuffer(); 227 _buffer = _buffers.getBuffer();
228 228
229 _contentWritten-=_buffer.length(); 229 _contentWritten-=_buffer.remaining();
230 230
231 // Handle the _content 231 // Handle the _content
232 if (_head) 232 if (_head)
233 return Integer.MAX_VALUE; 233 return Integer.MAX_VALUE;
234 234
261 _header.put(HttpTokens.CRLF); 261 _header.put(HttpTokens.CRLF);
262 262
263 try 263 try
264 { 264 {
265 // nasty semi busy flush! 265 // nasty semi busy flush!
266 while(_header.length()>0) 266 while(_header.remaining()>0)
267 { 267 {
268 int len = _endp.flush(_header); 268 int len = _endp.flush(_header);
269 if (len<0) 269 if (len<0)
270 throw new EofException(); 270 throw new EofException();
271 if (len==0) 271 if (len==0)
791 _buffer.setPutIndex(CHUNK_SPACE); 791 _buffer.setPutIndex(CHUNK_SPACE);
792 _buffer.setGetIndex(CHUNK_SPACE); 792 _buffer.setGetIndex(CHUNK_SPACE);
793 793
794 // Special case handling for small left over buffer from 794 // Special case handling for small left over buffer from
795 // an addContent that caused a buffer flush. 795 // an addContent that caused a buffer flush.
796 if (_content != null && _content.length() < _buffer.space() && _state != STATE_FLUSHING) 796 if (_content != null && _content.remaining() < _buffer.space() && _state != STATE_FLUSHING)
797 { 797 {
798 _buffer.put(_content); 798 _buffer.put(_content);
799 _content.clear(); 799 _content.clear();
800 _content=null; 800 _content=null;
801 } 801 }
802 } 802 }
803 } 803 }
804 804
805 // Are we completely finished for now? 805 // Are we completely finished for now?
806 if (!_needCRLF && !_needEOC && (_content==null || _content.length()==0)) 806 if (!_needCRLF && !_needEOC && (_content==null || _content.remaining()==0))
807 { 807 {
808 if (_state == STATE_FLUSHING) 808 if (_state == STATE_FLUSHING)
809 _state = STATE_END; 809 _state = STATE_END;
810 810
811 if (_state==STATE_END && _persistent != null && !_persistent && _status!=100 && _method==null) 811 if (_state==STATE_END && _persistent != null && !_persistent && _status!=100 && _method==null)
835 } 835 }
836 } 836 }
837 837
838 private int flushMask() 838 private int flushMask()
839 { 839 {
840 return ((_header != null && _header.length() > 0)?4:0) 840 return ((_header != null && _header.remaining() > 0)?4:0)
841 | ((_buffer != null && _buffer.length() > 0)?2:0) 841 | ((_buffer != null && _buffer.remaining() > 0)?2:0)
842 | ((_bypass && _content != null && _content.length() > 0)?1:0); 842 | ((_bypass && _content != null && _content.remaining() > 0)?1:0);
843 } 843 }
844 844
845 private void prepareBuffers() 845 private void prepareBuffers()
846 { 846 {
847 // if we are not flushing an existing chunk 847 // if we are not flushing an existing chunk
848 if (!_bufferChunked) 848 if (!_bufferChunked)
849 { 849 {
850 // Refill buffer if possible 850 // Refill buffer if possible
851 if (!_bypass && _content != null && _content.length() > 0 && _buffer != null && _buffer.space() > 0) 851 if (!_bypass && _content != null && _content.remaining() > 0 && _buffer != null && _buffer.space() > 0)
852 { 852 {
853 int len = _buffer.put(_content); 853 int len = _buffer.put(_content);
854 _content.skip(len); 854 _content.skip(len);
855 if (_content.length() == 0) 855 if (_content.remaining() == 0)
856 _content = null; 856 _content = null;
857 } 857 }
858 858
859 // Chunk buffer if need be 859 // Chunk buffer if need be
860 if (_contentLength == HttpTokens.CHUNKED_CONTENT) 860 if (_contentLength == HttpTokens.CHUNKED_CONTENT)
861 { 861 {
862 if (_bypass && (_buffer==null||_buffer.length()==0) && _content!=null) 862 if (_bypass && (_buffer==null||_buffer.remaining()==0) && _content!=null)
863 { 863 {
864 // this is a bypass write 864 // this is a bypass write
865 int size = _content.length(); 865 int size = _content.remaining();
866 _bufferChunked = true; 866 _bufferChunked = true;
867 867
868 if (_header == null) 868 if (_header == null)
869 _header = _buffers.getHeader(); 869 _header = _buffers.getHeader();
870 870
871 // if we need CRLF add this to header 871 // if we need CRLF add this to header
872 if (_needCRLF) 872 if (_needCRLF)
873 { 873 {
874 if (_header.length() > 0) throw new IllegalStateException("EOC"); 874 if (_header.remaining() > 0) throw new IllegalStateException("EOC");
875 _header.put(HttpTokens.CRLF); 875 _header.put(HttpTokens.CRLF);
876 _needCRLF = false; 876 _needCRLF = false;
877 } 877 }
878 // Add the chunk size to the header 878 // Add the chunk size to the header
879 BufferUtil.putHexInt(_header, size); 879 BufferUtil.putHexInt(_header, size);
882 // Need a CRLF after the content 882 // Need a CRLF after the content
883 _needCRLF = true; 883 _needCRLF = true;
884 } 884 }
885 else if (_buffer!=null) 885 else if (_buffer!=null)
886 { 886 {
887 int size = _buffer.length(); 887 int size = _buffer.remaining();
888 if (size > 0) 888 if (size > 0)
889 { 889 {
890 // Prepare a chunk! 890 // Prepare a chunk!
891 _bufferChunked = true; 891 _bufferChunked = true;
892 892
912 if (_header == null) 912 if (_header == null)
913 _header = _buffers.getHeader(); 913 _header = _buffers.getHeader();
914 914
915 if (_needCRLF) 915 if (_needCRLF)
916 { 916 {
917 if (_header.length() > 0) throw new IllegalStateException("EOC"); 917 if (_header.remaining() > 0) throw new IllegalStateException("EOC");
918 _header.put(HttpTokens.CRLF); 918 _header.put(HttpTokens.CRLF);
919 _needCRLF = false; 919 _needCRLF = false;
920 } 920 }
921 BufferUtil.putHexInt(_header, size); 921 BufferUtil.putHexInt(_header, size);
922 _header.put(HttpTokens.CRLF); 922 _header.put(HttpTokens.CRLF);
929 _needCRLF = true; 929 _needCRLF = true;
930 } 930 }
931 } 931 }
932 932
933 // If we need EOC and everything written 933 // If we need EOC and everything written
934 if (_needEOC && (_content == null || _content.length() == 0)) 934 if (_needEOC && (_content == null || _content.remaining() == 0))
935 { 935 {
936 if (_header == null && _buffer == null) 936 if (_header == null && _buffer == null)
937 _header = _buffers.getHeader(); 937 _header = _buffers.getHeader();
938 938
939 if (_needCRLF) 939 if (_needCRLF)
973 } 973 }
974 } 974 }
975 } 975 }
976 } 976 }
977 977
978 if (_content != null && _content.length() == 0) 978 if (_content != null && _content.remaining() == 0)
979 _content = null; 979 _content = null;
980 980
981 } 981 }
982 982
983 @Override 983 @Override
987 Buffer buffer=_buffer; 987 Buffer buffer=_buffer;
988 Buffer content=_content; 988 Buffer content=_content;
989 return String.format("%s{s=%d,h=%d,b=%d,c=%d}", 989 return String.format("%s{s=%d,h=%d,b=%d,c=%d}",
990 getClass().getSimpleName(), 990 getClass().getSimpleName(),
991 _state, 991 _state,
992 header == null ? -1 : header.length(), 992 header == null ? -1 : header.remaining(),
993 buffer == null ? -1 : buffer.length(), 993 buffer == null ? -1 : buffer.remaining(),
994 content == null ? -1 : content.length()); 994 content == null ? -1 : content.remaining());
995 } 995 }
996 } 996 }