List of usage examples for java.nio.channels WritableByteChannel close
public void close() throws IOException;
From source file:com.alibaba.jstorm.daemon.nimbus.ServiceHandler.java
@Override public void finishFileUpload(String location) throws TException { TimeCacheMap<Object, Object> uploaders = data.getUploaders(); Object obj = uploaders.get(location); if (obj == null) { throw new TException("File for that location does not exist (or timed out)"); }//from w w w. j av a 2 s . co m try { if (obj instanceof WritableByteChannel) { WritableByteChannel channel = (WritableByteChannel) obj; channel.close(); uploaders.remove(location); LOG.info("Finished uploading file from client: " + location); } else { throw new TException("Object isn't WritableByteChannel for " + location); } } catch (IOException e) { LOG.error(" WritableByteChannel close failed when finishFileUpload " + location); } }
From source file:edu.usc.pgroup.floe.client.FloeClient.java
/** * Download the file from the Coordinator's scratch folder. * Note: Filename should not be an absolute path or contain ".." * * @param fileName name of the file to be downloaded from coordinator's * scratch.//from w w w .j a v a 2 s.co m * @param outFileName (local) output file name. */ public final void downloadFileSync(final String fileName, final String outFileName) { if (!Utils.checkValidFileName(fileName)) { throw new IllegalArgumentException( "Filename is valid. Should not" + " contain .. and should not be an absolute path."); } int rfid = 0; WritableByteChannel outChannel = null; try { rfid = getClient().beginFileDownload(fileName); File outFile = new File(outFileName); File parent = outFile.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } outChannel = Channels.newChannel(new FileOutputStream(outFileName)); } catch (TException e) { LOGGER.error(e.getMessage()); throw new RuntimeException(e); } catch (FileNotFoundException e) { LOGGER.error(e.getMessage()); throw new RuntimeException(e); } int written; try { do { ByteBuffer buffer = null; buffer = getClient().downloadChunk(rfid); written = outChannel.write(buffer); LOGGER.info(String.valueOf(written)); } while (written != 0); } catch (TException e) { LOGGER.error(e.getMessage()); throw new RuntimeException(e); } catch (IOException e) { LOGGER.error(e.getMessage()); throw new RuntimeException(e); } finally { if (outChannel != null) { try { outChannel.close(); } catch (IOException e) { LOGGER.error(e.getMessage()); throw new RuntimeException(e); } } } }
From source file:edu.harvard.iq.dataverse.dataaccess.DataFileIO.java
public void copyPath(Path fileSystemPath) throws IOException { long newFileSize = -1; // if this is a local fileystem file, we'll use a // quick Files.copy method: if (isLocalFile()) { Path outputPath = null;//from ww w . j ava 2s . c o m try { outputPath = getFileSystemPath(); } catch (IOException ex) { outputPath = null; } if (outputPath != null) { Files.copy(fileSystemPath, outputPath, StandardCopyOption.REPLACE_EXISTING); newFileSize = outputPath.toFile().length(); } } else { // otherwise we'll open a writable byte channel and // copy the source file bytes using Channel.transferTo(): WritableByteChannel writeChannel = null; FileChannel readChannel = null; String failureMsg = null; try { open(DataAccessOption.WRITE_ACCESS); writeChannel = getWriteChannel(); readChannel = new FileInputStream(fileSystemPath.toFile()).getChannel(); long bytesPerIteration = 16 * 1024; // 16K bytes long start = 0; while (start < readChannel.size()) { readChannel.transferTo(start, bytesPerIteration, writeChannel); start += bytesPerIteration; } newFileSize = readChannel.size(); } catch (IOException ioex) { failureMsg = ioex.getMessage(); if (failureMsg == null) { failureMsg = "Unknown exception occured."; } } finally { if (readChannel != null) { try { readChannel.close(); } catch (IOException e) { } } if (writeChannel != null) { try { writeChannel.close(); } catch (IOException e) { } } } if (failureMsg != null) { throw new IOException(failureMsg); } } // if it has worked successfully, we also need to reset the size // of the object. setSize(newFileSize); }
From source file:org.codelabor.system.file.util.UploadUtils.java
/** * ?? .</br> ? ?? FILE_SYSTEM? , ?? DATABASE? byte[] * DTO? .// w ww . j a v a 2s .co m * * @param repositoryType * ? ? * @param inputStream * * @param fileDTO * ? DTO * @throws Exception * */ static public void processFile(RepositoryType repositoryType, InputStream inputStream, FileDTO fileDTO) throws Exception { // prepare io OutputStream outputStream = null; ReadableByteChannel inputChannel = null; WritableByteChannel outputChannel = null; int fileSize = 0; switch (repositoryType) { case FILE_SYSTEM: // prepare repository File repository = new File(fileDTO.getRepositoryPath()); String repositoryPath = fileDTO.getRepositoryPath(); StringBuilder sb = new StringBuilder(); if (logger.isDebugEnabled()) { sb.append("repositoryPath: ").append(repositoryPath); sb.append(", repositoryType: ").append(repositoryType); sb.append(", repository.exists(): ").append(repository.exists()); sb.append(", repository.isDirectory(): ").append(repository.isDirectory()); logger.debug(sb.toString()); } // prepare directory File file = new File(repositoryPath); if (!file.exists()) { try { FileUtils.forceMkdir(file); } catch (IOException e) { StringBuilder sb = new StringBuilder(); sb.append("Cannot make directory: "); sb.append(file.toString()); logger.error(sb.toString()); throw new ServletException(sb.toString()); } } // prepare stream sb.setLength(0); sb.append(fileDTO.getRepositoryPath()); if (!fileDTO.getRepositoryPath().endsWith(File.separator)) { sb.append(File.separator); } sb.append(fileDTO.getUniqueFilename()); String filename = sb.toString(); outputStream = new FileOutputStream(filename); logger.debug("filename: {}", filename); // copy channel inputChannel = Channels.newChannel(inputStream); outputChannel = Channels.newChannel(outputStream); fileSize = ChannelUtils.copy(inputChannel, outputChannel); // set vo if (StringUtils.isEmpty(fileDTO.getContentType())) { fileDTO.setContentType(TikaMimeDetectUtils.getMimeType(filename)); } break; case DATABASE: // prepare steam outputStream = new ByteArrayOutputStream(); // copy channel inputChannel = Channels.newChannel(inputStream); outputChannel = Channels.newChannel(outputStream); fileSize = ChannelUtils.copy(inputChannel, outputChannel); // set vo byte[] bytes = ((ByteArrayOutputStream) outputStream).toByteArray(); fileDTO.setBytes(bytes); fileDTO.setRepositoryPath(null); if (StringUtils.isEmpty(fileDTO.getContentType())) { fileDTO.setContentType(TikaMimeDetectUtils.getMimeType(bytes)); } break; } fileDTO.setFileSize(fileSize); // close io inputChannel.close(); outputChannel.close(); inputStream.close(); outputStream.close(); }
From source file:edu.harvard.iq.dvn.core.web.servlet.FileDownloadServlet.java
public void streamData(FileChannel in, WritableByteChannel out, String varHeader) { long position = 0; long howMany = 32 * 1024; try {/*from ww w .j a va 2s. c o m*/ // If we are streaming a TAB-delimited file, we will need to add the // variable header line: if (varHeader != null) { ByteBuffer varHeaderByteBuffer = ByteBuffer.wrap(varHeader.getBytes()); out.write(varHeaderByteBuffer); } while (position < in.size()) { in.transferTo(position, howMany, out); position += howMany; } in.close(); out.close(); } catch (IOException ex) { // whatever. we don't care at this point. } }
From source file:org.lnicholls.galleon.togo.ToGo.java
public boolean Download(Video video, CancelDownload cancelDownload) { ServerConfiguration serverConfiguration = Server.getServer().getServerConfiguration(); GoBackConfiguration goBackConfiguration = Server.getServer().getGoBackConfiguration(); ArrayList videos = new ArrayList(); GetMethod get = null;/*from www . j a va 2 s .c o m*/ try { URL url = new URL(video.getUrl()); Protocol protocol = new Protocol("https", new TiVoSSLProtocolSocketFactory(), 443); HttpClient client = new HttpClient(); // TODO How to get TiVo address?? client.getHostConfiguration().setHost(url.getHost(), 443, protocol); String password = Tools.decrypt(serverConfiguration.getMediaAccessKey()); if (video.getParentalControls() != null && video.getParentalControls().booleanValue()) { if (serverConfiguration.getPassword() == null) throw new NullPointerException("Parental Controls Password is null"); password = password + Tools.decrypt(serverConfiguration.getPassword()); } Credentials credentials = new UsernamePasswordCredentials("tivo", password); //client.getState().setCredentials("TiVo DVR", url.getHost(), credentials); client.getState().setCredentials(null, url.getHost(), credentials); get = new GetMethod(video.getUrl()); client.executeMethod(get); if (get.getStatusCode() != 200) { log.debug("Status code: " + get.getStatusCode()); return false; } InputStream input = get.getResponseBodyAsStream(); String path = serverConfiguration.getRecordingsPath(); File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } String name = getFilename(video); File file = null; if (goBackConfiguration.isGroupByShow()) { if (video.getSeriesTitle() != null && video.getSeriesTitle().trim().length() > 0) { path = path + File.separator + clean(video.getSeriesTitle()); File filePath = new File(path); if (!filePath.exists()) filePath.mkdirs(); file = new File(path + File.separator + name); } else file = new File(path + File.separator + name); } else { file = new File(path + File.separator + name); } // TODO Handle retransfers /* if (file.exists() && video.getStatus()!=Video.STATUS_DOWNLOADING) { log.debug("duplicate file: "+file); try { List list = VideoManager.findByPath(file.getCanonicalPath()); if (list!=null && list.size()>0) { video.setDownloadSize(file.length()); video.setDownloadTime(0); video.setPath(file.getCanonicalPath()); video.setStatus(Video.STATUS_DELETED); VideoManager.updateVideo(video); return true; } } catch (HibernateException ex) { log.error("Video update failed", ex); } } */ log.info("Downloading: " + name); WritableByteChannel channel = new FileOutputStream(file, false).getChannel(); long total = 0; double diff = 0.0; ByteBuffer buf = ByteBuffer.allocateDirect(1024 * 4); byte[] bytes = new byte[1024 * 4]; int amount = 0; int index = 0; long target = video.getSize(); long start = System.currentTimeMillis(); long last = start; while (amount == 0 && total < target) { while (amount >= 0 && !cancelDownload.cancel()) { if (index == amount) { amount = input.read(bytes); index = 0; total = total + amount; } while (index < amount && buf.hasRemaining()) { buf.put(bytes[index++]); } buf.flip(); int numWritten = channel.write(buf); if (buf.hasRemaining()) { buf.compact(); } else { buf.clear(); } if ((System.currentTimeMillis() - last > 10000) && (total > 0)) { try { video = VideoManager.retrieveVideo(video.getId()); if (video.getStatus() == Video.STATUS_DOWNLOADING) { diff = (System.currentTimeMillis() - start) / 1000.0; if (diff > 0) { video.setDownloadSize(total); video.setDownloadTime((int) diff); VideoManager.updateVideo(video); } } } catch (HibernateException ex) { log.error("Video update failed", ex); } last = System.currentTimeMillis(); } } if (cancelDownload.cancel()) { channel.close(); return false; } } diff = (System.currentTimeMillis() - start) / 1000.0; channel.close(); if (diff != 0) log.info("Download rate=" + (total / 1024) / diff + " KBps"); try { video.setPath(file.getCanonicalPath()); VideoManager.updateVideo(video); } catch (HibernateException ex) { log.error("Video update failed", ex); } } catch (MalformedURLException ex) { Tools.logException(ToGo.class, ex, video.getUrl()); return false; } catch (Exception ex) { Tools.logException(ToGo.class, ex, video.getUrl()); return false; } finally { if (get != null) get.releaseConnection(); } return true; }
From source file:edu.harvard.iq.dataverse.ingest.IngestServiceBean.java
public void addFiles(DatasetVersion version, List<DataFile> newFiles) { if (newFiles != null && newFiles.size() > 0) { // final check for duplicate file names; // we tried to make the file names unique on upload, but then // the user may have edited them on the "add files" page, and // renamed FOOBAR-1.txt back to FOOBAR.txt... checkForDuplicateFileNamesFinal(version, newFiles); Dataset dataset = version.getDataset(); try {//from ww w .j a v a2 s.c o m if (dataset.getFileSystemDirectory() != null && !Files.exists(dataset.getFileSystemDirectory())) { /* Note that "createDirectories()" must be used - not * "createDirectory()", to make sure all the parent * directories that may not yet exist are created as well. */ Files.createDirectories(dataset.getFileSystemDirectory()); } } catch (IOException dirEx) { logger.severe("Failed to create study directory " + dataset.getFileSystemDirectory().toString()); return; // TODO: // Decide how we are communicating failure information back to // the page, and what the page should be doing to communicate // it to the user - if anything. // -- L.A. } if (dataset.getFileSystemDirectory() != null && Files.exists(dataset.getFileSystemDirectory())) { for (DataFile dataFile : newFiles) { String tempFileLocation = getFilesTempDirectory() + "/" + dataFile.getStorageIdentifier(); FileMetadata fileMetadata = dataFile.getFileMetadatas().get(0); String fileName = fileMetadata.getLabel(); // temp dbug line System.out.println("ADDING FILE: " + fileName + "; for dataset: " + dataset.getGlobalId()); // These are all brand new files, so they should all have // one filemetadata total. -- L.A. boolean metadataExtracted = false; if (ingestableAsTabular(dataFile)) { /* * Note that we don't try to ingest the file right away - * instead we mark it as "scheduled for ingest", then at * the end of the save process it will be queued for async. * ingest in the background. In the meantime, the file * will be ingested as a regular, non-tabular file, and * appear as such to the user, until the ingest job is * finished with the Ingest Service. */ dataFile.SetIngestScheduled(); } else if (fileMetadataExtractable(dataFile)) { try { // FITS is the only type supported for metadata // extraction, as of now. -- L.A. 4.0 dataFile.setContentType("application/fits"); metadataExtracted = extractMetadata(tempFileLocation, dataFile, version); } catch (IOException mex) { logger.severe("Caught exception trying to extract indexable metadata from file " + fileName + ", " + mex.getMessage()); } if (metadataExtracted) { logger.fine("Successfully extracted indexable metadata from file " + fileName); } else { logger.fine("Failed to extract indexable metadata from file " + fileName); } } // Try to save the file in its permanent location: String storageId = dataFile.getStorageIdentifier().replaceFirst("^tmp://", ""); Path tempLocationPath = Paths.get(getFilesTempDirectory() + "/" + storageId); WritableByteChannel writeChannel = null; FileChannel readChannel = null; try { DataFileIO dataAccess = dataFile.getAccessObject(); /* This commented-out code demonstrates how to copy bytes from a local InputStream (or a readChannel) into the writable byte channel of a Dataverse DataAccessIO object: */ /* dataAccess.open(DataAccessOption.WRITE_ACCESS); writeChannel = dataAccess.getWriteChannel(); readChannel = new FileInputStream(tempLocationPath.toFile()).getChannel(); long bytesPerIteration = 16 * 1024; // 16K bytes long start = 0; while ( start < readChannel.size() ) { readChannel.transferTo(start, bytesPerIteration, writeChannel); start += bytesPerIteration; } */ /* But it's easier to use this convenience method from the DataAccessIO: (if the underlying storage method for this file is local filesystem, the DataAccessIO will simply copy the file using Files.copy, like this: Files.copy(tempLocationPath, dataAccess.getFileSystemLocation(), StandardCopyOption.REPLACE_EXISTING); */ dataAccess.copyPath(tempLocationPath); // Set filesize in bytes // dataFile.setFilesize(dataAccess.getSize()); } catch (IOException ioex) { logger.warning("Failed to save the file, storage id " + dataFile.getStorageIdentifier()); } finally { if (readChannel != null) { try { readChannel.close(); } catch (IOException e) { } } if (writeChannel != null) { try { writeChannel.close(); } catch (IOException e) { } } } // delete the temporary file: try { logger.fine("Will attempt to delete the temp file " + tempLocationPath.toString()); // also, delete a temporary thumbnail image file, if exists: // (TODO: probably not a very good style, that the size of the thumbnail // is hard-coded here; it may change in the future...) Path tempThumbnailPath = Paths.get(tempLocationPath.toString() + ".thumb64"); Files.delete(tempLocationPath); if (tempThumbnailPath.toFile().exists()) { Files.delete(tempThumbnailPath); } } catch (IOException ex) { // (non-fatal - it's just a temp file.) logger.warning("Failed to delete temp file " + tempLocationPath.toString()); } // Any necessary post-processing: performPostProcessingTasks(dataFile); } } } }
From source file:com.sonicle.webtop.mail.Service.java
private void fastStreamCopy(final InputStream src, final OutputStream dest) throws IOException { final ReadableByteChannel in = Channels.newChannel(src); final WritableByteChannel out = Channels.newChannel(dest); fastChannelCopy(in, out);//ww w . j ava 2 s. co m in.close(); out.close(); }