annotate src/org/eclipse/jetty/io/nio/DirectNIOBuffer.java @ 1017:d2c3ff33387c

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 24 Oct 2016 01:56:08 -0600
parents 2712133d5bce
children 80cad9086593
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
38
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
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
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
41 private static final Logger LOG = LoggerFactory.getLogger(DirectNIOBuffer.class);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
42
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
43 protected final ByteBuffer _buf;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
44 private ReadableByteChannel _in;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
45 private InputStream _inStream;
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46
1010
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
47 public DirectNIOBuffer(int size)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
48 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
49 super(READWRITE,NON_VOLATILE);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
50 _buf = ByteBuffer.allocateDirect(size);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
51 _buf.position(0);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
52 _buf.limit(_buf.capacity());
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
53 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
54
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55
1010
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
56 @Override
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
57 public boolean isDirect()
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
58 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
59 return true;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
60 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61
1010
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
62 @Override
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
63 public byte[] array()
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
64 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
65 return null;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
66 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
67
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
68 @Override
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
69 public int capacity()
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
70 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
71 return _buf.capacity();
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
72 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73
1010
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
74 @Override
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
75 public byte peek(int position)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
76 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
77 return _buf.get(position);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
78 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79
1010
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
80 @Override
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
81 public int peek(int index, byte[] b, int offset, int length)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
82 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
83 int l = length;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
84 if (index+l > capacity())
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
85 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
86 l=capacity()-index;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
87 if (l==0)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
88 return -1;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
89 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
90
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
91 if (l < 0)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
92 return -1;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
93 try
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
94 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
95 _buf.position(index);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
96 _buf.get(b,offset,l);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
97 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
98 finally
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
99 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
100 _buf.position(0);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
101 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
102
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
103 return l;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
104 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105
1010
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
106 @Override
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
107 public void poke(int index, byte b)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
108 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
109 if (isReadOnly()) throw new IllegalStateException(__READONLY);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
110 if (index < 0) throw new IllegalArgumentException("index<0: " + index + "<0");
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
111 if (index > capacity())
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
112 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity());
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
113 _buf.put(index,b);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
114 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115
1010
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
116 @Override
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
117 public int poke(int index, Buffer src)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
118 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
119 if (isReadOnly()) throw new IllegalStateException(__READONLY);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120
1010
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
121 byte[] array=src.array();
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
122 if (array!=null)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
123 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
124 return poke(index,array,src.getIndex(),src.length());
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
125 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
126 else
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
127 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
128 Buffer src_buf=src.buffer();
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
129 if (src_buf instanceof DirectNIOBuffer)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
130 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
131 ByteBuffer src_bytebuf = ((DirectNIOBuffer)src_buf)._buf;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
132 if (src_bytebuf==_buf)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
133 src_bytebuf=_buf.duplicate();
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
134 try
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
135 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
136 _buf.position(index);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
137 int space = _buf.remaining();
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
138
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
139 int length=src.length();
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
140 if (length>space)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
141 length=space;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
142
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
143 src_bytebuf.position(src.getIndex());
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
144 src_bytebuf.limit(src.getIndex()+length);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
145
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
146 _buf.put(src_bytebuf);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
147 return length;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
148 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
149 finally
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
150 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
151 _buf.position(0);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
152 src_bytebuf.limit(src_bytebuf.capacity());
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
153 src_bytebuf.position(0);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
154 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
155 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
156 else
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
157 return super.poke(index,src);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
158 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
159 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
160
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
161 @Override
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
162 public int poke(int index, byte[] b, int offset, int length)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
163 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
164 if (isReadOnly()) throw new IllegalStateException(__READONLY);
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165
1010
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
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
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
168 if (index + length > capacity())
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
169 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
170 length=capacity()-index;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
171 if (length<0)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
172 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity());
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
173 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
174
1010
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
175 try
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
176 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
177 _buf.position(index);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
178
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
179 int space=_buf.remaining();
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
180
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
181 if (length>space)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
182 length=space;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
183 if (length>0)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
184 _buf.put(b,offset,length);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
185 return length;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
186 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
187 finally
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
188 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
189 _buf.position(0);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
190 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
191 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
192
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
193 @Override
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
194 public ByteBuffer getByteBuffer()
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
195 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
196 return _buf;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
197 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
198
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
199 @Override
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
200 public int readFrom(InputStream in, int max) throws IOException
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
201 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
202 if (_in==null || !_in.isOpen() || in!=_inStream)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
203 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
204 _in = Channels.newChannel(in);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
205 _inStream = in;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
206 }
802
3428c60d7cfc replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
207
1010
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
208 if (max<0 || max>space())
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
209 max=space();
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
210 int p = putIndex();
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
211
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
212 try
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
213 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
214 int len=0, total=0, available=max;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
215 int loop=0;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
216 while (total<max)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
217 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
218 _buf.position(p);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
219 _buf.limit(p+available);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
220 len = _in.read(_buf);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
221 if (len<0)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
222 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
223 _in = null;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
224 _inStream = in;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
225 break;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
226 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
227 else if (len>0)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
228 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
229 p += len;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
230 total += len;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
231 available -= len;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
232 setPutIndex(p);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
233 loop=0;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
234 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
235 else if (loop++>1)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
236 break;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
237 if (in.available()<=0)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
238 break;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
239 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
240 if (len<0 && total==0)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
241 return -1;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
242 return total;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
243
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
244 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
245 catch(IOException e)
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
246 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
247 _in = null;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
248 _inStream = in;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
249 throw e;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
250 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
251 finally
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
252 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
253 if (_in!=null && !_in.isOpen())
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
254 {
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
255 _in = null;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
256 _inStream = in;
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
257 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
258 _buf.position(0);
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
259 _buf.limit(_buf.capacity());
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
260 }
2712133d5bce simplify Buffer code
Franklin Schmidt <fschmidt@gmail.com>
parents: 820
diff changeset
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 }