Include the data length in DataFileMessage, which is more similar to the approach used by ArbitraryDataMessage. These two message types are very similar, except arbitrary code currently has a requirement of one piece of data per signature, whereas DataFile code is independent and can support multiple files per transaction. Maybe the two can be combined at some point, but for now I'll keep them separate.

This commit is contained in:
CalDescent 2021-06-20 19:49:10 +01:00
parent c2d0c63db0
commit 16dc5b5327

View File

@ -1,7 +1,10 @@
package org.qortal.network.message;
import com.google.common.primitives.Ints;
import org.qortal.storage.DataFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
@ -26,19 +29,40 @@ public class DataFileMessage extends Message {
}
public static Message fromByteBuffer(int id, ByteBuffer byteBuffer) throws UnsupportedEncodingException {
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
DataFile dataFile = new DataFile(bytes);
int dataLength = byteBuffer.getInt();
if (byteBuffer.remaining() != dataLength)
return null;
byte[] data = new byte[dataLength];
byteBuffer.get(data);
DataFile dataFile = new DataFile(data);
return new DataFileMessage(id, dataFile);
}
@Override
protected byte[] toData() {
if (this.dataFile == null)
if (this.dataFile == null) {
return null;
}
return this.dataFile.getBytes();
byte[] data = this.dataFile.getBytes();
if (data == null) {
return null;
}
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bytes.write(Ints.toByteArray(data.length));
bytes.write(data);
return bytes.toByteArray();
} catch (IOException e) {
return null;
}
}
public DataFileMessage cloneWithNewId(int newId) {