Mercurial Hosting > luan
view src/org/eclipse/jetty/io/BufferUtil.java @ 1074:6b7ff30bb990
rename putQ() to put()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 10 Nov 2016 02:37:15 -0700 |
parents | 9d357b9e4bcb |
children | ebb0f1343ef6 |
line wrap: on
line source
// // ======================================================================== // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. // ------------------------------------------------------------------------ // All rights reserved. This program and the accompanying materials // are made available under the terms of the Eclipse Public License v1.0 // and Apache License v2.0 which accompanies this distribution. // // The Eclipse Public License is available at // http://www.eclipse.org/legal/epl-v10.html // // The Apache License v2.0 is available at // http://www.opensource.org/licenses/apache2.0.php // // You may elect to redistribute this code under either of these licenses. // ======================================================================== // package org.eclipse.jetty.io; import java.nio.ByteBuffer; import java.nio.charset.Charset; import org.eclipse.jetty.util.StringUtil; /* ------------------------------------------------------------------------------- */ /** Buffer utility methods. * * */ public final class BufferUtil { private static final byte[] DIGIT= {(byte)'0',(byte)'1',(byte)'2',(byte)'3',(byte)'4',(byte)'5',(byte)'6',(byte)'7',(byte)'8',(byte)'9',(byte)'A',(byte)'B',(byte)'C',(byte)'D',(byte)'E',(byte)'F'}; /** * Convert string to an long. * Parses up to the first non-numeric character. If no number is found an * IllegalArgumentException is thrown */ public static long toLong(String s) { long val = 0; boolean started= false; boolean minus= false; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c <= ' ') { if (started) break; } else if (c >= '0' && c <= '9') { val= val * 10L + (c - '0'); started= true; } else if (c == '-' && !started) { minus= true; } else break; } if (started) return minus ? (-val) : val; throw new NumberFormatException(s); } public static void putHexInt(JBuffer buffer, int n) { if (n < 0) { buffer.put((byte)'-'); if (n == Integer.MIN_VALUE) { buffer.put((byte)(0x7f&'8')); buffer.put((byte)(0x7f&'0')); buffer.put((byte)(0x7f&'0')); buffer.put((byte)(0x7f&'0')); buffer.put((byte)(0x7f&'0')); buffer.put((byte)(0x7f&'0')); buffer.put((byte)(0x7f&'0')); buffer.put((byte)(0x7f&'0')); return; } n= -n; } if (n < 0x10) { buffer.put(DIGIT[n]); } else { boolean started= false; // This assumes constant time int arithmatic for (int i= 0; i < hexDivisors.length; i++) { if (n < hexDivisors[i]) { if (started) buffer.put((byte)'0'); continue; } started= true; int d = n / hexDivisors[i]; buffer.put(DIGIT[d]); n= n - d * hexDivisors[i]; } } } public static void putDecLong(JBuffer buffer, long n) { if (n < 0) { buffer.put((byte)'-'); if (n == Long.MIN_VALUE) { buffer.put((byte)'9'); n = 223372036854775808L; } else n = -n; } if (n < 10) { buffer.put(DIGIT[(int)n]); } else { boolean started= false; // This assumes constant time int arithmatic for (int i= 0; i < decDivisorsL.length; i++) { if (n < decDivisorsL[i]) { if (started) buffer.put((byte)'0'); continue; } started= true; long d= n / decDivisorsL[i]; buffer.put(DIGIT[(int)d]); n= n - d * decDivisorsL[i]; } } } private final static int[] hexDivisors= { 0x10000000, 0x1000000, 0x100000, 0x10000, 0x1000, 0x100, 0x10, 0x1 }; private final static long[] decDivisorsL= { 1000000000000000000L, 100000000000000000L, 10000000000000000L, 1000000000000000L, 100000000000000L, 10000000000000L, 1000000000000L, 100000000000L, 10000000000L, 1000000000L, 100000000L, 10000000L, 1000000L, 100000L, 10000L, 1000L, 100L, 10L, 1L }; public static final JBuffer EMPTY_BUFFER = new JBuffer(ByteBuffer.allocate(0)); public static JBuffer wrap(byte[] array,int offset,int length) { return new JBuffer(ByteBuffer.wrap(array,offset,length)); } public static JBuffer wrap(byte[] array) { return new JBuffer(ByteBuffer.wrap(array)); } public static JBuffer wrap(String s) { byte[] bytes = StringUtil.getBytes(s); ByteBuffer bb = ByteBuffer.wrap(bytes).asReadOnlyBuffer(); return new JBuffer(bb); } public static JBuffer newBuffer(int size) { ByteBuffer bb = ByteBuffer.allocate(size); return new JBuffer(bb); } public static JBuffer newDirectBuffer(int size) { ByteBuffer bb = ByteBuffer.allocateDirect(size); return new JBuffer(bb); } private static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); public static String getString(JBuffer buffer) { byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); return new String(bytes,ISO_8859_1); } public static void compact(JBuffer buffer) { buffer.compact(); buffer.limit(buffer.position()); buffer.position(0); } }