Refactoring
This commit is contained in:
parent
e621ab159c
commit
44b96d9f2c
@ -229,6 +229,17 @@ public class ClientManagementTCP implements Runnable {
|
|||||||
} while(!fileFullyWritten);
|
} while(!fileFullyWritten);
|
||||||
if (!Arrays.equals(hash512, computeHashsum(filename, HashAlgorithm.SHA512))) {
|
if (!Arrays.equals(hash512, computeHashsum(filename, HashAlgorithm.SHA512))) {
|
||||||
System.err.println("Error: Hashsum does not match");
|
System.err.println("Error: Hashsum does not match");
|
||||||
|
System.err.println("Computed checksum:");
|
||||||
|
byte[] c = computeHashsum(filename, HashAlgorithm.SHA512);
|
||||||
|
for (byte b: c) {
|
||||||
|
System.err.print(String.format("%02X", b));
|
||||||
|
}
|
||||||
|
System.err.println("");
|
||||||
|
System.err.println("Received checksum:");
|
||||||
|
for (byte b: hash512) {
|
||||||
|
System.err.print(String.format("%02X", b));
|
||||||
|
}
|
||||||
|
System.err.println("");
|
||||||
throw new InternalError();
|
throw new InternalError();
|
||||||
}
|
}
|
||||||
socket.close();
|
socket.close();
|
||||||
|
@ -216,6 +216,17 @@ public class ClientManagementUDP implements Runnable {
|
|||||||
} while(!fileFullyWritten);
|
} while(!fileFullyWritten);
|
||||||
if (!Arrays.equals(hash512, computeHashsum(filename, HashAlgorithm.SHA512))) {
|
if (!Arrays.equals(hash512, computeHashsum(filename, HashAlgorithm.SHA512))) {
|
||||||
System.err.println("Error: Hashsum does not match");
|
System.err.println("Error: Hashsum does not match");
|
||||||
|
System.err.println("Computed checksum:");
|
||||||
|
byte[] c = computeHashsum(filename, HashAlgorithm.SHA512);
|
||||||
|
for (byte b: c) {
|
||||||
|
System.err.print(String.format("%02X", b));
|
||||||
|
}
|
||||||
|
System.err.println("");
|
||||||
|
System.err.println("Received checksum:");
|
||||||
|
for (byte b: hash512) {
|
||||||
|
System.err.print(String.format("%02X", b));
|
||||||
|
}
|
||||||
|
System.err.println("");
|
||||||
throw new InternalError();
|
throw new InternalError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,11 +49,7 @@ public class FileList extends Payload {
|
|||||||
throw new InternalError();
|
throw new InternalError();
|
||||||
}
|
}
|
||||||
int size = getPayloadSize(packet);
|
int size = getPayloadSize(packet);
|
||||||
try {
|
fileList = BytesArrayTools.readStringArray(packet, PAYLOAD_START_POSITION, size, "\n");
|
||||||
fileList = (new String(packet, PAYLOAD_START_POSITION, size, "UTF-8")).split("\n");
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new InternalError();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns a byte[] containing Packet with padding.
|
/** Returns a byte[] containing Packet with padding.
|
||||||
@ -71,7 +67,7 @@ public class FileList extends Payload {
|
|||||||
// set payload size
|
// set payload size
|
||||||
setPayloadSize(size - PAYLOAD_START_POSITION, packet);
|
setPayloadSize(size - PAYLOAD_START_POSITION, packet);
|
||||||
// Write fileList
|
// Write fileList
|
||||||
BytesArrayTools.writeStringArray(packet, fileList, PAYLOAD_START_POSITION, "\n");
|
BytesArrayTools.write(packet, fileList, PAYLOAD_START_POSITION, "\n");
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,22 +90,11 @@ public class FilePart extends Payload {
|
|||||||
BytesArrayTools.write(packet, TOTAL_FILESIZE_POSITION, totalSize);
|
BytesArrayTools.write(packet, TOTAL_FILESIZE_POSITION, totalSize);
|
||||||
// write filename’s size to Packet
|
// write filename’s size to Packet
|
||||||
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filename.length());
|
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filename.length());
|
||||||
try {
|
// write filename to Packet
|
||||||
// write filename to Packet
|
BytesArrayTools.write(packet, filename, FILENAME_POSITION);
|
||||||
int i = FILENAME_POSITION;
|
// write partialContent to Packet
|
||||||
for (byte b : filename.getBytes("UTF-8")) {
|
BytesArrayTools.write(packet, partialContent, FILENAME_POSITION + filename.length());
|
||||||
packet[i] = b;
|
return packet;
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
// write partialContent to Packet
|
|
||||||
for (byte b: partialContent) {
|
|
||||||
packet[i] = b;
|
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
return packet;
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new InternalError();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Write from Packet into offset.
|
/** Write from Packet into offset.
|
||||||
@ -150,11 +139,7 @@ public class FilePart extends Payload {
|
|||||||
*/
|
*/
|
||||||
private void setFilename(byte[] packet) throws ProtocolError, SizeError, InternalError {
|
private void setFilename(byte[] packet) throws ProtocolError, SizeError, InternalError {
|
||||||
int filenameSize = getFilenameSize(packet); // this can throw ProtocolError or SizeError
|
int filenameSize = getFilenameSize(packet); // this can throw ProtocolError or SizeError
|
||||||
try {
|
filename = BytesArrayTools.readString(packet, FILENAME_POSITION, filenameSize);
|
||||||
filename = new String(packet, FILENAME_POSITION, filenameSize, "UTF-8");
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new InternalError();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Write from Packet into partialContent.
|
/** Write from Packet into partialContent.
|
||||||
@ -165,11 +150,7 @@ public class FilePart extends Payload {
|
|||||||
private void setPartialContent(byte[] packet) throws ProtocolError, SizeError {
|
private void setPartialContent(byte[] packet) throws ProtocolError, SizeError {
|
||||||
int start = FILENAME_POSITION + getFilenameSize(packet); // this can throw SizeError or ProtocolError
|
int start = FILENAME_POSITION + getFilenameSize(packet); // this can throw SizeError or ProtocolError
|
||||||
int end = OFFSET_POSITION + getPayloadSize(packet); // this can throw SizeError
|
int end = OFFSET_POSITION + getPayloadSize(packet); // this can throw SizeError
|
||||||
try {
|
partialContent = BytesArrayTools.readByteArray(packet, start, end);
|
||||||
partialContent = Arrays.copyOfRange(packet, start, end);
|
|
||||||
} catch (ArrayIndexOutOfBoundsException e) {
|
|
||||||
throw new ProtocolError();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** partialContent getter.
|
/** partialContent getter.
|
||||||
|
@ -60,23 +60,18 @@ public class HashRequest extends Payload {
|
|||||||
}
|
}
|
||||||
/* Read filename */
|
/* Read filename */
|
||||||
int filenameSize = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION);
|
int filenameSize = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION);
|
||||||
try {
|
|
||||||
filename = new String(packet, FILENAME_POSITION, filenameSize, "UTF-8");
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new InternalError();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/* Read filename */
|
||||||
|
filename = BytesArrayTools.readString(packet, FILENAME_POSITION, filenameSize);
|
||||||
|
|
||||||
|
/* Read algo list */
|
||||||
int size = getPayloadSize(packet);
|
int size = getPayloadSize(packet);
|
||||||
try {
|
String[] algoListStr = BytesArrayTools.readStringArray(packet, FILENAME_POSITION + filenameSize, size, "\n");
|
||||||
String[] l = (new String(packet, FILENAME_POSITION + filenameSize, size, "UTF-8")).split("\n");
|
int i = 0;
|
||||||
int i = 0;
|
algoList = new HashAlgorithm[algoListStr.length];
|
||||||
algoList = new HashAlgorithm[l.length];
|
for(String algo : algoListStr) {
|
||||||
for(String algo : l) {
|
algoList[i] = HashAlgorithm.fromName(algo);
|
||||||
algoList[i] = HashAlgorithm.fromName(algo);
|
i++;
|
||||||
i++;
|
|
||||||
}
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new InternalError();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,63 +84,24 @@ public class HashRequest extends Payload {
|
|||||||
protected byte[] toPacket() throws InternalError {
|
protected byte[] toPacket() throws InternalError {
|
||||||
// compute size
|
// compute size
|
||||||
int filenameSize = filename.length();
|
int filenameSize = filename.length();
|
||||||
int size = FILENAME_POSITION + filenameSize;
|
String[] algoListStr = new String[algoList.length];
|
||||||
|
int i = 0;
|
||||||
for (HashAlgorithm h : algoList) {
|
for (HashAlgorithm h : algoList) {
|
||||||
if (h == null) {
|
algoListStr[i] = h.getName();
|
||||||
continue;
|
i++;
|
||||||
}
|
|
||||||
size += h.getName().length();
|
|
||||||
size += 1; // size for '\n'
|
|
||||||
}
|
}
|
||||||
size -=1; // remove trailing '\n'
|
int size = FILENAME_POSITION + filenameSize + BytesArrayTools.computeStringArraySize(algoListStr, "\n");
|
||||||
byte[] packet = new byte[size + 1]; // java initialize all to zero
|
byte[] packet = new byte[size + 1]; // java initialize all to zero
|
||||||
|
|
||||||
// set request/response code
|
// set request/response code
|
||||||
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
||||||
|
|
||||||
// set Payload size
|
// set Payload size
|
||||||
setPayloadSize(size - FILENAME_SIZE_POSITION, packet);
|
setPayloadSize(size - FILENAME_SIZE_POSITION, packet);
|
||||||
|
// write filename size
|
||||||
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filenameSize);
|
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filenameSize);
|
||||||
|
// write filename
|
||||||
// Write filename
|
BytesArrayTools.write(packet, filename, FILENAME_POSITION);
|
||||||
int bCount = FILENAME_POSITION;
|
// write algo list
|
||||||
try {
|
BytesArrayTools.write(packet, algoListStr, FILENAME_POSITION + filename.length(), "\n");
|
||||||
byte[] sb = filename.getBytes("UTF-8");
|
|
||||||
for(byte b : sb) {
|
|
||||||
packet[bCount] = b;
|
|
||||||
bCount += 1;
|
|
||||||
}
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new InternalError();
|
|
||||||
}
|
|
||||||
boolean firstIter = true;
|
|
||||||
for(HashAlgorithm h : algoList) {
|
|
||||||
if (h == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
String s = h.getName();
|
|
||||||
if (!firstIter) {
|
|
||||||
firstIter = false;
|
|
||||||
try {
|
|
||||||
packet[bCount] = "\n".getBytes("UTF-8")[0]; // separator
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new InternalError();
|
|
||||||
}
|
|
||||||
bCount += 1;
|
|
||||||
}
|
|
||||||
// Copy algoname
|
|
||||||
try {
|
|
||||||
byte[] sb = s.getBytes("UTF-8");
|
|
||||||
for(byte b : sb) {
|
|
||||||
packet[bCount] = b;
|
|
||||||
bCount += 1;
|
|
||||||
}
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new InternalError();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,33 +63,22 @@ public class HashResponse extends Payload {
|
|||||||
}
|
}
|
||||||
/* Read filename */
|
/* Read filename */
|
||||||
int filenameSize = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION);
|
int filenameSize = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION);
|
||||||
try {
|
filename = BytesArrayTools.readString(packet, FILENAME_POSITION, filenameSize);
|
||||||
filename = new String(packet, FILENAME_POSITION, filenameSize, "UTF-8");
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new InternalError();
|
|
||||||
}
|
|
||||||
|
|
||||||
int size = getPayloadSize(packet);
|
int size = getPayloadSize(packet);
|
||||||
int start = FILENAME_POSITION + filenameSize;
|
int start = FILENAME_POSITION + filenameSize;
|
||||||
do {
|
do {
|
||||||
int algoNameSize = BytesArrayTools.readInt(packet, start);
|
int algoNameSize = BytesArrayTools.readInt(packet, start);
|
||||||
start +=4;
|
start +=4;
|
||||||
try {
|
String algoName = BytesArrayTools.readString(packet, start, algoNameSize);
|
||||||
String algoName = new String(packet, start, algoNameSize, "UTF-8");
|
start += algoNameSize;
|
||||||
start += algoNameSize;
|
int hashSize = BytesArrayTools.readInt(packet, start);
|
||||||
int hashSize = BytesArrayTools.readInt(packet, start);
|
start += 4;
|
||||||
start += 4;
|
if (hashSize != 0) {
|
||||||
if (hashSize != 0) {
|
byte[] b = BytesArrayTools.readByteArray(packet, start, start+hashSize);
|
||||||
byte[] b = new byte[hashSize];
|
hashes.put(HashAlgorithm.fromName(algoName), b);
|
||||||
for (int i=0;i<hashSize;i++) {
|
|
||||||
b[i] = packet[start+i];
|
|
||||||
}
|
|
||||||
hashes.put(HashAlgorithm.fromName(algoName), b);
|
|
||||||
}
|
|
||||||
start += hashSize;
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new InternalError();
|
|
||||||
}
|
}
|
||||||
|
start += hashSize;
|
||||||
} while (start < size);
|
} while (start < size);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -114,49 +103,28 @@ public class HashResponse extends Payload {
|
|||||||
|
|
||||||
// set request/response code
|
// set request/response code
|
||||||
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
||||||
|
|
||||||
// set Payload size
|
// set Payload size
|
||||||
setPayloadSize(size - FILENAME_SIZE_POSITION, packet);
|
setPayloadSize(size - FILENAME_SIZE_POSITION, packet);
|
||||||
|
// write filename size
|
||||||
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filenameSize);
|
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filenameSize);
|
||||||
|
// write filename
|
||||||
// Write filename
|
BytesArrayTools.write(packet, filename, FILENAME_POSITION);
|
||||||
int bCount = FILENAME_POSITION;
|
int bCount = FILENAME_POSITION + filename.length();
|
||||||
try {
|
|
||||||
byte[] sb = filename.getBytes("UTF-8");
|
|
||||||
for(byte b : sb) {
|
|
||||||
packet[bCount] = b;
|
|
||||||
bCount += 1;
|
|
||||||
}
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new InternalError();
|
|
||||||
}
|
|
||||||
for(HashAlgorithm h : hashes.keySet()) {
|
for(HashAlgorithm h : hashes.keySet()) {
|
||||||
String s = h.getName();
|
String s = h.getName();
|
||||||
BytesArrayTools.write(packet, bCount, (int)s.length());
|
BytesArrayTools.write(packet, bCount, (int)s.length());
|
||||||
bCount += 4;
|
bCount += 4;
|
||||||
// Copy algoname
|
// write algoname
|
||||||
try {
|
BytesArrayTools.write(packet, s, bCount);
|
||||||
byte[] sb = s.getBytes("UTF-8");
|
bCount += s.length();
|
||||||
for(byte b : sb) {
|
// write hash size
|
||||||
packet[bCount] = b;
|
|
||||||
bCount += 1;
|
|
||||||
}
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new InternalError();
|
|
||||||
}
|
|
||||||
byte[] hashb = hashes.get(HashAlgorithm.fromName(s));
|
byte[] hashb = hashes.get(HashAlgorithm.fromName(s));
|
||||||
if (hashb.length == 0) {
|
BytesArrayTools.write(packet, bCount, (int)hashb.length);
|
||||||
BytesArrayTools.write(packet, bCount, (int)0);
|
bCount += 4;
|
||||||
bCount += 4;
|
if (hashb.length != 0) {
|
||||||
} else {
|
// write hash
|
||||||
BytesArrayTools.write(packet, bCount, (int)hashb.length);
|
BytesArrayTools.write(packet, hashb, bCount);
|
||||||
bCount +=4;
|
bCount += hashb.length;
|
||||||
// copy hash
|
|
||||||
for(byte b : hashb) {
|
|
||||||
packet[bCount] = b;
|
|
||||||
bCount += 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return packet;
|
return packet;
|
||||||
|
@ -64,11 +64,7 @@ public class LoadRequest extends Payload {
|
|||||||
|
|
||||||
/* Read filename */
|
/* Read filename */
|
||||||
int size = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION);
|
int size = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION);
|
||||||
try {
|
filename = BytesArrayTools.readString(packet, FILENAME_POSITION, size);
|
||||||
filename = new String(packet, FILENAME_POSITION, size, "UTF-8");
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new InternalError();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns a byte[] containing Packet with padding.
|
/** Returns a byte[] containing Packet with padding.
|
||||||
@ -82,33 +78,18 @@ public class LoadRequest extends Payload {
|
|||||||
int filenameSize = filename.length();
|
int filenameSize = filename.length();
|
||||||
int size = FILENAME_POSITION + filenameSize;
|
int size = FILENAME_POSITION + filenameSize;
|
||||||
byte[] packet = new byte[size + 1]; // java initialize all to zero
|
byte[] packet = new byte[size + 1]; // java initialize all to zero
|
||||||
|
|
||||||
// set request/response code
|
// set request/response code
|
||||||
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
||||||
|
|
||||||
// set Payload size
|
// set Payload size
|
||||||
setPayloadSize(size - OFFSET_POSITION, packet);
|
setPayloadSize(size - OFFSET_POSITION, packet);
|
||||||
|
|
||||||
// Write offset
|
// Write offset
|
||||||
BytesArrayTools.write(packet, OFFSET_POSITION, (long)offset);
|
BytesArrayTools.write(packet, OFFSET_POSITION, (long)offset);
|
||||||
|
|
||||||
// Write maxSizePartialContent
|
// Write maxSizePartialContent
|
||||||
BytesArrayTools.write(packet, MAX_SIZE_PARTIAL_CONTENT_POSITION, (long)maxSizePartialContent);
|
BytesArrayTools.write(packet, MAX_SIZE_PARTIAL_CONTENT_POSITION, (long)maxSizePartialContent);
|
||||||
|
|
||||||
// Write filenameSize
|
// Write filenameSize
|
||||||
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, (int)filenameSize);
|
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, (int)filenameSize);
|
||||||
|
|
||||||
// Write filename
|
// Write filename
|
||||||
int bCount = FILENAME_POSITION;
|
BytesArrayTools.write(packet, filename, FILENAME_POSITION);
|
||||||
try {
|
|
||||||
byte[] sb = filename.getBytes("UTF-8");
|
|
||||||
for(byte b : sb) {
|
|
||||||
packet[bCount] = b;
|
|
||||||
bCount += 1;
|
|
||||||
}
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new InternalError();
|
|
||||||
}
|
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package tools;
|
package tools;
|
||||||
import exception.SizeError;
|
import exception.SizeError;
|
||||||
|
import exception.ProtocolError;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
|
||||||
/** Helper to manipulate byte[].
|
/** Helper to manipulate byte[].
|
||||||
@ -127,7 +129,7 @@ public class BytesArrayTools {
|
|||||||
* @param separator separator to use
|
* @param separator separator to use
|
||||||
* @throws InternalError
|
* @throws InternalError
|
||||||
*/
|
*/
|
||||||
public static void writeStringArray(byte[] byteArray, String[] strArray, int start, String separator) throws InternalError {
|
public static void write(byte[] byteArray, String[] strArray, int start, String separator) throws InternalError {
|
||||||
int bCount = start;
|
int bCount = start;
|
||||||
try {
|
try {
|
||||||
byte[] sepb = separator.getBytes("UTF-8");
|
byte[] sepb = separator.getBytes("UTF-8");
|
||||||
@ -151,6 +153,27 @@ public class BytesArrayTools {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Read string array from byte array starting at start with size parameter.
|
||||||
|
* @param byteArray Array to read
|
||||||
|
* @param start Start position
|
||||||
|
* @param size Size to read
|
||||||
|
* @param separator Separator used
|
||||||
|
* @return String array output
|
||||||
|
* @throws InternalError
|
||||||
|
*/
|
||||||
|
public static String[] readStringArray(byte[] byteArray, int start, int size, String separator) throws InternalError {
|
||||||
|
try {
|
||||||
|
return (new String(byteArray, start, size, "UTF-8")).split(separator);
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
throw new InternalError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Compute size of string array once converted to byte array with separator
|
||||||
|
* @param strArray String Array
|
||||||
|
* @param separator Separator used
|
||||||
|
* @return size
|
||||||
|
*/
|
||||||
public static int computeStringArraySize(String[] strArray, String separator) {
|
public static int computeStringArraySize(String[] strArray, String separator) {
|
||||||
int size = 0;
|
int size = 0;
|
||||||
for (String s: strArray) {
|
for (String s: strArray) {
|
||||||
@ -161,4 +184,66 @@ public class BytesArrayTools {
|
|||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Write a string to a byte array.
|
||||||
|
* @param array Byte Array to write
|
||||||
|
* @param str String to read
|
||||||
|
* @param start Start position in byte array to write
|
||||||
|
* @throws InternalError
|
||||||
|
*/
|
||||||
|
public static void write(byte[] array, String str, int start) throws InternalError {
|
||||||
|
int bCount = start;
|
||||||
|
try {
|
||||||
|
byte[] sb = str.getBytes("UTF-8");
|
||||||
|
for(byte b : sb) {
|
||||||
|
array[bCount] = b;
|
||||||
|
bCount += 1;
|
||||||
|
}
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
throw new InternalError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read string from byte array
|
||||||
|
* @param array Byte array to read
|
||||||
|
* @param start start position in byte array
|
||||||
|
* @param size size to read
|
||||||
|
* @return String read
|
||||||
|
* @throws InternalError
|
||||||
|
*/
|
||||||
|
public static String readString(byte[] array, int start, int size) throws InternalError {
|
||||||
|
try {
|
||||||
|
return new String(array, start, size, "UTF-8");
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
throw new InternalError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Write byte Array to byte Array.
|
||||||
|
* @param dst Destination byte Array
|
||||||
|
* @param src Source byte Array
|
||||||
|
* @param start Start in dest array
|
||||||
|
*/
|
||||||
|
public static void write(byte[] dst, byte[] src, int start) {
|
||||||
|
int i = start;
|
||||||
|
for (byte b: src) {
|
||||||
|
dst[i] = b;
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Read byte Array to byte Array
|
||||||
|
* @param src Source byte Array
|
||||||
|
* @param start Start position in source
|
||||||
|
* @param end End position in source
|
||||||
|
* @return Resulting byte array
|
||||||
|
* @throws ProtocolError
|
||||||
|
*/
|
||||||
|
public static byte[] readByteArray(byte[] src, int start, int end) throws ProtocolError {
|
||||||
|
try {
|
||||||
|
return Arrays.copyOfRange(src, start, end);
|
||||||
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
|
throw new ProtocolError();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user