From: Greg W. <gre...@us...> - 2004-07-22 00:59:35
|
User: gregwilkins Date: 04/07/21 17:59:28 Modified: src/org/mortbay/io/nio NIOBuffer.java Added: src/org/mortbay/io/nio ChannelEndPoint.java Log: More faffing about with the buffer model. Important to get right as it will be central to the speed and portability of everything. Just about happy with it now and will get on with the real async HTTP handling soon Revision Changes Path 1.3 +64 -142 JettyExperimental/src/org/mortbay/io/nio/NIOBuffer.java Index: NIOBuffer.java =================================================================== RCS file: /cvsroot/jetty/JettyExperimental/src/org/mortbay/io/nio/NIOBuffer.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -w -r1.2 -r1.3 --- NIOBuffer.java 23 Jun 2004 11:12:03 -0000 1.2 +++ NIOBuffer.java 22 Jul 2004 00:59:28 -0000 1.3 @@ -17,7 +17,6 @@ import org.mortbay.io.AbstractBuffer; import org.mortbay.io.Buffer; -import org.mortbay.io.ByteArrayBuffer; import org.mortbay.io.Portable; import java.nio.ByteBuffer; @@ -36,186 +35,109 @@ private String _string; private boolean _volatile; private ByteBuffer _buf; - private ByteArrayBuffer _subBuffer; - private int _mark; - private int _put; - public NIOBuffer(int size, boolean direct) { - super(false); + super(READWRITE,NON_VOLATILE); _buf = direct ?ByteBuffer.allocateDirect(size) :ByteBuffer.allocate(size); - setPutIndex(0); + _buf.position(0); + _buf.limit(_buf.capacity()); + } public byte[] array() { + if (!_buf.hasArray()) + return null; return _buf.array(); } - /* - * @see org.mortbay.io.Buffer#asNonVolatile() - */ - public Buffer asNonVolatile() - { - if (!_volatile) - return this; - return new ByteArrayBuffer(array(), getIndex(), length(), isReadOnly()); - } - - /* - * @see org.mortbay.util.Buffer#asReadOnlyBuffer() - */ - public Buffer asReadOnlyBuffer() - { - if (!isReadOnly()) - { - byte[] bytes= new byte[putIndex() - getIndex()]; - Portable.arraycopy(array(), getIndex(), bytes, 0, bytes.length); - ByteArrayBuffer view=new ByteArrayBuffer(bytes, 0, length(), READONLY); - return view; - } - else - return this; - } - public int capacity() { return _buf.capacity(); } - public Buffer duplicate() - { - byte[] bytes= new byte[capacity()]; - if (markIndex() < 0) - Portable.arraycopy(array(), getIndex(), bytes, getIndex(), length()); - else - Portable.arraycopy( - array(), - markIndex(), - bytes, - markIndex(), - putIndex() - markIndex()); - - ByteArrayBuffer view= - new ByteArrayBuffer(bytes, getIndex(), length(), !READONLY); - view.setMarkIndex(markIndex()); - - return view; - } - - public byte get() + public byte peek(int position) { - return _buf.get(); + return _buf.get(position); } - public int get(byte[] b, int offset, int length) + public int peek(int index, byte[] b, int offset, int length) { int l = length; - if (l>length()) - l=length(); + if (index+l > capacity()) + l=capacity()-index; if (l<=0) return -1; - - _buf.get(b,offset,length); - return l; - } - - public boolean isVolatile() + try { - return _volatile; + _buf.position(index); + _buf.get(b,offset,length); } - - public byte peek(int position) + finally { - if (position < 0) - Portable.throwIllegalArgument("position<0: " + position + "<0"); - if (position > capacity()) - Portable.throwIllegalArgument("position>capacity(): " + position + ">" + capacity()); - return _buf.get(position); + _buf.position(0); } - public Buffer peek(int position, int length) - { - if (position < 0) - Portable.throwIllegalArgument("position<0: " + position + "<0"); - if (position + length > capacity()) - Portable.throwIllegalArgument( - "position+length>capacity(): " + position + "+" + length + ">" + capacity()); - return subBuffer(position, length); + return l; } public void poke(int position, byte b) { - if (isReadOnly()) - Portable.throwIllegalState("readOnly"); - if (position < 0) - Portable.throwIllegalArgument("position<0: " + position + "<0"); - if (position > capacity()) - Portable.throwIllegalArgument("position>capacity(): " + position + ">" + capacity()); - + if (isReadOnly()) Portable.throwIllegalState(__READONLY); _buf.put(position,b); } - protected Buffer subBuffer(int position, int length) + public void poke(int index, Buffer src) { - if (_subBuffer == null) + if (isReadOnly()) Portable.throwIllegalState(__READONLY); + + byte[] array=src.array(); + if (array!=null) { - _subBuffer= new ByteArrayBuffer(array(), position, length, READWRITE, Buffer.VOLATILE); + poke(index,array,src.getIndex(),src.length()); } else { - _subBuffer.setGetIndex(0); - _subBuffer.setPutIndex(position + length); - _subBuffer.setGetIndex(position); - } - return _subBuffer; - } - - public String toString() - { - if (_string != null) - return _string; - if (isReadOnly() && !isVolatile()) + Buffer buf=src.buffer(); + if (buf instanceof NIOBuffer) { - _string= new String(array(), getIndex(), length()); - return _string; + ByteBuffer b = ((NIOBuffer)buf)._buf; + if (b==_buf) + b=_buf.duplicate(); + try + { + _buf.position(index); + b.limit(src.putIndex()); + b.position(src.getIndex()); + _buf.put(b); + } + finally + { + _buf.position(0); + b.limit(b.capacity()); + b.position(0); } - - return new String(array(), getIndex(), length()); } - - public int getIndex() - { - return _buf.position(); + else + super.poke(index,src); } - - public int markIndex() - { - return _mark; } - public int putIndex() - { - return _put; - } - public void setGetIndex(int newGet) + public void poke(int index, byte[] b, int offset, int length) { - _buf.position(newGet); - } - - public void setMarkIndex(int newMark) + if (isReadOnly()) Portable.throwIllegalState(__READONLY); + try { - _mark = newMark; + _buf.position(index); + _buf.put(b,offset,length); } - - public void setPutIndex(int newPutIndex) + finally { - _put=newPutIndex; + _buf.position(0); + } } - - } 1.1 JettyExperimental/src/org/mortbay/io/nio/ChannelEndPoint.java Index: ChannelEndPoint.java =================================================================== //======================================================================== //$Id: ChannelEndPoint.java,v 1.1 2004/07/22 00:59:28 gregwilkins Exp $ //Copyright 2004 Mort Bay Consulting Pty. Ltd. //------------------------------------------------------------------------ //Licensed under the Apache License, Version 2.0 (the "License"); //you may not use this file except in compliance with the License. //You may obtain a copy of the License at //http://www.apache.org/licenses/LICENSE-2.0 //Unless required by applicable law or agreed to in writing, software //distributed under the License is distributed on an "AS IS" BASIS, //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //See the License for the specific language governing permissions and //limitations under the License. //======================================================================== package org.mortbay.io.nio; import java.io.IOException; import java.nio.channels.ByteChannel; import org.mortbay.io.Buffer; import org.mortbay.io.EndPoint; /** * @author gregw * * To change the template for this generated type comment go to * Window - Preferences - Java - Code Generation - Code and Comments */ public class ChannelEndPoint implements EndPoint { ByteChannel _channel; /** * */ public ChannelEndPoint(ByteChannel chanel) { super(); this._channel = chanel; } /* (non-Javadoc) * @see org.mortbay.io.EndPoint#isClosed() */ public boolean isClosed() { return !_channel.isOpen(); } /* (non-Javadoc) * @see org.mortbay.io.EndPoint#close() */ public void close() throws IOException { _channel.close(); } /* (non-Javadoc) * @see org.mortbay.io.EndPoint#fill(org.mortbay.io.Buffer) */ public int fill(Buffer buffer) throws IOException { int l= _channel.read(null); return 0; } /* (non-Javadoc) * @see org.mortbay.io.EndPoint#flush(org.mortbay.io.Buffer) */ public int flush(Buffer buffer) throws IOException { // TODO Auto-generated method stub return 0; } /* (non-Javadoc) * @see org.mortbay.io.EndPoint#flush(org.mortbay.io.Buffer, org.mortbay.io.Buffer, org.mortbay.io.Buffer) */ public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException { // TODO Auto-generated method stub return 0; } } |