📄 mysqlpacketbuffer.java
字号:
| ((long) (b[this.position++] & 0xff) << 8)
| ((long) (b[this.position++] & 0xff) << 16)
| ((long) (b[this.position++] & 0xff) << 24)
| ((long) (b[this.position++] & 0xff) << 32)
| ((long) (b[this.position++] & 0xff) << 40)
| ((long) (b[this.position++] & 0xff) << 48)
| ((long) (b[this.position++] & 0xff) << 56);
}
final int readnBytes() {
int sw = this.buffer[this.position++] & 0xff;
switch (sw) {
case 1:
return this.buffer[this.position++] & 0xff;
case 2:
return this.readInt();
case 3:
return this.readLongInt();
case 4:
return (int) this.readLong();
default:
return 255;
}
}
//
// Read a null-terminated string
//
// To avoid alloc'ing a new byte array, we
// do this by hand, rather than calling getNullTerminatedBytes()
//
final String readString() {
int i = this.position;
int len = 0;
int maxLen = getBufLength();
while ((i < maxLen) && (this.buffer[i] != 0)) {
len++;
i++;
}
String s = new String(this.buffer, this.position, len);
this.position += (len + 1); // update cursor
return s;
}
final String readLengthCodedString(String encoding){
int fieldLength = (int)this.readFieldLength();
if(fieldLength ==0) return null;
try {
if(encoding != null){
return new String(this.buffer, this.position, (int)fieldLength, encoding);
}else{
return new String(this.buffer, this.position, fieldLength);
}
}catch(UnsupportedEncodingException e){
//TODO logger exception
return new String(this.buffer, this.position, fieldLength);
} finally {
this.position += fieldLength; // update cursor
}
}
public static boolean isErrorPacket(byte[] bty){
return isPacketType(bty, (byte)0xff);
}
public static boolean isEofPacket(byte[] bty){
return isPacketType(bty,(byte)0xfe);
}
public static int increasePacketId(int packetId){
if(packetId >= 255){
return 0;
}else{
return packetId++;
}
}
public static boolean isPacketType(byte[] bytes,byte type){
if(bytes.length>=5){
return bytes[4] == type;
}
return false;
}
public static boolean isOkPacket(byte[] bty){
return isPacketType(bty, (byte)0x00);
}
final String readString(String encoding){
int i = this.position;
int len = 0;
int maxLen = getBufLength();
while ((i < maxLen) && (this.buffer[i] != 0)) {
len++;
i++;
}
try {
return new String(this.buffer, this.position, len, encoding);
}catch(UnsupportedEncodingException e){
//TODO logger exception
return new String(this.buffer, this.position, len);
} finally {
this.position += (len + 1); // update cursor
}
}
void setBufLength(int bufLengthToSet) {
this.length = bufLengthToSet;
}
/**
* Sets the array of bytes to use as a buffer to read from.
*
* @param buffer
* the array of bytes to use as a buffer
*/
public void setByteBuffer(byte[] byteBufferToSet) {
this.buffer = byteBufferToSet;
}
/**
* Sets whether this packet was part of a multipacket
*
* @param flag
* was this packet part of a multipacket?
*/
public void setWasMultiPacket(boolean flag) {
this.wasMultiPacket = flag;
}
public String toString() {
return dumpClampedBytes(getPosition());
}
public String toSuperString() {
return super.toString();
}
/**
* Was this packet part of a multipacket?
*
* @return was this packet part of a multipacket?
*/
public boolean wasMultiPacket() {
return this.wasMultiPacket;
}
// Write a byte array
final void writeBytesNoNull(byte[] bytes){
int len = bytes.length;
ensureCapacity(len);
System.arraycopy(bytes, 0, this.buffer, this.position, len);
this.position += len;
}
// Write a byte array with the given offset and length
final void writeBytesNoNull(byte[] bytes, int offset, int length)
throws SQLException {
ensureCapacity(length);
System.arraycopy(bytes, offset, this.buffer, this.position, length);
this.position += length;
}
public final void writeDouble(double d){
long l = Double.doubleToLongBits(d);
writeLongLong(l);
}
public double readDouble(){
long result = readLongLong();
return Double.longBitsToDouble(result);
}
public final void writeFieldLength(long length){
if (length < 251) {
writeByte((byte) length);
} else if (length < 65536L) {
ensureCapacity(3);
writeByte((byte) 252);
writeInt((int) length);
} else if (length < 16777216L) {
ensureCapacity(4);
writeByte((byte) 253);
writeLongInt((int) length);
} else {
ensureCapacity(9);
writeByte((byte) 254);
writeLongLong(length);
}
}
public final void writeFloat(float f){
ensureCapacity(4);
int i = Float.floatToIntBits(f);
byte[] b = this.buffer;
b[this.position++] = (byte) (i & 0xff);
b[this.position++] = (byte) (i >>> 8);
b[this.position++] = (byte) (i >>> 16);
b[this.position++] = (byte) (i >>> 24);
}
final float readFloat(){
byte[] b = this.buffer;
int result =((int) b[this.position++] & 0xff)
| (((int) b[this.position++] & 0xff) << 8)
| ((int) (b[this.position++] & 0xff) << 16)
| ((int) (b[this.position++] & 0xff) << 24);
return Float.intBitsToFloat(result);
}
// 2000-06-05 Changed
final void writeInt(int i){
ensureCapacity(2);
byte[] b = this.buffer;
b[this.position++] = (byte) (i & 0xff);
b[this.position++] = (byte) (i >>> 8);
}
// Write a String using the specified character
// encoding
final void writeLenBytes(byte[] b){
int len = b.length;
ensureCapacity(len + 9);
writeFieldLength(len);
System.arraycopy(b, 0, this.buffer, this.position, len);
this.position += len;
}
public final void writeLengthCodedString(String s,String encoding){
if(s != null){
byte[] b;
try {
b = s.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
//TODO
e.printStackTrace();
b = s.getBytes();
}
ensureCapacity(b.length + 9);
this.writeFieldLength(b.length);
this.writeBytesNoNull(b);
}else{
this.writeByte((byte)0);
}
}
// Write a String using the specified character
// encoding
final void writeLenString(String s, String encoding, String serverEncoding,
SingleByteCharsetConverter converter, boolean parserKnowsUnicode)
throws UnsupportedEncodingException{
byte[] b = null;
if (converter != null) {
b = converter.toBytes(s);
} else {
b = MysqlStringUtil.getBytes(s,converter, encoding, serverEncoding,
parserKnowsUnicode);
}
int len = b.length;
ensureCapacity(len + 9);
writeFieldLength(len);
System.arraycopy(b, 0, this.buffer, this.position, len);
this.position += len;
}
final void writeLong(long i){
ensureCapacity(4);
byte[] b = this.buffer;
b[this.position++] = (byte) (i & 0xff);
b[this.position++] = (byte) (i >>> 8);
b[this.position++] = (byte) (i >>> 16);
b[this.position++] = (byte) (i >>> 24);
}
final void writeLongInt(int i){
ensureCapacity(3);
byte[] b = this.buffer;
b[this.position++] = (byte) (i & 0xff);
b[this.position++] = (byte) (i >>> 8);
b[this.position++] = (byte) (i >>> 16);
}
final void writeLongLong(long i){
ensureCapacity(8);
byte[] b = this.buffer;
b[this.position++] = (byte) (i & 0xff);
b[this.position++] = (byte) (i >>> 8);
b[this.position++] = (byte) (i >>> 16);
b[this.position++] = (byte) (i >>> 24);
b[this.position++] = (byte) (i >>> 32);
b[this.position++] = (byte) (i >>> 40);
b[this.position++] = (byte) (i >>> 48);
b[this.position++] = (byte) (i >>> 56);
}
// Write null-terminated string
final void writeString(String s){
ensureCapacity((s.length() * 2) + 1);
writeStringNoNull(s);
this.buffer[this.position++] = 0;
}
// Write null-terminated string in the given encoding
final void writeString(String s, String encoding) throws UnsupportedEncodingException{
ensureCapacity((s.length() * 2) + 1);
writeStringNoNull(s, encoding, encoding, false);
this.buffer[this.position++] = 0;
}
// Write string, with no termination
final void writeStringNoNull(String s){
int len = s.length();
ensureCapacity(len * 2);
System.arraycopy(s.getBytes(), 0, this.buffer, this.position, len);
this.position += len;
}
// Write a String using the specified character
// encoding
final void writeStringNoNull(String s, String encoding,
String serverEncoding, boolean parserKnowsUnicode) throws UnsupportedEncodingException{
byte[] b = null;
SingleByteCharsetConverter converter = SingleByteCharsetConverter.getInstance(encoding);
b = MysqlStringUtil.getBytes(s, converter,encoding, serverEncoding,
parserKnowsUnicode);
int len = b.length;
ensureCapacity(len);
System.arraycopy(b, 0, this.buffer, this.position, len);
this.position += len;
}
/**
* 将从0当到前位置的所有字节写入到 ByteBuffer中,并且将 ByteBuffer position设置到0
* @return
*/
public ByteBuffer toByteBuffer(){
ByteBuffer buffer = ByteBuffer.allocate(this.getPacketLength()+4);
buffer.put(this.buffer,0,this.getPacketLength()+4);
buffer.rewind();
return buffer;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -