List of usage examples for java.io FileOutputStream getChannel
public FileChannel getChannel()
From source file:com.recomdata.transmart.data.export.util.ExportImageProcessor.java
public void run() { URL imageURL = null;//from ww w . ja v a 2 s .c o m File imageFile = new File(imagesTempDir + File.separator + filename); ReadableByteChannel rbc = null; FileOutputStream fos = null; try { if (StringUtils.isEmpty(imageURI)) return; imageURL = new URL(imageURI); rbc = Channels.newChannel(imageURL.openStream()); fos = new FileOutputStream(imageFile); fos.getChannel().transferFrom(rbc, 0, 1 << 24); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != fos) try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:org.pentaho.platform.osgi.ServerSocketBasedKarafInstanceResolver.java
private void assignAvailableCacheFolderForType(KarafInstance instance) { // something like karaf/caches String cacheParentFolder = instance.getCacheParentFolder(); // We separate the caches by client type to avoid reuse of an inappropriate data folder File clientTypeCacheFolder = new File(cacheParentFolder + "/" + instance.getClientType()); clientTypeCacheFolder.mkdirs();// w w w . j a v a2 s . co m File[] dataDirectories = clientTypeCacheFolder.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.startsWith(DATA); } }); int maxInstanceNoFound = 0; Pattern pattern = Pattern.compile(DATA + "\\-([0-9]+)"); // Go through existing data directories. If one is not in-use, use that. If not find the highest number and create // one greater for (File dataDirectory : dataDirectories) { boolean locked = true; Matcher matcher = pattern.matcher(dataDirectory.getName()); if (!matcher.matches()) { // unexpected directory, not a data folder, skipping continue; } // extract the data folder number int instanceNo = Integer.parseInt(matcher.group(1)); maxInstanceNoFound = Math.max(maxInstanceNoFound, instanceNo); File lockFile = new File(dataDirectory, ".lock"); FileLock lock = null; if (!lockFile.exists()) { // Lock file was not present, we can use it locked = false; } else { // Lock file was there, see if another process actually holds a lock on it try { FileOutputStream fileOutputStream = new FileOutputStream(lockFile); try { lock = fileOutputStream.getChannel().tryLock(); if (lock != null) { // not locked by another program instance.setCacheLock(lock); locked = false; } } catch (Exception e) { // Lock active on another program } } catch (FileNotFoundException e) { logger.error("Error locking file in data cache directory", e); } } if (!locked) { instance.setCachePath(dataDirectory.getPath()); // we're good to use this one, break out of existing directory loop break; } } if (instance.getCachePath() == null) { // Create a new cache folder File newCacheFolder = null; while (newCacheFolder == null) { maxInstanceNoFound++; File candidate = new File(clientTypeCacheFolder, DATA + "-" + maxInstanceNoFound); if (candidate.exists()) { // Another process slipped in and created a folder, lets skip over them continue; } newCacheFolder = candidate; } FileOutputStream fileOutputStream = null; try { newCacheFolder.mkdir(); // create lock file and lock it for this process File lockFile = new File(newCacheFolder, ".lock"); fileOutputStream = new FileOutputStream(lockFile); FileLock lock = fileOutputStream.getChannel().lock(); instance.setCachePath(newCacheFolder.getPath()); instance.setCacheLock(lock); } catch (IOException e) { logger.error("Error creating data cache folder", e); } } }
From source file:com.frostwire.util.MP4Muxer.java
public void mux(String video, String audio, String output, final MP4Metadata mt) throws IOException { FileInputStream videoIn = new FileInputStream(video); FileInputStream audioIn = new FileInputStream(audio); try {/*from w w w .ja va 2s. c o m*/ FileChannel videoChannel = videoIn.getChannel(); Movie videoMovie = buildMovie(videoChannel); FileChannel audioChannel = audioIn.getChannel(); Movie audioMovie = buildMovie(audioChannel); Movie outMovie = new Movie(); for (Track trk : videoMovie.getTracks()) { outMovie.addTrack(trk); } for (Track trk : audioMovie.getTracks()) { outMovie.addTrack(trk); } Container out = new DefaultMp4Builder() { @Override protected FileTypeBox createFileTypeBox(Movie movie) { List<String> minorBrands = new LinkedList<String>(); minorBrands.add("iso6"); minorBrands.add("avc1"); minorBrands.add("mp41"); minorBrands.add("\0\0\0\0"); return new FileTypeBox("MP4 ", 0, minorBrands); } @Override protected MovieBox createMovieBox(Movie movie, Map<Track, int[]> chunks) { MovieBox moov = super.createMovieBox(movie, chunks); moov.getMovieHeaderBox().setVersion(0); return moov; } @Override protected TrackBox createTrackBox(Track track, Movie movie, Map<Track, int[]> chunks) { TrackBox trak = super.createTrackBox(track, movie, chunks); trak.getTrackHeaderBox().setVersion(0); trak.getTrackHeaderBox().setVolume(1.0f); return trak; } @Override protected Box createUdta(Movie movie) { return mt != null ? addUserDataBox(mt) : null; } }.build(outMovie); FileOutputStream fos = new FileOutputStream(output); try { out.writeContainer(fos.getChannel()); } finally { IOUtils.closeQuietly(fos); } } finally { IOUtils.closeQuietly(videoIn); IOUtils.closeQuietly(audioIn); } }
From source file:com.frostwire.util.MP4Muxer.java
public void demuxAudio(String video, String output, final MP4Metadata mt) throws IOException { FileInputStream videoIn = new FileInputStream(video); try {//from w w w. ja v a2s . co m FileChannel videoChannel = videoIn.getChannel(); Movie videoMovie = buildMovie(videoChannel); Track audioTrack = null; for (Track trk : videoMovie.getTracks()) { if (trk.getHandler().equals("soun")) { audioTrack = trk; break; } } if (audioTrack == null) { IOUtils.closeQuietly(videoIn); return; } Movie outMovie = new Movie(); outMovie.addTrack(audioTrack); Container out = new DefaultMp4Builder() { @Override protected FileTypeBox createFileTypeBox(Movie movie) { List<String> minorBrands = new LinkedList<String>(); minorBrands.add("M4A "); minorBrands.add("mp42"); minorBrands.add("isom"); minorBrands.add("\0\0\0\0"); return new FileTypeBox("M4A ", 0, minorBrands); } @Override protected MovieBox createMovieBox(Movie movie, Map<Track, int[]> chunks) { MovieBox moov = super.createMovieBox(movie, chunks); moov.getMovieHeaderBox().setVersion(0); return moov; } @Override protected TrackBox createTrackBox(Track track, Movie movie, Map<Track, int[]> chunks) { TrackBox trak = super.createTrackBox(track, movie, chunks); trak.getTrackHeaderBox().setVersion(0); trak.getTrackHeaderBox().setVolume(1.0f); return trak; } @Override protected Box createUdta(Movie movie) { return mt != null ? addUserDataBox(mt) : null; } }.build(outMovie); FileOutputStream fos = new FileOutputStream(output); try { out.writeContainer(fos.getChannel()); } finally { IOUtils.closeQuietly(fos); } } finally { IOUtils.closeQuietly(videoIn); } }
From source file:net.ytbolg.mcxa.ImportOldMc.java
void fileChannelCopy(File s, File t) { FileInputStream fi = null;/* w w w.j a v a2 s . co m*/ FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try { fi = new FileInputStream(s); fo = new FileOutputStream(t); in = fi.getChannel();//? out = fo.getChannel();//? in.transferTo(0, in.size(), out);//?in???out? } catch (IOException e) { e.printStackTrace(); } finally { try { fi.close(); in.close(); fo.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:org.chromium.APKPackager.java
private void copyFile(File src, File dest) throws FileNotFoundException, IOException { FileInputStream istream = new FileInputStream(src); FileOutputStream ostream = new FileOutputStream(dest); FileChannel input = istream.getChannel(); FileChannel output = ostream.getChannel(); try {//ww w .j ava 2 s . co m input.transferTo(0, input.size(), output); } finally { istream.close(); ostream.close(); input.close(); output.close(); } }
From source file:org.tinymediamanager.scraper.http.Url.java
/** * Download an Url to a file via NIO FileChannel (synchron) * /* w w w . ja v a 2s . co m*/ * @param file * @return successful or not */ public boolean download(File file) { try { InputStream is = getInputStream(); if (is == null) { return false; } ReadableByteChannel rbc = Channels.newChannel(is); FileOutputStream fos = new FileOutputStream(file); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); return true; } catch (IOException e) { LOGGER.error("Error downloading " + this.url); } catch (InterruptedException ignored) { if (call != null) { call.cancel(); } } return false; }
From source file:com.cloud.maint.UpgradeManagerImpl.java
public String deployNewAgent(String url) { s_logger.info("Updating agent with binary from " + url); final HttpClient client = new HttpClient(s_httpClientManager); final GetMethod method = new GetMethod(url); int response; File file = null;// ww w .ja v a 2 s. co m try { response = client.executeMethod(method); if (response != HttpURLConnection.HTTP_OK) { s_logger.warn("Retrieving the agent gives response code: " + response); return "Retrieving the file from " + url + " got response code: " + response; } final InputStream is = method.getResponseBodyAsStream(); file = File.createTempFile("agent-", "-" + Long.toString(new Date().getTime())); file.deleteOnExit(); s_logger.debug("Retrieving new agent into " + file.getAbsolutePath()); final FileOutputStream fos = new FileOutputStream(file); final ByteBuffer buffer = ByteBuffer.allocate(2048); final ReadableByteChannel in = Channels.newChannel(is); final WritableByteChannel out = fos.getChannel(); while (in.read(buffer) != -1) { buffer.flip(); out.write(buffer); buffer.clear(); } in.close(); out.close(); s_logger.debug("New Agent zip file is now retrieved"); } catch (final HttpException e) { return "Unable to retrieve the file from " + url; } catch (final IOException e) { return "Unable to retrieve the file from " + url; } finally { method.releaseConnection(); } file.delete(); return "File will be deployed."; }
From source file:io.github.microcks.service.ServiceService.java
/** Download a remote HTTP URL into a temporary local file. */ private String handleRemoteFileDownload(String remoteUrl) throws IOException { // Build remote Url and local file. URL website = new URL(remoteUrl); String localFile = System.getProperty("java.io.tmpdir") + "/microcks-" + System.currentTimeMillis() + ".project"; // Set authenticator instance. Authenticator.setDefault(new UsernamePasswordAuthenticator(username, password)); ReadableByteChannel rbc = null; FileOutputStream fos = null; try {/* www . j av a2 s . c o m*/ rbc = Channels.newChannel(website.openStream()); // Transfer file to local. fos = new FileOutputStream(localFile); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } finally { if (fos != null) fos.close(); if (rbc != null) rbc.close(); } return localFile; }