forked from KNEMC/KLALB
UPDATE 2.2
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package org.kne.io;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
public class VarDataInputStream extends DataInputStream {
|
||||
|
||||
public VarDataInputStream(InputStream in) {
|
||||
super(in);
|
||||
|
||||
}
|
||||
public byte[] readPacketVarBytes() throws IOException {
|
||||
|
||||
int i;
|
||||
try {
|
||||
i=readVarInt();
|
||||
}catch(Exception e) {
|
||||
return null;
|
||||
}
|
||||
byte[]b=new byte[i];
|
||||
readFully(b);
|
||||
return b;
|
||||
}
|
||||
public byte[] readVarBytes() throws IOException {
|
||||
|
||||
int i=readVarInt();
|
||||
byte[]b=new byte[i];
|
||||
readFully(b);
|
||||
return b;
|
||||
}
|
||||
public String readVarUTF8() throws IOException {
|
||||
return new String(readVarBytes(), Charset.forName("UTF-8"));
|
||||
}
|
||||
int cache=0;
|
||||
public int readVarInt_addd()throws IOException {
|
||||
cache+=readVarInt();
|
||||
return cache;
|
||||
}
|
||||
public int readVarInt() throws IOException {
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
|
||||
while (true) {
|
||||
byte b0 = this.readByte();
|
||||
i |= (b0 & 127) << j++ * 7;
|
||||
|
||||
if (j > 5) {
|
||||
throw new RuntimeException("VarInt too big");
|
||||
}
|
||||
|
||||
if ((b0 & 128) != 128) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
public long readVarLong() throws IOException {
|
||||
long i = 0L;
|
||||
int j = 0;
|
||||
|
||||
while (true) {
|
||||
byte b0 = this.readByte();
|
||||
i |= (long) (b0 & 127) << j++ * 7;
|
||||
|
||||
if (j > 10) {
|
||||
throw new RuntimeException("VarLong too big");
|
||||
}
|
||||
|
||||
if ((b0 & 128) != 128) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.kne.io;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
public class VarDataOutputStream extends DataOutputStream {
|
||||
|
||||
public VarDataOutputStream(OutputStream out) {
|
||||
super(out);
|
||||
}
|
||||
int cache=0;
|
||||
public void writeVarInt_add(int input)throws IOException{
|
||||
writeVarInt (input-cache);
|
||||
cache=input;
|
||||
}
|
||||
public void writeVarBytes(byte[]b) throws IOException {
|
||||
writeVarInt(b.length);
|
||||
write(b);
|
||||
}
|
||||
public void writeVarUTF8(String str) throws IOException {
|
||||
writeVarBytes(str.getBytes(Charset.forName("UTF-8")));
|
||||
}
|
||||
public void writeVarInt(int input) throws IOException {
|
||||
while ((input & -128) != 0) {
|
||||
this.out.write(input & 127 | 128);
|
||||
input >>>= 7;
|
||||
}
|
||||
|
||||
this.out.write(input);
|
||||
}
|
||||
|
||||
|
||||
public void writeVarLong(long value) throws IOException {
|
||||
while ((value & -128L) != 0L) {
|
||||
this.writeByte((int) (value & 127L) | 128);
|
||||
value >>>= 7;
|
||||
}
|
||||
|
||||
this.writeByte((int) value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user