public class BytesReader {
private static final String TABLE = "0123456789ABCDEF"; private int position = 0; private byte[] data;
public BytesReader(byte[] bytes) { this.data = bytes; }
public boolean isLeftoverAllFF() { boolean allFF = true; if (data != null && position < data.length) { byte[] rst = new byte[data.length - position]; System.arraycopy(data, position, rst, 0, data.length - position); for (byte datum : rst) { if (datum != -1) { allFF = false; break; } } } return allFF; }
public boolean isNotLeftoverAllFF() { return !isLeftoverAllFF(); }
public byte[] getBytes(int length) { byte[] rst = null; if (data != null && position + length <= data.length) { rst = new byte[length]; System.arraycopy(data, position, rst, 0, length); position += length; } return rst; }
public byte getS8() { byte rst = 0; if (data != null && position + 1 <= data.length) { rst = data[position]; position += 1; } return rst; }
public short getU8() { short rst = 0; if (data != null && position + 1 <= data.length) { rst = data[position] < 0 ? (short) (256 + data[position]) : (short) (data[position]); position += 1; } return rst; }
public short getS16(boolean isLe) { short rst = 0; if (data != null && position + 2 <= data.length) { byte[] tmp = new byte[2]; System.arraycopy(data, position, tmp, 0, 2); position += 2; rst = isLe ? EndianUtils.le2Short(tmp) : EndianUtils.be2Short(tmp); } return rst; }
public short getS16() { return getS16(true); }
public int getU16(boolean isLe) { int rst = 0; if (data != null && position + 2 <= data.length) { byte[] tmp = new byte[2]; System.arraycopy(data, position, tmp, 0, 2); position += 2; rst = isLe ? EndianUtils.le2U16(tmp) : EndianUtils.be2U16(tmp); } return rst; }
public int getU16() { return getU16(true); }
public int getS32(boolean isLe) { int rst = 0; if (data != null && position + 4 <= data.length) { byte[] tmp = new byte[4]; System.arraycopy(data, position, tmp, 0, 4); position += 4; rst = isLe ? EndianUtils.le2Int(tmp) : EndianUtils.be2Int(tmp); } return rst; }
public int getS32() { return getS32(true); }
public long getU32(boolean isLe) { long rst = 0; if (data != null && position + 4 <= data.length) { byte[] tmp = new byte[4]; System.arraycopy(data, position, tmp, 0, 4); position += 4; rst = isLe ? EndianUtils.le2U32(tmp) : EndianUtils.be2U32(tmp); } return rst; }
public long getU32() { return getU32(true); }
public long getS64(boolean isLe) { long rst = 0; if (data != null && position + 8 <= data.length) { byte[] tmp = new byte[8]; System.arraycopy(data, position, tmp, 0, 8); position += 8; rst = isLe ? EndianUtils.le2Long(tmp) : EndianUtils.be2Long(tmp); } return rst; }
public long getS64() { return getS64(true); }
public String getString(int length, String charSet) { String rst = null; if (null != data) { if (position + length > data.length) { length = data.length - position; } byte[] tmp = new byte[length]; System.arraycopy(data, position, tmp, 0, length); position += length; try { rst = new String(tmp, charSet); } catch (UnsupportedEncodingException ignored) { } } return rst; }
public String getString(int length) { return getString(length, "GBK"); }
public int getPos() { return position; }
public boolean setPos(int pos) { if (null != data && pos < data.length) { position = pos; return true; } return false; }
public boolean movePos(int length) { if (null != data && position + length < data.length) { position += length; return true; } return false; }
public int residue() { int rst = 0; if (null != data) { rst = data.length - position; } return rst; }
public byte[] getResidue() { byte[] rst = null; if (data != null && position < data.length) { rst = new byte[data.length - position]; System.arraycopy(data, position, rst, 0, data.length - position); position = data.length; } return rst; }
public byte spyS8() { if (data != null && position < data.length) { return data[position]; } return 0; }
public short spyU8() { if (data != null && position < data.length) { return data[position] < 0 ? (short) (256 + data[position]) : (short) (data[position]); } return 0; }
public short spyS16(boolean isLe) { if (data != null && position + 1 < data.length) { byte[] tmp = new byte[2]; System.arraycopy(data, position, tmp, 0, 2); return isLe ? EndianUtils.le2Short(tmp) : EndianUtils.be2Short(tmp); } return 0; }
public int spyU16(boolean isLe) { if (data != null && position + 1 < data.length) { byte[] tmp = new byte[2]; System.arraycopy(data, position, tmp, 0, 2); return isLe ? EndianUtils.le2U16(tmp) : EndianUtils.be2U16(tmp); } return 0; }
public int spyS32(boolean isLe) { if (data != null && position + 3 < data.length) { byte[] tmp = new byte[4]; System.arraycopy(data, position, tmp, 0, 4); return isLe ? EndianUtils.le2Int(tmp) : EndianUtils.be2Int(tmp); } return 0; }
public long spyU32(boolean isLe) { if (data != null && position + 3 < data.length) { byte[] tmp = new byte[4]; System.arraycopy(data, position, tmp, 0, 4); return isLe ? EndianUtils.le2U32(tmp) : EndianUtils.be2U32(tmp); } return 0; }
public long spyS64(boolean isLe) { if (data != null && position + 7 < data.length) { byte[] tmp = new byte[8]; System.arraycopy(data, position, tmp, 0, 8); return isLe ? EndianUtils.le2Long(tmp) : EndianUtils.be2Long(tmp); } return 0; }
@Override public String toString() { return toString(data); }
public static String toString(byte[] src) { if (src != null) { StringBuilder rst = new StringBuilder(); for (byte aSrc : src) { short val = aSrc < 0 ? (short) (256 + aSrc) : aSrc; rst.append(TABLE.substring(((val >> 4) & 0x0f), (((val >> 4) & 0x0f) + 1))) .append(TABLE.substring((val & 0x0f), (val & 0x0f) + 1)) .append(" "); } return rst.toString(); } else { return "null"; } } }
|