Refactoring of BinaryStructInputStream to use a stream
This commit is contained in:
parent
227f294ae0
commit
c378e98794
4 changed files with 62 additions and 30 deletions
|
|
@ -72,6 +72,6 @@ public class MulticastDNSClient extends ThreadedUDPNetwork implements ThreadedUD
|
|||
@Override
|
||||
public void receivedPacket(DatagramPacket packet, ThreadedUDPNetwork network) {
|
||||
DNSPacket header = new DNSPacket();
|
||||
int length = BinaryStructInputStream.parse(header, packet.getData());
|
||||
BinaryStructInputStream.read(header, packet.getData());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,8 +43,8 @@ public class CSVParser extends Parser{
|
|||
public CSVParser(Reader in){
|
||||
this(in, false, ',');
|
||||
}
|
||||
public CSVParser(Reader in, boolean inclusedHeader){
|
||||
this(in, inclusedHeader, ',');
|
||||
public CSVParser(Reader in, boolean includedHeader){
|
||||
this(in, includedHeader, ',');
|
||||
}
|
||||
public CSVParser(Reader in, boolean includesHeader, char delimiter){
|
||||
this.in = in;
|
||||
|
|
|
|||
|
|
@ -28,6 +28,10 @@ import zutil.ByteUtil;
|
|||
import zutil.converter.Converter;
|
||||
import zutil.parser.binary.BinaryStruct.BinaryField;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
|
@ -42,31 +46,59 @@ import java.util.List;
|
|||
*/
|
||||
public class BinaryStructInputStream {
|
||||
|
||||
public static int parse(BinaryStruct struct, byte[] data) {
|
||||
private InputStream in;
|
||||
private byte data;
|
||||
private int dataBitIndex = -1;
|
||||
|
||||
public BinaryStructInputStream(InputStream in){
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parses a byte array and assigns all fields in the struct
|
||||
*/
|
||||
public static int read(BinaryStruct struct, byte[] data) {
|
||||
int read = 0;
|
||||
try {
|
||||
ByteArrayInputStream buffer = new ByteArrayInputStream(data);
|
||||
BinaryStructInputStream in = new BinaryStructInputStream(buffer);
|
||||
read = in.read(struct);
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return read;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the given struct from the input stream
|
||||
*/
|
||||
public int read(BinaryStruct struct) throws IOException {
|
||||
List<BinaryFieldData> structDataList = BinaryFieldData.getStructFieldList(struct.getClass());
|
||||
int bitOffset = 0;
|
||||
|
||||
int totalReadLength = 0;
|
||||
for (BinaryFieldData field : structDataList){
|
||||
|
||||
int byteIndex = bitOffset / 8;
|
||||
int bitIndex = 7 - bitOffset % 8;
|
||||
int bitLength = Math.min(bitIndex+1, field.getBitLength());
|
||||
|
||||
int readLength = 0;
|
||||
byte[] valueData = new byte[(int) Math.ceil(field.getBitLength() / 8.0)];
|
||||
for (int index = 0; index < valueData.length; ++index) {
|
||||
valueData[index] = ByteUtil.getShiftedBits(data[byteIndex], bitIndex, bitLength);
|
||||
readLength += bitLength;
|
||||
byteIndex++;
|
||||
bitIndex = 7;
|
||||
bitLength = Math.min(bitIndex+1, field.getBitLength() - readLength);
|
||||
int fieldReadLength = 0;
|
||||
|
||||
// Parse value
|
||||
for (int valueDataIndex = 0; valueDataIndex < valueData.length; ++valueDataIndex) {
|
||||
if(dataBitIndex < 0) { // Read new data?
|
||||
data = (byte) in.read();
|
||||
dataBitIndex = 7;
|
||||
}
|
||||
int bitLength = Math.min(dataBitIndex+1, field.getBitLength() - fieldReadLength);
|
||||
valueData[valueDataIndex] = ByteUtil.getShiftedBits(data, dataBitIndex, bitLength);
|
||||
fieldReadLength += bitLength;
|
||||
dataBitIndex -= bitLength;
|
||||
}
|
||||
// Set value
|
||||
field.setValue(struct, valueData);
|
||||
bitOffset += field.getBitLength();
|
||||
totalReadLength += fieldReadLength;
|
||||
}
|
||||
return bitOffset;
|
||||
|
||||
return totalReadLength;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ public class BinaryStructInputStreamTest {
|
|||
}
|
||||
};
|
||||
|
||||
BinaryStructInputStream.parse(struct, new byte[]{0,0,0,1, 0,0,0,2});
|
||||
BinaryStructInputStream.read(struct, new byte[]{0,0,0,1, 0,0,0,2});
|
||||
struct.assertObj();
|
||||
}
|
||||
|
||||
|
|
@ -66,11 +66,11 @@ public class BinaryStructInputStreamTest {
|
|||
|
||||
public void assertObj(){
|
||||
assertFalse(b1);
|
||||
assertFalse(b2);
|
||||
assert(b2);
|
||||
}
|
||||
};
|
||||
|
||||
BinaryStructInputStream.parse(struct, new byte[]{0b0100_000});
|
||||
BinaryStructInputStream.read(struct, new byte[]{0b0100_0000});
|
||||
struct.assertObj();
|
||||
}
|
||||
|
||||
|
|
@ -85,7 +85,7 @@ public class BinaryStructInputStreamTest {
|
|||
}
|
||||
};
|
||||
|
||||
BinaryStructInputStream.parse(struct, "hello world!".getBytes());
|
||||
BinaryStructInputStream.read(struct, "hello world!".getBytes());
|
||||
struct.assertObj();
|
||||
}
|
||||
|
||||
|
|
@ -103,7 +103,7 @@ public class BinaryStructInputStreamTest {
|
|||
}
|
||||
};
|
||||
|
||||
BinaryStructInputStream.parse(struct, new byte[]{0b0000_0000,0b0001_1000,0b0000_0000});
|
||||
BinaryStructInputStream.read(struct, new byte[]{0b0000_0000,0b0001_1000,0b0000_0000});
|
||||
struct.assertObj();
|
||||
}
|
||||
|
||||
|
|
@ -122,7 +122,7 @@ public class BinaryStructInputStreamTest {
|
|||
}
|
||||
};
|
||||
|
||||
BinaryStructInputStream.parse(struct, new byte[]{0b0000_0001,0b0001_1000,0b0000_0000});
|
||||
BinaryStructInputStream.read(struct, new byte[]{0b0000_0001,0b0001_1000,0b0000_0000});
|
||||
struct.assertObj();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue