forked from Qortal/qortal
Added DataFileMessage and GetDataFileMessage, used for requesting and sending files between peers.
This commit is contained in:
parent
abfe0a925a
commit
5ac676d201
@ -0,0 +1,44 @@
|
||||
package org.qortal.network.message;
|
||||
|
||||
import org.qortal.storage.DataFile;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class DataFileMessage extends Message {
|
||||
|
||||
private final DataFile dataFile;
|
||||
|
||||
public DataFileMessage(DataFile dataFile) {
|
||||
super(MessageType.DATA_FILE);
|
||||
|
||||
this.dataFile = dataFile;
|
||||
}
|
||||
|
||||
public DataFile getDataFile() {
|
||||
return this.dataFile;
|
||||
}
|
||||
|
||||
public static Message fromByteBuffer(int id, ByteBuffer byteBuffer) throws UnsupportedEncodingException {
|
||||
byte[] bytes = new byte[byteBuffer.remaining()];
|
||||
byteBuffer.get(bytes);
|
||||
DataFile dataFile = new DataFile(bytes);
|
||||
|
||||
return new DataFileMessage(dataFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected byte[] toData() {
|
||||
if (this.dataFile == null)
|
||||
return null;
|
||||
|
||||
return this.dataFile.getBytes();
|
||||
}
|
||||
|
||||
public DataFileMessage cloneWithNewId(int newId) {
|
||||
DataFileMessage clone = new DataFileMessage(this.dataFile);
|
||||
clone.setId(newId);
|
||||
return clone;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package org.qortal.network.message;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class GetDataFileMessage extends Message {
|
||||
|
||||
private static final int DIGEST_LENGTH = 32;
|
||||
|
||||
private final byte[] digest;
|
||||
|
||||
public GetDataFileMessage(byte[] digest) {
|
||||
this(-1, digest);
|
||||
}
|
||||
|
||||
private GetDataFileMessage(int id, byte[] digest) {
|
||||
super(id, MessageType.GET_DATA_FILE);
|
||||
|
||||
this.digest = digest;
|
||||
}
|
||||
|
||||
public byte[] getDigest() {
|
||||
return this.digest;
|
||||
}
|
||||
|
||||
public static Message fromByteBuffer(int id, ByteBuffer bytes) throws UnsupportedEncodingException {
|
||||
if (bytes.remaining() != DIGEST_LENGTH)
|
||||
return null;
|
||||
|
||||
byte[] digest = new byte[DIGEST_LENGTH];
|
||||
|
||||
bytes.get(digest);
|
||||
|
||||
return new GetDataFileMessage(id, digest);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected byte[] toData() {
|
||||
try {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
|
||||
bytes.write(this.digest);
|
||||
|
||||
return bytes.toByteArray();
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user