001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.util;
016:
017: import java.io.ByteArrayOutputStream;
018: import java.io.IOException;
019: import java.io.InputStream;
020:
021: /** Set of useful utilites to do with byte manipulations */
022: public class ByteUtils {
023: /** Fully reads given input stream and returns byte array with the result */
024: public static byte[] readByteStreamFully(InputStream str)
025: throws IOException {
026: ByteArrayOutputStream baos = null;
027: try {
028: baos = new ByteArrayOutputStream();
029: StreamUtils.copyData(str, baos);
030: baos.flush();
031: return baos.toByteArray();
032: } finally {
033: if (baos != null)
034: baos.close();
035: }
036: }
037:
038: /** Converts given binary byte array to the character string
039: * Each byte is represented by 2 successive characters in the string, which are the
040: * the hex representation of the value of the byte. This string is double in size of
041: * the underlying byte array, however it is safe to travel over many
042: * character based protocols such as XML. Use textToBytes() for reverse conversion
043: * @param pBytes byte array to convert
044: * @return String with hexadecimal representation of the bytes in array
045: */
046: public static String bytesToText(byte[] pBytes) {
047: return bytesToText(pBytes, 0, pBytes.length);
048: }
049:
050: /** Converts given binary byte array to the character string
051: * Each byte is represented by 2 successive characters in the string, which are the
052: * the hex representation of the value of the byte. This string is double in size of
053: * the underlying byte array, however it is safe to travel over many
054: * character based protocols such as XML. Use textToBytes() for reverse conversion
055: * @param pBytes byte array to convert
056: * @param pOffset offset in the given byte array to start conversion from
057: * @param pLength number of bytes from offset to convert
058: * @return String with hexadecimal representation of the bytes in array
059: */
060: public static String bytesToText(byte[] pBytes, int pOffset,
061: int pLength) {
062: StringBuffer lTextBuffer = new StringBuffer(pLength * 2);
063: int lStopAtPos = pOffset + pLength;
064: for (int i = pOffset; i < lStopAtPos; i++) {
065: byte b = pBytes[i];
066: byte bs_high = (byte) (b >>> 4);
067: byte bs_low = (byte) (b & 0x0F);
068: lTextBuffer.append(nibbleToChar(bs_high));
069: lTextBuffer.append(nibbleToChar(bs_low));
070: }
071: return lTextBuffer.toString();
072: }
073:
074: /** Converts given character string, which is assumed to be textual representation of the byte array
075: * to the byte array. Each byte is represented by 2 successive characters in the string, which are the
076: * the hex representation of the value of the byte. This string is double in size of
077: * the underlying byte array, however it is safe to travel over many
078: * character based protocols such as XML. Use bytesToText() for reverse conversion
079: * @param pText string to convert
080: * @return byte[] with converted bytes
081: * @exception IllegalArgumentException is thrown when supplied text is not convertable to bytes (i.e. text has
082: * not hexadecimal characters in it)
083: */
084: public static byte[] textToBytes(String pText)
085: throws IllegalArgumentException {
086: byte[] lBytes;
087: if (pText == null)
088: lBytes = new byte[0];
089: else {
090: int nBytes = pText.length() / 2;
091: lBytes = new byte[nBytes];
092: for (int i = 0; i < nBytes; i++) {
093: int iStr = i * 2;
094: byte hi = (byte) (charToNibble(pText.charAt(iStr)) << 4);
095: byte lo = (byte) (charToNibble(pText.charAt(iStr + 1)));
096: lBytes[i] = (byte) (hi | lo);
097: }
098: }
099:
100: return lBytes;
101: }
102:
103: // Helper. Converts nibble (four lower bits of the byte) to the character representing hex digit
104: private static char nibbleToChar(byte p_nibble) {
105: byte l_nibble = (byte) (p_nibble & 0x0F);
106: if (l_nibble <= 0x9)
107: return (char) (l_nibble + 48); // 0.....9
108: return (char) (l_nibble + 55); // A.....F
109: }
110:
111: // Helper. Converts hex digit character to nibble (four lower bits of the byte)
112: private static byte charToNibble(char p_char) {
113: if (p_char <= '9')
114: return (byte) (((byte) p_char) - 48); // 0.....9
115: return (byte) (((byte) p_char) - 55); // A.....F
116: }
117: }
|