Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/nio/DirectNIOBuffer.java @ 1016:8c13b9224cff
remove returnBuffer()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 24 Oct 2016 01:38:00 -0600 |
parents | 2712133d5bce |
children | 80cad9086593 |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.io.nio; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.File; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.io.FileInputStream; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.io.IOException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 import java.io.InputStream; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 import java.io.OutputStream; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 import java.nio.ByteBuffer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 import java.nio.channels.Channels; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 import java.nio.channels.FileChannel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 import java.nio.channels.ReadableByteChannel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import java.nio.channels.WritableByteChannel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 import org.eclipse.jetty.io.AbstractBuffer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 import org.eclipse.jetty.io.Buffer; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 import org.eclipse.jetty.util.IO; |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
35 import org.slf4j.Logger; |
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
36 import org.slf4j.LoggerFactory; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 |
1010 | 38 |
39 public final class DirectNIOBuffer extends AbstractBuffer implements NIOBuffer | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 { |
1010 | 41 private static final Logger LOG = LoggerFactory.getLogger(DirectNIOBuffer.class); |
42 | |
43 protected final ByteBuffer _buf; | |
44 private ReadableByteChannel _in; | |
45 private InputStream _inStream; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 |
1010 | 47 public DirectNIOBuffer(int size) |
48 { | |
49 super(READWRITE,NON_VOLATILE); | |
50 _buf = ByteBuffer.allocateDirect(size); | |
51 _buf.position(0); | |
52 _buf.limit(_buf.capacity()); | |
53 } | |
54 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
55 |
1010 | 56 @Override |
57 public boolean isDirect() | |
58 { | |
59 return true; | |
60 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
61 |
1010 | 62 @Override |
63 public byte[] array() | |
64 { | |
65 return null; | |
66 } | |
67 | |
68 @Override | |
69 public int capacity() | |
70 { | |
71 return _buf.capacity(); | |
72 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
73 |
1010 | 74 @Override |
75 public byte peek(int position) | |
76 { | |
77 return _buf.get(position); | |
78 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
79 |
1010 | 80 @Override |
81 public int peek(int index, byte[] b, int offset, int length) | |
82 { | |
83 int l = length; | |
84 if (index+l > capacity()) | |
85 { | |
86 l=capacity()-index; | |
87 if (l==0) | |
88 return -1; | |
89 } | |
90 | |
91 if (l < 0) | |
92 return -1; | |
93 try | |
94 { | |
95 _buf.position(index); | |
96 _buf.get(b,offset,l); | |
97 } | |
98 finally | |
99 { | |
100 _buf.position(0); | |
101 } | |
102 | |
103 return l; | |
104 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
105 |
1010 | 106 @Override |
107 public void poke(int index, byte b) | |
108 { | |
109 if (isReadOnly()) throw new IllegalStateException(__READONLY); | |
110 if (index < 0) throw new IllegalArgumentException("index<0: " + index + "<0"); | |
111 if (index > capacity()) | |
112 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
113 _buf.put(index,b); | |
114 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
115 |
1010 | 116 @Override |
117 public int poke(int index, Buffer src) | |
118 { | |
119 if (isReadOnly()) throw new IllegalStateException(__READONLY); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
120 |
1010 | 121 byte[] array=src.array(); |
122 if (array!=null) | |
123 { | |
124 return poke(index,array,src.getIndex(),src.length()); | |
125 } | |
126 else | |
127 { | |
128 Buffer src_buf=src.buffer(); | |
129 if (src_buf instanceof DirectNIOBuffer) | |
130 { | |
131 ByteBuffer src_bytebuf = ((DirectNIOBuffer)src_buf)._buf; | |
132 if (src_bytebuf==_buf) | |
133 src_bytebuf=_buf.duplicate(); | |
134 try | |
135 { | |
136 _buf.position(index); | |
137 int space = _buf.remaining(); | |
138 | |
139 int length=src.length(); | |
140 if (length>space) | |
141 length=space; | |
142 | |
143 src_bytebuf.position(src.getIndex()); | |
144 src_bytebuf.limit(src.getIndex()+length); | |
145 | |
146 _buf.put(src_bytebuf); | |
147 return length; | |
148 } | |
149 finally | |
150 { | |
151 _buf.position(0); | |
152 src_bytebuf.limit(src_bytebuf.capacity()); | |
153 src_bytebuf.position(0); | |
154 } | |
155 } | |
156 else | |
157 return super.poke(index,src); | |
158 } | |
159 } | |
160 | |
161 @Override | |
162 public int poke(int index, byte[] b, int offset, int length) | |
163 { | |
164 if (isReadOnly()) throw new IllegalStateException(__READONLY); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
165 |
1010 | 166 if (index < 0) throw new IllegalArgumentException("index<0: " + index + "<0"); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
167 |
1010 | 168 if (index + length > capacity()) |
169 { | |
170 length=capacity()-index; | |
171 if (length<0) | |
172 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
173 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
174 |
1010 | 175 try |
176 { | |
177 _buf.position(index); | |
178 | |
179 int space=_buf.remaining(); | |
180 | |
181 if (length>space) | |
182 length=space; | |
183 if (length>0) | |
184 _buf.put(b,offset,length); | |
185 return length; | |
186 } | |
187 finally | |
188 { | |
189 _buf.position(0); | |
190 } | |
191 } | |
192 | |
193 @Override | |
194 public ByteBuffer getByteBuffer() | |
195 { | |
196 return _buf; | |
197 } | |
198 | |
199 @Override | |
200 public int readFrom(InputStream in, int max) throws IOException | |
201 { | |
202 if (_in==null || !_in.isOpen() || in!=_inStream) | |
203 { | |
204 _in = Channels.newChannel(in); | |
205 _inStream = in; | |
206 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
207 |
1010 | 208 if (max<0 || max>space()) |
209 max=space(); | |
210 int p = putIndex(); | |
211 | |
212 try | |
213 { | |
214 int len=0, total=0, available=max; | |
215 int loop=0; | |
216 while (total<max) | |
217 { | |
218 _buf.position(p); | |
219 _buf.limit(p+available); | |
220 len = _in.read(_buf); | |
221 if (len<0) | |
222 { | |
223 _in = null; | |
224 _inStream = in; | |
225 break; | |
226 } | |
227 else if (len>0) | |
228 { | |
229 p += len; | |
230 total += len; | |
231 available -= len; | |
232 setPutIndex(p); | |
233 loop=0; | |
234 } | |
235 else if (loop++>1) | |
236 break; | |
237 if (in.available()<=0) | |
238 break; | |
239 } | |
240 if (len<0 && total==0) | |
241 return -1; | |
242 return total; | |
243 | |
244 } | |
245 catch(IOException e) | |
246 { | |
247 _in = null; | |
248 _inStream = in; | |
249 throw e; | |
250 } | |
251 finally | |
252 { | |
253 if (_in!=null && !_in.isOpen()) | |
254 { | |
255 _in = null; | |
256 _inStream = in; | |
257 } | |
258 _buf.position(0); | |
259 _buf.limit(_buf.capacity()); | |
260 } | |
261 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
262 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
263 } |