Mercurial Hosting > luan
comparison src/org/eclipse/jetty/io/BufferUtil.java @ 1048:2b769da7f67d
remove Buffer
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 07 Nov 2016 23:15:42 -0700 |
parents | a8c92b0a08ed |
children | 4a2489f1d5fe |
comparison
equal
deleted
inserted
replaced
1047:1accf965d51a | 1048:2b769da7f67d |
---|---|
17 // | 17 // |
18 | 18 |
19 package org.eclipse.jetty.io; | 19 package org.eclipse.jetty.io; |
20 | 20 |
21 import java.nio.ByteBuffer; | 21 import java.nio.ByteBuffer; |
22 import org.eclipse.jetty.io.nio.NIOBuffer; | |
23 import org.eclipse.jetty.util.StringUtil; | 22 import org.eclipse.jetty.util.StringUtil; |
24 | 23 |
25 | 24 |
26 /* ------------------------------------------------------------------------------- */ | 25 /* ------------------------------------------------------------------------------- */ |
27 /** Buffer utility methods. | 26 /** Buffer utility methods. |
40 * Parses up to the first non-numeric character. If no number is found an | 39 * Parses up to the first non-numeric character. If no number is found an |
41 * IllegalArgumentException is thrown | 40 * IllegalArgumentException is thrown |
42 * @param buffer A buffer containing an integer. The position is not changed. | 41 * @param buffer A buffer containing an integer. The position is not changed. |
43 * @return an int | 42 * @return an int |
44 */ | 43 */ |
45 public static int toInt(Buffer buffer) | 44 public static int toInt(JBuffer buffer) |
46 { | 45 { |
47 int val= 0; | 46 int val= 0; |
48 boolean started= false; | 47 boolean started= false; |
49 boolean minus= false; | 48 boolean minus= false; |
50 for (int i= buffer.getIndex(); i < buffer.putIndex(); i++) | 49 for (int i= buffer.getIndex(); i < buffer.putIndex(); i++) |
107 if (started) | 106 if (started) |
108 return minus ? (-val) : val; | 107 return minus ? (-val) : val; |
109 throw new NumberFormatException(s); | 108 throw new NumberFormatException(s); |
110 } | 109 } |
111 | 110 |
112 public static void putHexInt(Buffer buffer, int n) | 111 public static void putHexInt(JBuffer buffer, int n) |
113 { | 112 { |
114 | 113 |
115 if (n < 0) | 114 if (n < 0) |
116 { | 115 { |
117 buffer.put((byte)'-'); | 116 buffer.put((byte)'-'); |
161 /** | 160 /** |
162 * Add hex integer BEFORE current getIndex. | 161 * Add hex integer BEFORE current getIndex. |
163 * @param buffer | 162 * @param buffer |
164 * @param n | 163 * @param n |
165 */ | 164 */ |
166 public static void prependHexInt(Buffer buffer, int n) | 165 public static void prependHexInt(JBuffer buffer, int n) |
167 { | 166 { |
168 if (n==0) | 167 if (n==0) |
169 { | 168 { |
170 int gi=buffer.getIndex(); | 169 int gi=buffer.getIndex(); |
171 buffer.poke(--gi,(byte)'0'); | 170 buffer.poke(--gi,(byte)'0'); |
193 buffer.setGetIndex(gi); | 192 buffer.setGetIndex(gi); |
194 } | 193 } |
195 } | 194 } |
196 | 195 |
197 | 196 |
198 public static void putDecLong(Buffer buffer, long n) | 197 public static void putDecLong(JBuffer buffer, long n) |
199 { | 198 { |
200 if (n < 0) | 199 if (n < 0) |
201 { | 200 { |
202 buffer.put((byte)'-'); | 201 buffer.put((byte)'-'); |
203 | 202 |
269 10L, | 268 10L, |
270 1L | 269 1L |
271 }; | 270 }; |
272 | 271 |
273 | 272 |
274 public static void putCRLF(Buffer buffer) | 273 public static void putCRLF(JBuffer buffer) |
275 { | 274 { |
276 buffer.put((byte)13); | 275 buffer.put((byte)13); |
277 buffer.put((byte)10); | 276 buffer.put((byte)10); |
278 } | 277 } |
279 | 278 |
280 | 279 |
281 public static final NIOBuffer EMPTY_BUFFER = new JBuffer(ByteBuffer.allocate(0)); | 280 public static final JBuffer EMPTY_BUFFER = new JBuffer(ByteBuffer.allocate(0)); |
282 | 281 |
283 public static NIOBuffer wrap(byte[] array,int offset,int length) { | 282 public static JBuffer wrap(byte[] array,int offset,int length) { |
284 return new JBuffer(ByteBuffer.wrap(array,offset,length)); | 283 return new JBuffer(ByteBuffer.wrap(array,offset,length)); |
285 } | 284 } |
286 | 285 |
287 public static NIOBuffer wrap(byte[] array) { | 286 public static JBuffer wrap(byte[] array) { |
288 return new JBuffer(ByteBuffer.wrap(array)); | 287 return new JBuffer(ByteBuffer.wrap(array)); |
289 } | 288 } |
290 | 289 |
291 public static NIOBuffer wrap(String s) { | 290 public static JBuffer wrap(String s) { |
292 byte[] bytes = StringUtil.getBytes(s); | 291 byte[] bytes = StringUtil.getBytes(s); |
293 ByteBuffer bb = ByteBuffer.wrap(bytes).asReadOnlyBuffer(); | 292 ByteBuffer bb = ByteBuffer.wrap(bytes).asReadOnlyBuffer(); |
294 return new JBuffer(bb); | 293 return new JBuffer(bb); |
295 } | 294 } |
296 | 295 |
297 public static NIOBuffer newBuffer(int size) { | 296 public static JBuffer newBuffer(int size) { |
298 ByteBuffer bb = ByteBuffer.allocate(size); | 297 ByteBuffer bb = ByteBuffer.allocate(size); |
299 bb.limit(0); | 298 bb.limit(0); |
300 return new JBuffer(bb); | 299 return new JBuffer(bb); |
301 } | 300 } |
302 | 301 |
303 public static NIOBuffer newDirectBuffer(int size) { | 302 public static JBuffer newDirectBuffer(int size) { |
304 ByteBuffer bb = ByteBuffer.allocateDirect(size); | 303 ByteBuffer bb = ByteBuffer.allocateDirect(size); |
305 bb.limit(0); | 304 bb.limit(0); |
306 return new JBuffer(bb); | 305 return new JBuffer(bb); |
307 } | 306 } |
308 | 307 |