comparison src/org/eclipse/jetty/io/nio/ChannelEndPoint.java @ 1048:2b769da7f67d

remove Buffer
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 07 Nov 2016 23:15:42 -0700
parents a8c92b0a08ed
children 4afdf0f0c5bc
comparison
equal deleted inserted replaced
1047:1accf965d51a 1048:2b769da7f67d
25 import java.nio.ByteBuffer; 25 import java.nio.ByteBuffer;
26 import java.nio.channels.ByteChannel; 26 import java.nio.channels.ByteChannel;
27 import java.nio.channels.GatheringByteChannel; 27 import java.nio.channels.GatheringByteChannel;
28 import java.nio.channels.SocketChannel; 28 import java.nio.channels.SocketChannel;
29 29
30 import org.eclipse.jetty.io.Buffer; 30 import org.eclipse.jetty.io.JBuffer;
31 import org.eclipse.jetty.io.EndPoint; 31 import org.eclipse.jetty.io.EndPoint;
32 import org.eclipse.jetty.util.StringUtil; 32 import org.eclipse.jetty.util.StringUtil;
33 import org.slf4j.Logger; 33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory; 34 import org.slf4j.LoggerFactory;
35 35
162 LOG.debug("close {}",this); 162 LOG.debug("close {}",this);
163 _channel.close(); 163 _channel.close();
164 } 164 }
165 165
166 @Override 166 @Override
167 public int fill(Buffer buffer) throws IOException 167 public int fill(JBuffer buffer) throws IOException
168 { 168 {
169 if (_ishut) 169 if (_ishut)
170 return -1; 170 return -1;
171 Buffer buf = buffer.buffer(); 171 JBuffer nbuf = buffer.buffer();
172 int len = 0; 172 int len = 0;
173 if (buf instanceof NIOBuffer) 173 final ByteBuffer bbuf = nbuf.getByteBuffer();
174 { 174
175 final NIOBuffer nbuf = (NIOBuffer)buf; 175 //noinspection SynchronizationOnLocalVariableOrMethodParameter
176 final ByteBuffer bbuf = nbuf.getByteBuffer(); 176 try
177 177 {
178 //noinspection SynchronizationOnLocalVariableOrMethodParameter 178 synchronized(bbuf)
179 {
180 try
181 {
182 bbuf.position(buffer.putIndex());
183 len = _channel.read(bbuf);
184 }
185 finally
186 {
187 buffer.setPutIndex(bbuf.position());
188 bbuf.position(0);
189 }
190 }
191
192 if (len<0 && isOpen())
193 {
194 if (!isInputShutdown())
195 shutdownInput();
196 if (isOutputShutdown())
197 _channel.close();
198 }
199 }
200 catch (IOException x)
201 {
202 LOG.debug("Exception while filling", x);
179 try 203 try
180 { 204 {
181 synchronized(bbuf) 205 if (_channel.isOpen())
182 { 206 _channel.close();
183 try 207 }
184 { 208 catch (Exception xx)
185 bbuf.position(buffer.putIndex()); 209 {
186 len = _channel.read(bbuf); 210 LOG.trace("",xx);
187 } 211 }
188 finally 212
189 { 213 if (len>0)
190 buffer.setPutIndex(bbuf.position()); 214 throw x;
191 bbuf.position(0); 215 len = -1;
192 }
193 }
194
195 if (len<0 && isOpen())
196 {
197 if (!isInputShutdown())
198 shutdownInput();
199 if (isOutputShutdown())
200 _channel.close();
201 }
202 }
203 catch (IOException x)
204 {
205 LOG.debug("Exception while filling", x);
206 try
207 {
208 if (_channel.isOpen())
209 _channel.close();
210 }
211 catch (Exception xx)
212 {
213 LOG.trace("",xx);
214 }
215
216 if (len>0)
217 throw x;
218 len = -1;
219 }
220 }
221 else
222 {
223 throw new IOException("Not Implemented");
224 } 216 }
225 217
226 return len; 218 return len;
227 } 219 }
228 220
229 @Override 221 @Override
230 public int flush(Buffer buffer) throws IOException 222 public int flush(JBuffer buffer) throws IOException
231 { 223 {
232 Buffer buf = buffer.buffer(); 224 JBuffer nbuf = buffer.buffer();
233 int len=0; 225 int len=0;
234 if (buf instanceof NIOBuffer) 226 final ByteBuffer bbuf = nbuf.getByteBuffer().asReadOnlyBuffer();
235 { 227 try
236 final NIOBuffer nbuf = (NIOBuffer)buf; 228 {
237 final ByteBuffer bbuf = nbuf.getByteBuffer().asReadOnlyBuffer(); 229 bbuf.position(buffer.getIndex());
238 try 230 bbuf.limit(buffer.putIndex());
239 { 231 len=_channel.write(bbuf);
240 bbuf.position(buffer.getIndex()); 232 }
241 bbuf.limit(buffer.putIndex()); 233 finally
242 len=_channel.write(bbuf); 234 {
243 }
244 finally
245 {
246 if (len>0)
247 buffer.skip(len);
248 }
249 }
250 else if (buffer.array()!=null)
251 {
252 ByteBuffer b = ByteBuffer.wrap(buffer.array(), buffer.getIndex(), buffer.remaining());
253 len=_channel.write(b);
254 if (len>0) 235 if (len>0)
255 buffer.skip(len); 236 buffer.skip(len);
256 } 237 }
257 else
258 {
259 throw new IOException("Not Implemented");
260 }
261 return len; 238 return len;
262 } 239 }
263 240
264 @Override 241 @Override
265 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException 242 public int flush(JBuffer header, JBuffer buffer, JBuffer trailer) throws IOException
266 { 243 {
267 int length=0; 244 int length=0;
268 245
269 Buffer buf0 = header==null?null:header.buffer(); 246 JBuffer buf0 = header==null?null:header.buffer();
270 Buffer buf1 = buffer==null?null:buffer.buffer(); 247 JBuffer buf1 = buffer==null?null:buffer.buffer();
271 248
272 if (_channel instanceof GatheringByteChannel && 249 if (_channel instanceof GatheringByteChannel &&
273 header!=null && header.remaining()!=0 && buf0 instanceof NIOBuffer && 250 header!=null && header.remaining()!=0 &&
274 buffer!=null && buffer.remaining()!=0 && buf1 instanceof NIOBuffer) 251 buffer!=null && buffer.remaining()!=0)
275 { 252 {
276 length = gatheringFlush(header,((NIOBuffer)buf0).getByteBuffer(),buffer,((NIOBuffer)buf1).getByteBuffer()); 253 length = gatheringFlush(header,buf0.getByteBuffer(),buffer,buf1.getByteBuffer());
277 } 254 }
278 else 255 else
279 { 256 {
280 // flush header 257 // flush header
281 if (header!=null && header.remaining()>0) 258 if (header!=null && header.remaining()>0)
294 } 271 }
295 272
296 return length; 273 return length;
297 } 274 }
298 275
299 private int gatheringFlush(Buffer header, ByteBuffer bbuf0, Buffer buffer, ByteBuffer bbuf1) throws IOException 276 private int gatheringFlush(JBuffer header, ByteBuffer bbuf0, JBuffer buffer, ByteBuffer bbuf1) throws IOException
300 { 277 {
301 int length; 278 int length;
302 279
303 synchronized(this) 280 synchronized(this)
304 { 281 {