Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 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;
 
 
 /* ------------------------------------------------------------------------------- */
Buffer utility methods.
 
 public class BufferUtil
 {
     static final byte SPACE= 0x20;
     static final byte MINUS'-';
     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 buffer to an integer. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown

Parameters:
buffer A buffer containing an integer. The position is not changed.
Returns:
an int
 
     public static int toInt(Buffer buffer)
     {
         int val= 0;
         boolean startedfalse;
         boolean minusfalse;
         for (int ibuffer.getIndex(); i < buffer.putIndex(); i++)
         {
             byte bbuffer.peek(i);
             if (b <= )
             {
                 if (started)
                     break;
             }
             else if (b >= '0' && b <= '9')
             {
                 valval * 10 + (b - '0');
                 startedtrue;
             }
             else if (b ==  && !started)
             {
                 minustrue;
             }
             else
                 break;
         }
 
         if (started)
             return minus ? (-val) : val;
         throw new NumberFormatException(buffer.toString());
     }
    
    
Convert buffer to an long. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown

Parameters:
buffer A buffer containing an integer. The position is not changed.
Returns:
an int
 
     public static long toLong(Buffer buffer)
     {
         long val= 0;
         boolean startedfalse;
         boolean minusfalse;
         for (int ibuffer.getIndex(); i < buffer.putIndex(); i++)
         {
             byte bbuffer.peek(i);
             if (b <= )
             {
                 if (started)
                     break;
             }
             else if (b >= '0' && b <= '9')
             {
                 valval * 10L + (b - '0');
                 startedtrue;
             }
             else if (b ==  && !started)
            {
                minustrue;
            }
            else
                break;
        }
        if (started)
            return minus ? (-val) : val;
        throw new NumberFormatException(buffer.toString());
    }
    public static void putHexInt(Buffer bufferint n)
    {
        if (n < 0)
        {
            buffer.put((byte)'-');
            if (n == .)
            {
                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([n]);
        }
        else
        {
            boolean startedfalse;
            // This assumes constant time int arithmatic
            for (int i= 0; i < .i++)
            {
                if (n < [i])
                {
                    if (started)
                        buffer.put((byte)'0');
                    continue;
                }
                startedtrue;
                int dn / [i];
                buffer.put([d]);
                nn - d * [i];
            }
        }
    }
    /* ------------------------------------------------------------ */
    
Add hex integer BEFORE current getIndex.

Parameters:
buffer
n
    public static void prependHexInt(Buffer bufferint n)
    {
        if (n==0)
        {
            int gi=buffer.getIndex();
            buffer.poke(--gi,(byte)'0');
            buffer.setGetIndex(gi);
        }
        else
        {
            boolean minus=false;
            if (n<0)
            {
                minus=true;
                n=-n;
            }
            int gi=buffer.getIndex();
            while(n>0)
            {
                int d = 0xf&n;
                n=n>>4;
                buffer.poke(--gi,[d]);
            }
            
            if (minus)
                buffer.poke(--gi,(byte)'-');
            buffer.setGetIndex(gi);
        }
    }
    
    /* ------------------------------------------------------------ */
    public static void putDecInt(Buffer bufferint n)
    {
        if (n < 0)
        {
            buffer.put((byte)'-');
            if (n == .)
            {
                buffer.put((byte)'2');
                n= 147483648;
            }
            else
                n= -n;
        }
        if (n < 10)
        {
            buffer.put([n]);
        }
        else
        {
            boolean startedfalse;
            // This assumes constant time int arithmatic
            for (int i= 0; i < .i++)
            {
                if (n < [i])
                {
                    if (started)
                        buffer.put((byte)'0');
                    continue;
                }
                startedtrue;
                int dn / [i];
                buffer.put([d]);
                nn - d * [i];
            }
        }
    }
    
    public static void putDecLong(Buffer bufferlong n)
    {
        if (n < 0)
        {
            buffer.put((byte)'-');
            if (n == .)
            {
                buffer.put((byte)'9');
                n= 223372036854775808L;
            }
            else
                n= -n;
        }
        if (n < 10)
        {
            buffer.put([(int)n]);
        }
        else
        {
            boolean startedfalse;
            // This assumes constant time int arithmatic
            for (int i= 0; i < .i++)
            {
                if (n < [i])
                {
                    if (started)
                        buffer.put((byte)'0');
                    continue;
                }
                startedtrue;
                long dn / [i];
                buffer.put([(int)d]);
                nn - d * [i];
            }
        }
    }
    
    public static Buffer toBuffer(long value)
    {
        ByteArrayBuffer buf=new ByteArrayBuffer(32);
        putDecLong(bufvalue);
        return buf;
    }
    private final static int[] decDivisors=
    { 
        1000000000,
        100000000,
        10000000,
        1000000,
        100000,
        10000,
        1000,
        100,
        10,
        1 
    };
    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 void putCRLF(Buffer buffer)
    {
        buffer.put((byte)13);
        buffer.put((byte)10);
    }
    
    public static boolean isPrefix(Buffer prefix,Buffer buffer)
    {
        if (prefix.length()>buffer.length())
            return false;
        int bi=buffer.getIndex();
        for (int i=prefix.getIndex(); i<prefix.putIndex();i++)
            if (prefix.peek(i)!=buffer.peek(bi++))
                return false;
        return true;
    }
    public static String to8859_1_String(Buffer buffer)
    {
        if (buffer instanceof CachedBuffer)
            return buffer.toString();
        return buffer.toString(.);
    }
New to GrepCode? Check out our FAQ X