use of jodd.io.FastByteArrayOutputStream in project jodd by oblac.
the class AdaptiveFileUpload method processStream.
@Override
protected void processStream() throws IOException {
if (!checkUpload()) {
return;
}
size = 0;
if (memoryThreshold > 0) {
FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream(memoryThreshold + 1);
int written = input.copyMax(fbaos, memoryThreshold + 1);
data = fbaos.toByteArray();
if (written <= memoryThreshold) {
size = data.length;
valid = true;
return;
}
}
tempFile = FileUtil.createTempFile(JoddCore.tempFilePrefix, TMP_FILE_SUFFIX, uploadPath);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile));
if (data != null) {
size = data.length;
out.write(data);
// not needed anymore
data = null;
}
boolean deleteTempFile = false;
try {
if (maxFileSize == -1) {
size += input.copyAll(out);
} else {
// one more byte to detect larger files
size += input.copyMax(out, maxFileSize - size + 1);
if (size > maxFileSize) {
deleteTempFile = true;
fileTooBig = true;
valid = false;
if (breakOnError) {
throw new IOException("File upload (" + header.getFileName() + ") too big, > " + maxFileSize);
}
input.skipToBoundary();
return;
}
}
valid = true;
} finally {
StreamUtil.close(out);
if (deleteTempFile) {
tempFile.delete();
tempFile = null;
}
}
}
use of jodd.io.FastByteArrayOutputStream in project jodd by oblac.
the class MemoryFileUpload method processStream.
/**
* Reads data from input stream into byte array and stores file size.
*/
@Override
public void processStream() throws IOException {
FastByteArrayOutputStream out = new FastByteArrayOutputStream();
size = 0;
if (maxFileSize == -1) {
size += input.copyAll(out);
} else {
// one more byte to detect larger files
size += input.copyMax(out, maxFileSize + 1);
if (size > maxFileSize) {
fileTooBig = true;
valid = false;
input.skipToBoundary();
return;
}
}
data = out.toByteArray();
size = data.length;
valid = true;
}
use of jodd.io.FastByteArrayOutputStream in project jodd by oblac.
the class MultipartRequestInputStream method readDataHeaderString.
protected String readDataHeaderString(String encoding) throws IOException {
FastByteArrayOutputStream data = new FastByteArrayOutputStream();
byte b;
while (true) {
// end marker byte on offset +0 and +2 must be 13
if ((b = readByte()) != '\r') {
data.write(b);
continue;
}
mark(4);
skipBytes(1);
int i = read();
if (i == -1) {
// reached end of stream
return null;
}
if (i == '\r') {
reset();
break;
}
reset();
data.write(b);
}
skipBytes(3);
if (encoding != null) {
return data.toString(encoding);
} else {
return data.toString();
}
}
use of jodd.io.FastByteArrayOutputStream in project jodd by oblac.
the class ObjectUtil method objectToByteArray.
// ---------------------------------------------------------------- serialization to byte array
/**
* Serialize an object to byte array.
*/
public static byte[] objectToByteArray(Object obj) throws IOException {
FastByteArrayOutputStream bos = new FastByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
} finally {
StreamUtil.close(oos);
}
return bos.toByteArray();
}
use of jodd.io.FastByteArrayOutputStream in project jodd by oblac.
the class EmailAttachment method toByteArray.
// ---------------------------------------------------------------- content methods
/**
* Returns byte content of the attachment.
*/
public byte[] toByteArray() {
FastByteArrayOutputStream out = size != -1 ? new FastByteArrayOutputStream(size) : new FastByteArrayOutputStream();
writeToStream(out);
return out.toByteArray();
}
Aggregations