long endOfTime,
K contentId, PutContentProvider content,
final long startTime, final long timeoutMsecs)
throws IOException, ExecutionException, InterruptedException
{
JdkHttpClientPathBuilder path = _server.rootPath();
path = _pathFinder.appendStoreEntryPath(path);
path = _keyConverter.appendToPath(path, contentId);
// Is compression known?
Compression comp = content.getExistingCompression();
if (comp != null) { // if so, must be indicated
path = path.addCompression(comp, content.uncompressedLength());
}
if (params != null) {
path = params.appendToPath(path, contentId);
}
// Ok; and then figure out most optimal way for getting content:
OutputStream out = null;
HttpURLConnection conn;
int hash = content.getContentHash();
try {
ByteContainer bc = content.contentAsBytes();
if (bc != null) { // most efficient, yay
if (hash == HashConstants.NO_CHECKSUM) {
hash = _keyConverter.contentHashFor(bc);
content.setContentHash(hash);
}
path = addChecksum(path, hash);
URL url = path.asURL();
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setFixedLengthStreamingMode(bc.byteLength());
conn = initRequest("PUT", conn, path, timeoutMsecs);
out = conn.getOutputStream();
bc.writeBytes(out);
} else {
InputStream in; // closed in copy()
File f = content.contentAsFile();
// !!! TODO: add wrapper for calculating hash sum, if not yet calculated
if (f != null) {
in = new FileInputStream(f);
} else {
in = content.contentAsStream();
}
if (hash != HashConstants.NO_CHECKSUM) {
path = addChecksum(path, hash);
}
URL url = path.asURL();
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setChunkedStreamingMode(CHUNK_SIZE);
conn = initRequest("PUT", conn, path, timeoutMsecs);
out = conn.getOutputStream();