com.dusbabek.lib.id3
Class ByteUtils

java.lang.Object
  extended by com.dusbabek.lib.id3.ByteUtils

public class ByteUtils
extends java.lang.Object

Utilities for converting long to bytes and vice versa. Please note that the longs I deal with are only 4 bytes in size (not 8). I could have gotten away with using ints, but java has a nasty habit of using sign extension which would have foiled me for large UNSIGNED values.


Constructor Summary
ByteUtils()
           
 
Method Summary
static long addZeroBits(long l)
          Unique to Id3s.
static long byte3ToLong(byte[] b, int offset)
          convert a 3 byte array to an 8 byte long.
static long byte4ToLong(byte[] b, int offset)
          convert a 4 byte array to an 8 byte long.
static java.lang.String byteToString(byte b)
           
static byte[] longToByte3(long l)
          convert long to 3 byte array.
static byte[] longToByte4(long l)
          convert long to 4 byte array.
static long removeZeroBits(long l)
          This is unique to Id3s.
static byte[] sync(byte[] data)
           
static byte[] unsync(byte[] data)
           
static byte[] zip_expand(byte[] b, long size)
          decompress data using ZLIB.
static byte[] zip(byte[] b)
          compress data using ZLIB
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteUtils

public ByteUtils()
Method Detail

byte3ToLong

public static long byte3ToLong(byte[] b,
                               int offset)
convert a 3 byte array to an 8 byte long. only last 3 bytes matter though.

Parameters:
b - byte[]
offset - int
Returns:
long

byte4ToLong

public static long byte4ToLong(byte[] b,
                               int offset)
convert a 4 byte array to an 8 byte long. only last 4 bytes matter though.

Parameters:
b - byte[]
offset - int
Returns:
long

longToByte3

public static byte[] longToByte3(long l)
convert long to 3 byte array. Only the last 3 bytes of l are significant.

Parameters:
l - long
Returns:
byte[]

longToByte4

public static byte[] longToByte4(long l)
convert long to 4 byte array. Only the last 4 bytes of l are significant.

Parameters:
l - long
Returns:
byte[]

removeZeroBits

public static long removeZeroBits(long l)
This is unique to Id3s. It doesn't really belong here. Tag headers are encoded in 4 bytes, but only the 7 least bits are significant. So take the long; convert it to 4 bytes; then throw away the first bit of each byte. It is IMPORTANT to get this method right because it is used to report the tag size, which is crucial.

Parameters:
l - long
Returns:
long

addZeroBits

public static long addZeroBits(long l)
Unique to Id3s. Used in putting the tag size field (4 bytes) into spec format. The size field is encoded in 28 bits spread over 4 bytes (32 bits). So for each 7 bits, add another at the front. The spec claims to explain whether or not that bit should be 1 or 0, but for the life of me, I couldn't figure it out. Maybe I need to read it again. For now, until I get my head on straight, I am just padding with 0.

Parameters:
l - long
Returns:
long

zip_expand

public static byte[] zip_expand(byte[] b,
                                long size)
                         throws java.io.IOException
decompress data using ZLIB.

Parameters:
b - byte[] data to decompress.
size - long expected uncompressed size.
Returns:
byte[] uncompressed data.
Throws:
java.io.IOException

zip

public static byte[] zip(byte[] b)
                  throws java.io.IOException
compress data using ZLIB

Parameters:
b - byte[] data to compress
Returns:
byte[] compressed data.
Throws:
java.io.IOException

byteToString

public static java.lang.String byteToString(byte b)

unsync

public static byte[] unsync(byte[] data)

sync

public static byte[] sync(byte[] data)


Copyright © 2004, 2005 Gary Dusbabek. All Rights Reserved.