/** * write to an existing file in a webdav server using the current domain * * @param path The path relative to the domain for the file * @param buffer The contents of the file * @param last true if this is the last block of data * @return The total number of bytes stored or null if method failed */ public long writeToFile(String path, byte[] buffer, boolean last) { long result = -1L; if (_tempFile == null) _tempFile = Util.createCache(); try { if (buffer != null && buffer.length > 0) { FileOutputStream fOut = new FileOutputStream(_tempFile.getCanonicalPath(), true); fOut.write(buffer); fOut.close(); } if (last) { if (_isLocal) { File tmpFile = new File(_rootFile.getCanonicalPath() + File.separatorChar + path); if (_tempFile.renameTo(tmpFile)) { _tempFile = null; return tmpFile.length(); } } else { WebdavResource webdavRes = Util.initWebdavResource(_domain); if (webdavRes == null) { _tempFile.delete(); _tempFile = null; return result; } result = Util.putFile(webdavRes, _domain, path, _tempFile); } } else { return _tempFile.length(); } } catch (Exception ex) { ex.printStackTrace(); } return result; }
/** * write zipped data to an existing file in a webdav server to be stored in the original form * * @param path The path relative to the domain for the file * @param buffer The zipped contents of the block of data to be written * @param last true if this is the last block of data * @return The total number of bytes stored or null if method failed */ public long writeToFileZipped(String path, byte[] buffer, boolean last) { long result = -1L; if (_tempFile == null) _tempFile = Util.createCache(); try { if (buffer != null && buffer.length > 0) { ByteArrayInputStream bIn = new ByteArrayInputStream(buffer); GZIPInputStream zIn = new GZIPInputStream(bIn); int bufSize = 0; byte[] tmpBuf = new byte[buffer.length]; FileOutputStream fOut = new FileOutputStream(_tempFile.getCanonicalPath(), true); do { bufSize = zIn.read(tmpBuf); if (bufSize != -1) fOut.write(tmpBuf, 0, bufSize); } while (bufSize == tmpBuf.length); zIn.close(); bIn.close(); fOut.close(); } if (last) { if (_isLocal) { File tmpFile = new File(_rootFile.getCanonicalPath() + File.separatorChar + path); if (_tempFile.renameTo(tmpFile)) { _tempFile = null; return tmpFile.length(); } } else { WebdavResource webdavRes = Util.initWebdavResource(_domain); if (webdavRes == null) return result; result = Util.putFile(webdavRes, _domain, path, _tempFile); } } else { return _tempFile.length(); } } catch (Exception ex) { ex.printStackTrace(); } _tempFile.delete(); _tempFile = null; return result; }
/** * write zipped data to a group of existing files in a webdav server to be stored in the original * form using the current domain * * @param paths The paths relative to the domain for the files to be written * @param block The zipped contents of the block of data to be written and the description for * each file * @param last true if this is the last block of data * @return The total number of bytes stored or null if method failed */ public long writeToStreamZipped(String[] paths, MultiFileBlock block, boolean last) { long result = -1L; if (_tempFile == null) _tempFile = Util.createCache(); try { byte[] buffer = block.getBlockData(); ByteArrayInputStream bIn = new ByteArrayInputStream(buffer); GZIPInputStream zIn = new GZIPInputStream(bIn); int bufSize = 0; byte[] tmpBuf = new byte[buffer.length]; FileOutputStream fOut = new FileOutputStream(_tempFile.getCanonicalPath(), true); BlockFileDescriptor des = block.getBlockFileDescriptor(0); int i = 1; do { bufSize = zIn.read(tmpBuf); if (bufSize != -1) { long partSize = des.getFileSize() - _tempFile.length(); if (partSize < bufSize) { fOut.write(tmpBuf, 0, (int) partSize); fOut.close(); if (_isLocal) { File partRootFile = Util.initLocalResource(des.getFileDomain()); File tmpFile = new File( partRootFile.getCanonicalPath() + File.separatorChar + des.getFilePath()); if (_tempFile.renameTo(tmpFile)) { result = tmpFile.length(); } } else { WebdavResource webdavRes = Util.initWebdavResource(_domain); if (webdavRes == null) { _tempFile.delete(); _tempFile = null; return result; } result = Util.putFile(webdavRes, des.getFileDomain(), des.getFilePath(), _tempFile); } _tempFile = Util.createCache(); des = block.getBlockFileDescriptor(i); i++; fOut = new FileOutputStream(_tempFile.getCanonicalPath(), true); fOut.write(tmpBuf, (int) partSize, (int) (bufSize - partSize)); } else { fOut.write(tmpBuf, 0, bufSize); } fOut.flush(); } } while (bufSize == tmpBuf.length); fOut.close(); zIn.close(); bIn.close(); if (last) { if (_isLocal) { File partRootFile = Util.initLocalResource(des.getFileDomain()); File tmpFile = new File(partRootFile.getCanonicalPath() + File.separatorChar + des.getFilePath()); if (_tempFile.renameTo(tmpFile)) { _tempFile = null; return tmpFile.length(); } } else { WebdavResource webdavRes = Util.initWebdavResource(_domain); if (webdavRes == null) { _tempFile.delete(); _tempFile = null; return result; } result = Util.putFile(webdavRes, des.getFileDomain(), des.getFilePath(), _tempFile); } } else { return _tempFile.length(); } } catch (Exception ex) { ex.printStackTrace(); } return result; }