public void write(byte[] b, int off, int len) throws IOException { if (fileOutputStream != null) { fileOutputStream.write(b, off, len); } else if (len > (chunks.length-chunkIndex)*chunkSize - chunkOffset) { // The buffer will overflow. Switch to a temporary file. fileOutputStream = switchToTempFile(); // Write the new data to the temporary file. fileOutputStream.write(b, off, len); } else { // The data will fit into the buffer. while (len > 0) { byte[] chunk = getCurrentChunk(); // Determine number of bytes that can be copied to the current chunk. int c = Math.min(len, chunkSize-chunkOffset); // Copy data to the chunk. System.arraycopy(b, off, chunk, chunkOffset, c); // Update variables. len -= c; off += c; chunkOffset += c; if (chunkOffset == chunkSize) { chunkIndex++; chunkOffset = 0; } } } }
/** * Take the data from temporary storage and write it to the output stream * @param out output stream output stream * @param msgContext messagecontext * @throws IOException if an exception occurred while writing data */ private void writeMessageFromTempData(OutputStream out, MessageContext msgContext) throws IOException { TemporaryData serialized = (TemporaryData) msgContext.getProperty(NhttpConstants.SERIALIZED_BYTES); try { serialized.writeTo(out); } finally { serialized.release(); } }
/** * Write the stream to a temporary storage and calculate the content length * @param entity HTTPEntity * @param messageFormatter message formatter * @param msgContext current message context * @param format message format * @throws IOException if an exception occurred while writing data */ private void setStreamAsTempData(BasicHttpEntity entity, MessageFormatter messageFormatter, MessageContext msgContext, OMOutputFormat format) throws IOException { TemporaryData serialized = new TemporaryData(256, 4096, "http-nio_", ".dat"); OutputStream out = serialized.getOutputStream(); try { messageFormatter.writeTo(msgContext, format, out, true); } finally { out.close(); } msgContext.setProperty(NhttpConstants.SERIALIZED_BYTES, serialized); entity.setContentLength(serialized.getLength()); }
int c = in.read(getCurrentChunk(), chunkOffset, chunkSize-chunkOffset); if (c == -1) { break; chunkOffset = 0; if (chunkIndex == chunks.length) { FileOutputStream fileOutputStream = switchToTempFile(); IOUtils.copy(in, fileOutputStream); fileOutputStream.close();
public void write(byte[] b, int off, int len) throws IOException { if (fileOutputStream != null) { fileOutputStream.write(b, off, len); } else if (len > (chunks.length-chunkIndex)*chunkSize - chunkOffset) { // The buffer will overflow. Switch to a temporary file. fileOutputStream = switchToTempFile(); // Write the new data to the temporary file. fileOutputStream.write(b, off, len); } else { // The data will fit into the buffer. while (len > 0) { byte[] chunk = getCurrentChunk(); // Determine number of bytes that can be copied to the current chunk. int c = Math.min(len, chunkSize-chunkOffset); // Copy data to the chunk. System.arraycopy(b, off, chunk, chunkOffset, c); // Update variables. len -= c; off += c; chunkOffset += c; if (chunkOffset == chunkSize) { chunkIndex++; chunkOffset = 0; } } } }
int c = in.read(getCurrentChunk(), chunkOffset, chunkSize-chunkOffset); if (c == -1) { break; chunkOffset = 0; if (chunkIndex == chunks.length) { FileOutputStream fileOutputStream = switchToTempFile(); IOUtils.copy(in, fileOutputStream); fileOutputStream.close();