org.bdware.doip.sbyterepo.LargeFileDO Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doip-audit-tool Show documentation
Show all versions of doip-audit-tool Show documentation
doip audit tool developed by bdware
The newest version!
package org.bdware.doip.sbyterepo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.audit.client.AuditDoipClient;
import org.bdware.doip.codec.digitalObject.DigitalObject;
import org.bdware.doip.codec.digitalObject.DoType;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
import org.bdware.doip.codec.operations.BasicOperations;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
public class LargeFileDO extends DigitalObject {
static Logger LOGGER = LogManager.getLogger(LargeFileDO.class);
static DoipMessageFactory factory = new DoipMessageFactory();
static final int buffSize = 1024 * 1024;
public LargeFileDO(String id) {
super(id, DoType.DO);
}
public String create(AuditDoipClient client) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(this.id, BasicOperations.Create.getName());
builder.setBody("stub".getBytes(StandardCharsets.UTF_8));
DoipMessage result = client.sendMessageSync(builder.create());
this.id = result.header.parameters.id;
return result.header.parameters.id;
}
public void updateFromInputStreamSync(AuditDoipClient client, InputStream inputStream) throws IOException {
long writePos = 0;
byte[] buff = new byte[buffSize];
for (int len; (len = inputStream.read(buff)) > 0; ) {
update(client, buff, writePos, len);
LOGGER.info("update:" + writePos);
writePos += len;
}
return;
}
public int update(AuditDoipClient client, byte[] buff, long offset, int count) {
if (buff.length < count)
throw new IllegalArgumentException("insuccficient buff size");
byte[] sendBuff = new byte[count];
client.setTimeout(10 * 1000L);
System.arraycopy(buff, 0, sendBuff, 0, count);
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(this.id, BasicOperations.Update.getName());
builder.addAttributes("offset", offset + "");
builder.addAttributes("count", count + "");
builder.setBody(sendBuff);
DoipMessage result = client.sendMessageSync(builder.create());
return result.header.parameters.attributes.get("updatedSize").getAsInt();
}
public byte[] retrieve(AuditDoipClient client, long offset, int count) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(this.id, BasicOperations.Retrieve.getName());
builder.addAttributes("offset", offset + "");
builder.addAttributes("count", count + "");
DoipMessage msg = builder.create();
DoipMessage result = client.sendMessageSync(msg);
return result.body.encodedData;
}
public long size(AuditDoipClient client) {
try {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
builder.createRequest(this.id, BasicOperations.Hello.getName());
DoipMessage msg = client.sendMessageSync(builder.create());
long ret = msg.header.parameters.attributes.get("size").getAsLong();
return ret;
} catch (Exception e) {
e.printStackTrace();
}
return -1L;
}
public void retrieveToOutputStreamSync(AuditDoipClient client, OutputStream outputStream) {
long size = size(client);
long write = 0;
for (long i = 0; i < size; ) {
int len = buffSize;
if (i + buffSize >= size) {
len = (int) (size - i);
}
try {
byte[] retrieved = retrieve(client, i, len);
outputStream.write(retrieved);
write += retrieved.length;
} catch (IOException e) {
e.printStackTrace();
}
i += len;
assert write == i;
}
return;
}
}