Search in sources :

Example 1 with FastByteArrayOutputStream

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;
        }
    }
}
Also used : FastByteArrayOutputStream(jodd.io.FastByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with FastByteArrayOutputStream

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;
}
Also used : FastByteArrayOutputStream(jodd.io.FastByteArrayOutputStream)

Example 3 with FastByteArrayOutputStream

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();
    }
}
Also used : FastByteArrayOutputStream(jodd.io.FastByteArrayOutputStream)

Example 4 with FastByteArrayOutputStream

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();
}
Also used : FastByteArrayOutputStream(jodd.io.FastByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 5 with FastByteArrayOutputStream

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();
}
Also used : FastByteArrayOutputStream(jodd.io.FastByteArrayOutputStream)

Aggregations

FastByteArrayOutputStream (jodd.io.FastByteArrayOutputStream)10 IOException (java.io.IOException)2 ObjectOutputStream (java.io.ObjectOutputStream)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 PrintStream (java.io.PrintStream)1 Serializable (java.io.Serializable)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 Multipart (javax.mail.Multipart)1 Part (javax.mail.Part)1 MimeMessage (javax.mail.internet.MimeMessage)1 MimePart (javax.mail.internet.MimePart)1 InvokeProxetta (jodd.proxetta.impl.InvokeProxetta)1 Test (org.junit.Test)1