parent
a6968642aa
commit
f48ac05cdd
@ -0,0 +1,98 @@
|
|||||||
|
package com.xxl.job.core.util;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* byte read util
|
||||||
|
* @author xuxueli 2015-11-15 03:50:10
|
||||||
|
*/
|
||||||
|
public class ByteReadFactory {
|
||||||
|
private static transient Logger logger = LoggerFactory.getLogger(ByteReadFactory.class);
|
||||||
|
private int m_iPos;
|
||||||
|
private int m_iReqLen;
|
||||||
|
private byte[] m_byte = null;
|
||||||
|
|
||||||
|
public ByteReadFactory(byte[] hexBytes){
|
||||||
|
m_iPos = 0;
|
||||||
|
m_byte = hexBytes;
|
||||||
|
m_iReqLen = m_byte.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int readInt() {
|
||||||
|
if (m_iPos + 4 > m_iReqLen) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int iInt = (m_byte[m_iPos] & 0xff)
|
||||||
|
| ((m_byte[m_iPos + 1] & 0xff) << 8)
|
||||||
|
| ((m_byte[m_iPos + 2] & 0xff) << 16)
|
||||||
|
| ((m_byte[m_iPos + 3] & 0xff) << 24);
|
||||||
|
m_iPos += 4;
|
||||||
|
return iInt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long readLong() {
|
||||||
|
if (m_iPos + 8 > m_iReqLen) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
long iLong = (m_byte[m_iPos] & 0xff)
|
||||||
|
| ((m_byte[m_iPos + 1] & 0xff) << 8)
|
||||||
|
| ((m_byte[m_iPos + 2] & 0xff) << 16)
|
||||||
|
| ((m_byte[m_iPos + 3] & 0xff) << 24)
|
||||||
|
| ((m_byte[m_iPos + 4] & 0xff) << 32)
|
||||||
|
| ((m_byte[m_iPos + 5] & 0xff) << 40)
|
||||||
|
| ((m_byte[m_iPos + 6] & 0xff) << 48)
|
||||||
|
| ((m_byte[m_iPos + 7] & 0xff) << 56);
|
||||||
|
m_iPos += 8;
|
||||||
|
return iLong;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String readString(int length) {
|
||||||
|
if (m_iPos + length > m_iReqLen) {
|
||||||
|
logger.error("[byte stream factory read string length error.]");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
for (index = 0; index < length; index++) {
|
||||||
|
if (m_byte[m_iPos + index] == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String msg = "";
|
||||||
|
try {
|
||||||
|
msg = new String(m_byte, m_iPos, index, "UTF-8");
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
logger.error("[byte stream factory read string exception.]", e);
|
||||||
|
}
|
||||||
|
m_iPos += length;
|
||||||
|
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] read(int length) {
|
||||||
|
if (m_iPos + length > m_iReqLen || length<=0) {
|
||||||
|
logger.error("[byte stream factory read string length error.]");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
if (m_byte[m_iPos + i] == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] result = new byte[length];
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
result[i] = m_byte[m_iPos + i];
|
||||||
|
}
|
||||||
|
m_iPos += length;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] readByteAll() {
|
||||||
|
return read(m_iReqLen - m_iPos);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
package com.xxl.job.core.util;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* byte write util
|
||||||
|
* @author xuxueli 2015-11-15 03:49:36
|
||||||
|
*/
|
||||||
|
public class ByteWriteFactory {
|
||||||
|
private static transient Logger logger = LoggerFactory.getLogger(ByteWriteFactory.class);
|
||||||
|
private ByteBuffer m_byteBuf = null;
|
||||||
|
public ByteWriteFactory() {
|
||||||
|
m_byteBuf = ByteBuffer.allocate(1024 * 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeInt(int intValue) {
|
||||||
|
byte[] intBytes = new byte[4];
|
||||||
|
for (int index = 0; index < 4; index++) {
|
||||||
|
intBytes[index] = (byte) (intValue >>> (index * 8));
|
||||||
|
}
|
||||||
|
m_byteBuf.put(intBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void write(int[] intArr) {
|
||||||
|
for (int index = 0; index < intArr.length; index++) {
|
||||||
|
writeInt(intArr[index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void write(byte[] byteArr) {
|
||||||
|
m_byteBuf.put(byteArr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void writeString(String value, int length) {
|
||||||
|
byte[] bytes = new byte[length];
|
||||||
|
if (value != null && value.trim().length() > 0) {
|
||||||
|
try {
|
||||||
|
byte[] infoBytes = value.getBytes("UTF-8");
|
||||||
|
int len = infoBytes.length < length ? infoBytes.length : length;
|
||||||
|
System.arraycopy(infoBytes, 0, bytes, 0, len);
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
logger.error("[response stream factory encoding exception.]", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_byteBuf.put(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getBytes() {
|
||||||
|
m_byteBuf.flip();
|
||||||
|
if (m_byteBuf.limit() == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] bytes = new byte[m_byteBuf.limit()];
|
||||||
|
m_byteBuf.get(bytes);
|
||||||
|
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue