📄 xbytebuffer.java
字号:
/**
* Extracts the message bytes from a package.
* If no package exists, a IllegalStateException will be thrown.
* @param clearFromBuffer - if true, the package will be removed from the byte buffer
* @return - returns the actual message bytes (header, size and footer not included).
*/
public byte[] extractPackage(boolean clearFromBuffer) throws java.io.IOException {
int psize = countPackages();
if ( psize == 0 ) throw new java.lang.IllegalStateException("No package exists in XByteBuffer");
int size = toInt(buf, START_DATA.length);
byte[] data = new byte[size];
System.arraycopy(buf,START_DATA.length+4,data,0,size);
if ( clearFromBuffer ) {
int totalsize = START_DATA.length + 4 + size + END_DATA.length;
bufSize = bufSize - totalsize;
System.arraycopy(buf, totalsize, buf, 0, bufSize);
}
java.io.ByteArrayInputStream bin = new java.io.ByteArrayInputStream(data);
java.util.zip.GZIPInputStream gin = new java.util.zip.GZIPInputStream(bin);
byte[] tmp = new byte[1024];
byte[] result = new byte[0];
int length = gin.read(tmp);
while ( length > 0 ) {
byte[] tmpdata = result;
result = new byte[result.length+length];
System.arraycopy(tmpdata,0,result,0,tmpdata.length);
System.arraycopy(tmp,0,result,tmpdata.length,length);
length = gin.read(tmp);
}
gin.close();
return result;
}//extractPackage
/**
* Convert four bytes to an int
* @param b - the byte array containing the four bytes
* @param off - the offset
* @return the integer value constructed from the four bytes
* @exception java.lang.ArrayOutOfBoundsException
*/
public static int toInt(byte[] b,int off){
return ( ( (int) b[off+3]) & 0xFF) +
( ( ( (int) b[off+2]) & 0xFF) << 8) +
( ( ( (int) b[off+1]) & 0xFF) << 16) +
( ( ( (int) b[off+0]) & 0xFF) << 24);
}//toInt
/**
* Convert eight bytes to a long
* @param b - the byte array containing the four bytes
* @param off - the offset
* @return the long value constructed from the eight bytes
* @exception java.lang.ArrayOutOfBoundsException
*/
public static long toLong(byte[] b,int off){
return ( ( (long) b[off+7]) & 0xFF) +
( ( ( (long) b[off+6]) & 0xFF) << 8) +
( ( ( (long) b[off+5]) & 0xFF) << 16) +
( ( ( (long) b[off+4]) & 0xFF) << 24) +
( ( ( (long) b[off+3]) & 0xFF) << 32) +
( ( ( (long) b[off+2]) & 0xFF) << 40) +
( ( ( (long) b[off+1]) & 0xFF) << 48) +
( ( ( (long) b[off+0]) & 0xFF) << 56);
}//toInt
/**
* Converts an integer to four bytes
* @param n - the integer
* @return - four bytes in an array
*/
public static byte[] toBytes(int n) {
byte[] b = new byte[4];
b[3] = (byte) (n);
n >>>= 8;
b[2] = (byte) (n);
n >>>= 8;
b[1] = (byte) (n);
n >>>= 8;
b[0] = (byte) (n);
return b;
} //toBytes
/**
* Converts an long to eight bytes
* @param n - the long
* @return - eight bytes in an array
*/
public static byte[] toBytes(long n) {
byte[] b = new byte[8];
b[7] = (byte) (n);
n >>>= 8;
b[6] = (byte) (n);
n >>>= 8;
b[5] = (byte) (n);
n >>>= 8;
b[4] = (byte) (n);
n >>>= 8;
b[3] = (byte) (n);
n >>>= 8;
b[2] = (byte) (n);
n >>>= 8;
b[1] = (byte) (n);
n >>>= 8;
b[0] = (byte) (n);
return b;
} //toBytes
/**
* Similar to a String.IndexOf, but uses pure bytes
* @param src - the source bytes to be searched
* @param srcOff - offset on the source buffer
* @param find - the string to be found within src
* @return - the index of the first matching byte. -1 if the find array is not found
*/
public static int firstIndexOf(byte[] src, int srcOff, byte[] find){
int result = -1;
if (find.length > src.length) return result;
if (find.length == 0 || src.length == 0) return result;
if (srcOff >= src.length ) throw new java.lang.ArrayIndexOutOfBoundsException();
boolean found = false;
int srclen = src.length;
int findlen = find.length;
byte first = find[0];
int pos = srcOff;
while (!found) {
//find the first byte
while (pos < srclen){
if (first == src[pos])
break;
pos++;
} //while
if (pos >= srclen)
return -1;
//we found the first character
//match the rest of the bytes - they have to match
if ( (srclen - pos) < findlen)
return -1;
//assume it does exist
found = true;
for (int i = 1; ( (i < findlen) && found); i++)
found = found && (find[i] == src[pos + i]);
if (found)
result = pos;
else if ( (srclen - pos) < findlen)
return -1; //no more matches possible
else
pos++;
} //while
return result;
} //firstIndexOf
/**
* Creates a complete data package
* @param data - the message data to be contained within the package
* @return - a full package (header,size,data,footer)
*/
public static byte[] createDataPackage(byte[] indata) throws java.io.IOException {
java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream(indata.length/2);
java.util.zip.GZIPOutputStream gout = new java.util.zip.GZIPOutputStream(bout);
gout.write(indata);
gout.flush();
gout.close();
byte[] data = bout.toByteArray();
byte[] result = new byte[START_DATA.length+4+data.length+END_DATA.length];
System.arraycopy(START_DATA,0,result,0,START_DATA.length);
System.arraycopy(toBytes(data.length),0,result,START_DATA.length,4);
System.arraycopy(data,0,result,START_DATA.length+4,data.length);
System.arraycopy(END_DATA,0,result,START_DATA.length+4+data.length,END_DATA.length);
return result;
}//createDataPackage
public static void main(String[] args) throws Exception {
log.info("Before="+Integer.MAX_VALUE);
byte[] d = toBytes(Integer.MAX_VALUE);
log.info("After="+toInt(d,0));
log.info("Before="+Long.MAX_VALUE);
d = toBytes(Long.MAX_VALUE);
log.info("After="+toLong(d,0));
log.info("Before=" + 4564564);
d = toBytes((long)4564564);
log.info("After=" + toLong(d, 0));
byte[] d1 = createDataPackage(new byte[] {1});
byte[] d2 = createDataPackage(new byte[] {2});
byte[] d3 = createDataPackage(new byte[] {3});
byte[] test = new byte[d1.length+d2.length+d3.length+5];
System.arraycopy(d1,0,test,0,d1.length);
System.arraycopy(d2,0,test,d1.length,d2.length);
System.arraycopy(d3,0,test,d2.length+d1.length,d3.length);
printBuf(d1);
printBuf(d2);
printBuf(d3);
printBuf(test);
XByteBuffer b = new XByteBuffer();
b.append(test,0,test.length);
int s = b.countPackages();
log.info("Nr of packages="+s);
while ( s > 0 ) {
d = b.extractPackage(true);
System.out.print("Package d1=");
printBuf(d);
s--;
}//while
}
public static void printBuf(byte[] b) {
StringBuffer buf = new StringBuffer();
for ( int i=0; i<b.length; i++ ) {
buf.append(b[i] + " ");
}
log.info(buf);
}
}//class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -