ByteUtils.java :  » UML » MetaBoss » com » metaboss » util » Java Open Source

Java Open Source » UML » MetaBoss 
MetaBoss » com » metaboss » util » ByteUtils.java
// THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
// OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Copyright 2000-2005  Softaris Pty.Ltd. All Rights Reserved.
package com.metaboss.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/** Set of useful utilites to do with byte manipulations */
public class ByteUtils
{
    /** Fully reads given input stream and returns byte array with the result */
  public static  byte[] readByteStreamFully(InputStream str) throws IOException
  {
    ByteArrayOutputStream baos = null;
    try
    {
      baos = new ByteArrayOutputStream();
      StreamUtils.copyData(str,baos);
      baos.flush();
      return baos.toByteArray();
    }
    finally
    {
      if (baos != null)
        baos.close();
    }
  }

  /** Converts given binary byte array to the character string
     *  Each byte is represented by 2 successive characters in the string, which are the
     *  the hex representation of the value of the byte. This string is double in size of
     *  the underlying byte array, however it is safe to travel over many
     *  character based protocols such as XML. Use textToBytes() for reverse conversion
   * @param pBytes byte array to convert
   * @return String with hexadecimal representation of the bytes in array
   */
    public static String bytesToText(byte[] pBytes)
  {
        return bytesToText(pBytes, 0, pBytes.length);
  }

  /** Converts given binary byte array to the character string
     *  Each byte is represented by 2 successive characters in the string, which are the
     *  the hex representation of the value of the byte. This string is double in size of
     *  the underlying byte array, however it is safe to travel over many
     *  character based protocols such as XML. Use textToBytes() for reverse conversion
   * @param pBytes byte array to convert
   * @param pOffset offset in the given byte array to start conversion from
   * @param pLength number of bytes from offset to convert
   * @return String with hexadecimal representation of the bytes in array
   */
    public static String bytesToText(byte[] pBytes, int pOffset, int pLength)
  {
    StringBuffer lTextBuffer = new StringBuffer(pLength * 2);
        int lStopAtPos = pOffset + pLength;
    for (int i = pOffset; i < lStopAtPos; i++)
    {
      byte b = pBytes[i];
      byte bs_high = (byte)(b >>> 4);
      byte bs_low = (byte)(b & 0x0F);
      lTextBuffer.append(nibbleToChar(bs_high));
      lTextBuffer.append(nibbleToChar(bs_low));
    }
    return lTextBuffer.toString();
  }

  /** Converts given character string, which is assumed to be textual representation of the byte array
     *  to the byte array. Each byte is represented by 2 successive characters in the string, which are the
     *  the hex representation of the value of the byte. This string is double in size of
     *  the underlying byte array, however it is safe to travel over many
     *  character based protocols such as XML. Use bytesToText() for reverse conversion
   * @param pText string to convert
   * @return byte[] with converted bytes
     * @exception IllegalArgumentException is thrown when supplied text is not convertable to bytes (i.e. text has
     * not hexadecimal characters in it)
   */
  public static byte[] textToBytes(String pText) throws IllegalArgumentException
  {
    byte[] lBytes;
    if(pText == null)
      lBytes = new byte[0];
    else
    {
      int nBytes = pText.length() / 2;
      lBytes = new byte[nBytes];
      for (int i = 0; i < nBytes; i++)
      {
        int iStr = i * 2;
        byte hi = (byte) (charToNibble(pText.charAt(iStr)) << 4);
        byte lo = (byte) (charToNibble(pText.charAt(iStr+1)));
        lBytes[i] = (byte) (hi|lo);
      }
    }

    return lBytes;
  }

  // Helper. Converts nibble (four lower bits of the byte) to the character representing hex digit
  private static char nibbleToChar(byte p_nibble)
  {
    byte l_nibble = (byte)(p_nibble & 0x0F);
    if (l_nibble <= 0x9)
      return (char)(l_nibble + 48); // 0.....9
    return (char)(l_nibble + 55); // A.....F
  }

  // Helper. Converts hex digit character to nibble (four lower bits of the byte)
  private static byte charToNibble(char p_char)
  {
    if (p_char <= '9')
      return (byte) (((byte)p_char) - 48); // 0.....9
    return (byte)(((byte)p_char) - 55); // A.....F
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.