List of usage examples for java.io FileOutputStream getChannel
public FileChannel getChannel()
From source file:org.ops4j.pax.runner.platform.internal.PlatformImpl.java
/** * Downloads files from urls.//from w ww .j a v a 2 s. com * * @param workDir the directory where to download bundles * @param url of the file to be downloaded * @param displayName to be shown during download * @param overwrite if the bundles should be overwritten * @param checkAttributes whether or not to check attributes in the manifest * @param failOnValidation if validation fails should or not fail with an exception (or just return null) * @param downloadFeeback whether or not downloading process should display fine grained progres info * * @return the File corresponding to the downloaded file, or null if the bundle is invalid (not an osgi bundle) * * @throws PlatformException if the url could not be downloaded */ private File download(final File workDir, final URL url, final String displayName, final Boolean overwrite, final boolean checkAttributes, final boolean failOnValidation, final boolean downloadFeeback) throws PlatformException { LOGGER.debug("Downloading [" + url + "]"); File downloadedBundlesFile = new File(workDir, "bundles/downloaded_bundles.properties"); Properties fileNamesForUrls = loadProperties(downloadedBundlesFile); String downloadedFileName = fileNamesForUrls.getProperty(url.toExternalForm()); String hashFileName = "" + url.toExternalForm().hashCode(); if (downloadedFileName == null) { // destination will be made based on the hashcode of the url to be downloaded downloadedFileName = hashFileName + ".jar"; } File destination = new File(workDir, "bundles/" + downloadedFileName); // download the bundle only if is a forced overwrite or the file does not exist or the file is there but is // invalid boolean forceOverwrite = overwrite || !destination.exists(); if (!forceOverwrite) { try { String cachingName = determineCachingName(destination, hashFileName); if (!destination.getName().equals(cachingName)) { throw new PlatformException("File " + destination + " should have name " + cachingName); } } catch (PlatformException ignore) { forceOverwrite = true; } } if (forceOverwrite) { try { LOGGER.debug("Creating new file at destination: " + destination.getAbsolutePath()); destination.getParentFile().mkdirs(); destination.createNewFile(); FileOutputStream os = null; try { os = new FileOutputStream(destination); FileChannel fileChannel = os.getChannel(); StreamUtils.ProgressBar progressBar = null; if (LOGGER.isInfoEnabled()) { if (downloadFeeback) { progressBar = new StreamUtils.FineGrainedProgressBar(displayName); } else { progressBar = new StreamUtils.CoarseGrainedProgressBar(displayName); } } StreamUtils.streamCopy(url, fileChannel, progressBar); fileChannel.close(); LOGGER.debug("Succesfully downloaded to [" + destination + "]"); } finally { if (os != null) { os.close(); } } } catch (IOException e) { throw new PlatformException("[" + url + "] could not be downloaded", e); } } if (checkAttributes) { try { validateBundle(url, destination); } catch (PlatformException e) { if (failOnValidation) { throw e; } return null; } } String cachingName = determineCachingName(destination, hashFileName); File newDestination = new File(destination.getParentFile(), cachingName); if (!cachingName.equals(destination.getName())) { if (newDestination.exists()) { if (!newDestination.delete()) { throw new PlatformException("Cannot delete " + newDestination); } } if (!destination.renameTo(newDestination)) { throw new PlatformException("Cannot rename " + destination + " to " + newDestination); } fileNamesForUrls.setProperty(url.toExternalForm(), cachingName); saveProperties(fileNamesForUrls, downloadedBundlesFile); } return newDestination; }
From source file:hd3gtv.mydmam.useraction.fileoperation.CopyMove.java
private void copyFile(File source_file, File destination_file) throws IOException { if (destination_file.exists()) { Log2Dump dump = new Log2Dump(); dump.add("source_file", source_file); dump.add("destination_file", destination_file); dump.add("delete_after_copy", delete_after_copy); if (fileexistspolicy == FileExistsPolicy.IGNORE) { Log2.log.debug("Destination file exists, ignore copy/move", dump); return; } else if (fileexistspolicy == FileExistsPolicy.OVERWRITE) { Log2.log.debug("Destination file exists, overwrite it", dump); FileUtils.forceDelete(destination_file); } else if (fileexistspolicy == FileExistsPolicy.RENAME) { // destination_file int cursor = 1; int dot_pos; StringBuilder sb;/*from w ww .j a va 2s . co m*/ while (destination_file.exists()) { sb = new StringBuilder(); sb.append(destination_file.getParent()); sb.append(File.separator); dot_pos = destination_file.getName().lastIndexOf("."); if (dot_pos > 0) { sb.append(destination_file.getName().substring(0, dot_pos)); sb.append(" ("); sb.append(cursor); sb.append(")"); sb.append( destination_file.getName().substring(dot_pos, destination_file.getName().length())); } else { sb.append(destination_file.getName()); sb.append(" ("); sb.append(cursor); sb.append(")"); } destination_file = new File(sb.toString()); cursor++; } dump.add("new destination file name", destination_file); Log2.log.debug("Destination file exists, change destionation name", dump); } } /** * Imported from org.apache.commons.io.FileUtils * Licensed to the Apache Software Foundation, * http://www.apache.org/licenses/LICENSE-2.0 */ FileInputStream fis = null; FileOutputStream fos = null; FileChannel input = null; FileChannel output = null; try { fis = new FileInputStream(source_file); fos = new FileOutputStream(destination_file); input = fis.getChannel(); output = fos.getChannel(); long size = input.size(); long pos = 0; long count = 0; while (pos < size) { count = (size - pos) > FIFTY_MB ? FIFTY_MB : (size - pos); pos += output.transferFrom(input, pos, count); if (progression != null) { actual_progress_value = (int) ((pos + progress_copy) / (1024 * 1024)); if (actual_progress_value > last_progress_value) { last_progress_value = actual_progress_value; progression.updateProgress(actual_progress_value, progress_size); } } } } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(fos); IOUtils.closeQuietly(input); IOUtils.closeQuietly(fis); } if (source_file.length() != destination_file.length()) { throw new IOException( "Failed to copy full contents from '" + source_file + "' to '" + destination_file + "'"); } if (destination_file.setExecutable(source_file.canExecute()) == false) { Log2.log.error("Can't set Executable status to dest file", new IOException(destination_file.getPath())); } if (destination_file.setLastModified(source_file.lastModified()) == false) { Log2.log.error("Can't set LastModified status to dest file", new IOException(destination_file.getPath())); } }
From source file:org.gdms.driver.shapefile.ShapefileDriver.java
@Override public void createSource(String path, Metadata metadata, DataSourceFactory dataSourceFactory) throws DriverException { LOG.trace("Creating source file"); // write dbf/*from ww w . j a v a 2 s . c om*/ String dbfFile = replaceExtension(new File(path), ".dbf").getAbsolutePath(); DBFDriver tempDbfDriver = new DBFDriver(); tempDbfDriver.setDataSourceFactory(dataSourceFactory); tempDbfDriver.createSource(dbfFile, new DBFMetadata(metadata), dataSourceFactory); // write shapefile and shx try { FileOutputStream shpFis = new FileOutputStream(new File(path)); final FileOutputStream shxFis = new FileOutputStream(replaceExtension(new File(path), ".shx")); ShapefileWriter writer = new ShapefileWriter(shpFis.getChannel(), shxFis.getChannel()); Type geomType = getGeometryType(metadata); int dimension = getGeometryDimension(null, metadata); int typeCode = geomType.getTypeCode(); if (typeCode == Type.NULL || (typeCode & Type.GEOMETRY) == 0) { throw new DriverException("Shapefiles need a " + "specific geometry type"); } ShapeType shapeType = getShapeType(geomType, dimension); writer.writeHeaders(new Envelope(0, 0, 0, 0), shapeType, 0, 100); writer.close(); writeprj(replaceExtension(new File(path), ".prj"), metadata); } catch (FileNotFoundException e) { throw new DriverException(e); } catch (IOException e) { throw new DriverException(e); } }
From source file:edu.ucsb.eucalyptus.storage.fs.FileSystemStorageManager.java
public void copyObject(String sourceBucket, String sourceObject, String destinationBucket, String destinationObject) throws IOException { File oldObjectFile = new File( rootDirectory + FILE_SEPARATOR + sourceBucket + FILE_SEPARATOR + sourceObject); File newObjectFile = new File( rootDirectory + FILE_SEPARATOR + destinationBucket + FILE_SEPARATOR + destinationObject); if (!oldObjectFile.equals(newObjectFile)) { FileInputStream fileInputStream = null; FileChannel fileIn = null; FileOutputStream fileOutputStream = null; FileChannel fileOut = null; try {// www . j av a 2s. c o m fileInputStream = new FileInputStream(oldObjectFile); fileIn = fileInputStream.getChannel(); fileOutputStream = new FileOutputStream(newObjectFile); fileOut = fileOutputStream.getChannel(); fileIn.transferTo(0, fileIn.size(), fileOut); } finally { if (fileIn != null) fileIn.close(); if (fileInputStream != null) fileInputStream.close(); if (fileOut != null) fileOut.close(); if (fileOutputStream != null) fileOutputStream.close(); } } }
From source file:org.gdms.driver.shapefile.ShapefileDriver.java
@Override public void writeFile(final File file, final DataSet dataSource, ProgressMonitor pm) throws DriverException { LOG.trace("Writing file " + file.getAbsolutePath()); // write dbf//from w ww .ja v a 2s.com DBFDriver tempDbfDriver = new DBFDriver(); tempDbfDriver.setDataSourceFactory(dataSourceFactory); tempDbfDriver.writeFile(replaceExtension(file, ".dbf"), new DBFRowProvider(dataSource), pm); // write shapefile and shx try { final long rowCount = dataSource.getRowCount(); pm.startTask("Writing geometries", rowCount); FileOutputStream shpFis = new FileOutputStream(file); final FileOutputStream shxFis = new FileOutputStream(replaceExtension(file, ".shx")); ShapefileWriter writer = new ShapefileWriter(shpFis.getChannel(), shxFis.getChannel()); Envelope fullExtent = DriverUtilities.getFullExtent(dataSource); Metadata outMetadata = dataSource.getMetadata(); //What geometries are we about to process ? Type geomType = getGeometryType(outMetadata); //We have a look at the dimension. Are we in 3D ? int dimension = getGeometryDimension(dataSource, outMetadata); ShapeType shapeType; //We shall analyse the type code. int typeCode = geomType.getTypeCode(); //Are we dealing with a general vectorial type ? boolean isGeneral = typeCode == Type.GEOMETRY || typeCode == Type.GEOMETRYCOLLECTION; //Do we have a constraint upon dimension ? boolean noDimCons = geomType.getConstraint(Constraint.DIMENSION_3D_GEOMETRY) == null; if (isGeneral && typeCode != Type.NULL && noDimCons) { //We're in a case that is too general for a shape... let's //try to improve this as far as we can. LOG.warn("No geometry type in the " + "metadata. Will take the type of the first geometry"); shapeType = getFirstShapeType(dataSource, dimension); if (shapeType == null) { throw new IllegalArgumentException("A " + "geometry type have to be specified"); } } else { shapeType = getShapeType(geomType, dimension); } int fileLength = computeSize(dataSource, shapeType); writer.writeHeaders(fullExtent, shapeType, (int) rowCount, fileLength); final int spatialFieldIndex = MetadataUtilities.getSpatialFieldIndex(dataSource.getMetadata()); for (int i = 0; i < rowCount; i++) { if (i >= 100 && i % 100 == 0) { if (pm.isCancelled()) { break; } else { pm.progressTo(i); } } Value v = dataSource.getFieldValue(i, spatialFieldIndex); if (!v.isNull()) { writer.writeGeometry(convertGeometry(v.getAsGeometry(), shapeType)); } else { writer.writeGeometry(null); } } pm.progressTo(rowCount); writer.close(); writeprj(replaceExtension(file, ".prj"), dataSource.getMetadata()); } catch (FileNotFoundException e) { throw new DriverException(e); } catch (IOException e) { throw new DriverException(e); } catch (ShapefileException e) { throw new DriverException(e); } pm.endTask(); }
From source file:dk.netarkivet.harvester.indexserver.FileBasedCache.java
/** * Ensure that a file containing the appropriate content exists for the ID. * If the content cannot be found, this method may return null (if I is a * simple type) or an appropriate subset (if I is, say, a Set) indicating * the data that is actually available. In the latter case, calling cache * on the returned set should always fill the file for that subset (barring * catastrophic failure).// www .j a v a 2 s. co m * * Locking: If the file is not immediately found, we enter a file-creation * state. To avoid corrupted data, we must ensure that only one cache * instance, and only one thread within any instance, creates the file. Thus * as long as somebody else seems to be creating the file, we wait and see * if they finish. This is checked by having an exclusive lock on a * ".working" file (we cannot use the result file, as it has to be created * to be locked, and we may end up with a different cached file than we * thought, see above). The .working file itself is irrelevant, only the * lock on it matters. * * @param id Some sort of id that uniquely identifies the item within the * cache. * * @return The id given if it was successfully fetched, otherwise null if * the type parameter I does not allow subsets, or a subset of id if * it does. This subset should be immediately cacheable. */ public I cache(I id) { ArgumentNotValid.checkNotNull(id, "id"); File cachedFile = getCacheFile(id); try { File fileBehindLockFile = new File(cachedFile.getAbsolutePath() + ".working"); FileOutputStream lockFile = new FileOutputStream(fileBehindLockFile); FileLock lock = null; // Make sure no other thread tries to create this log.debug("Waiting to enter synchronization on " + fileBehindLockFile.getAbsolutePath().intern()); // FIXME Potential memory leak. intern() remembers all strings until JVM exits. synchronized (fileBehindLockFile.getAbsolutePath().intern()) { try { // Make sure no other process tries to create this. log.debug("locking filechannel for file '" + fileBehindLockFile.getAbsolutePath() + "' (thread = " + Thread.currentThread().getName() + ")"); try { lock = lockFile.getChannel().lock(); } catch (OverlappingFileLockException e) { // Exception is logged below throw new IOException(e.getMessage(), e); } // Now we know nobody else touches the file. // If the file already exists, just return it. if (cachedFile.exists()) { return id; } return cacheData(id); } finally { if (lock != null) { log.debug("release lock on filechannel " + lockFile.getChannel()); lock.release(); } lockFile.close(); } } } catch (IOException e) { String errMsg = "Error obtaining lock for file '" + cachedFile.getAbsolutePath() + "'."; log.warn(errMsg, e); throw new IOFailure(errMsg, e); } }
From source file:org.emonocot.job.io.StaxEventItemWriter.java
/** * Helper method for opening output source at given file position. * @param position Set the position//from w w w. j a va2s.co m * @param restarted Is this execution being restarted */ private void open(final long position, final boolean restarted) { File file; FileOutputStream os = null; try { file = resource.getFile(); FileUtils.setUpOutputFile(file, restarted, overwriteOutput); Assert.state(resource.exists(), "Output resource must exist"); os = new FileOutputStream(file, true); channel = os.getChannel(); setPosition(position); } catch (IOException ioe) { throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]", ioe); } XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); if (outputFactory.isPropertySupported("com.ctc.wstx.automaticEndElements")) { // If the current XMLOutputFactory implementation is supplied by // Woodstox >= 3.2.9 we want to disable its // automatic end element feature (see: // http://jira.codehaus.org/browse/WSTX-165) per // http://jira.springframework.org/browse/BATCH-761. outputFactory.setProperty("com.ctc.wstx.automaticEndElements", Boolean.FALSE); } try { if (transactional) { bufferedWriter = new TransactionAwareBufferedWriter(new OutputStreamWriter(os, encoding), new Runnable() { public void run() { closeStream(); } }); } else { bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, encoding)); } delegateEventWriter = outputFactory.createXMLEventWriter(bufferedWriter); eventWriter = new NoStartEndDocumentStreamWriter(delegateEventWriter); if (!restarted) { startDocument(delegateEventWriter); } } catch (XMLStreamException xse) { throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]", xse); } catch (UnsupportedEncodingException e) { throw new DataAccessResourceFailureException( "Unable to write to file resource: [" + resource + "] with encoding=[" + encoding + "]", e); } }
From source file:edu.harvard.iq.dvn.ingest.dsb.DSBWrapper.java
public String ingest(StudyFileEditBean file) throws IOException { dbgLog.fine("***** DSBWrapper: ingest(): start *****\n"); String ddi = null;/* w w w. j a va2s . c o m*/ BufferedInputStream infile = null; // ingest-source file File tempFile = new File(file.getTempSystemFileLocation()); SDIOData sd = null; if (file.getControlCardSystemFileLocation() == null) { // A "classic", 1 file ingest: String mime_type = file.getStudyFile().getFileType(); infile = new BufferedInputStream(new FileInputStream(tempFile)); dbgLog.info("\nfile mimeType=" + mime_type + "\n\n"); // get available FileReaders for this MIME-type Iterator<StatDataFileReader> itr = StatDataIO.getStatDataFileReadersByMIMEType(mime_type); if (itr.hasNext()) { // use the first Subsettable data reader StatDataFileReader sdioReader = itr.next(); dbgLog.info("reader class name=" + sdioReader.getClass().getName()); if (mime_type != null) { String requestedCharacterEncoding = file.getDataLanguageEncoding(); if (requestedCharacterEncoding != null) { dbgLog.fine("Will try to process the file assuming that the character strings are " + "encoded in " + requestedCharacterEncoding); sdioReader.setDataLanguageEncoding(requestedCharacterEncoding); } sd = sdioReader.read(infile, null); } else { // fail-safe block if mime_type is null // check the format type again and then read the file dbgLog.info("mime-type was null: use the back-up method"); sd = StatDataIO.read(infile, null); } } else { throw new IllegalArgumentException( "No FileReader Class found" + " for this mime type=" + mime_type); } } else { // This is a 2-file ingest. // As of now, there are 2 supported methods: // 1. CSV raw data file + SPSS control card; // 2. TAB raw data file + DDI control card; // NOTE, that "POR file with the Extended Labels" is NOT a 2-file. // control card-based ingest! Rather, we ingest the file as a regular // SPSS/POR dataset, then modify the variable labels in the resulting // TabularFile. File rawDataFile = tempFile; infile = new BufferedInputStream(new FileInputStream(file.getControlCardSystemFileLocation())); String controlCardType = file.getControlCardType(); if (controlCardType == null || controlCardType.equals("")) { dbgLog.info("No Control Card Type supplied."); throw new IllegalArgumentException("No Control Card Type supplied."); } Iterator<StatDataFileReader> itr = StatDataIO.getStatDataFileReadersByFormatName(controlCardType); if (!itr.hasNext()) { dbgLog.info("No FileReader class found for " + controlCardType + "."); throw new IllegalArgumentException("No FileReader Class found for " + controlCardType + "."); } StatDataFileReader sdioReader = itr.next(); dbgLog.info("reader class name=" + sdioReader.getClass().getName()); sd = sdioReader.read(infile, rawDataFile); } if (sd != null) { SDIOMetadata smd = sd.getMetadata(); // tab-file: source file String tabDelimitedDataFileLocation = smd.getFileInformation().get("tabDelimitedDataFileLocation") .toString(); dbgLog.fine("tabDelimitedDataFileLocation=" + tabDelimitedDataFileLocation); dbgLog.fine("data file(tempFile): abs path:\n" + file.getTempSystemFileLocation()); dbgLog.fine("mimeType :\n" + file.getStudyFile().getFileType()); if (infile != null) { infile.close(); } // parse the response StudyFile f = file.getStudyFile(); // first, check dir // create a sub-directory "ingested" File newDir = new File(tempFile.getParentFile(), "ingested"); if (!newDir.exists()) { newDir.mkdirs(); } dbgLog.fine("newDir: abs path:\n" + newDir.getAbsolutePath()); // tab-file case: destination File newFile = new File(newDir, tempFile.getName()); // nio-based file-copying idiom FileInputStream fis = new FileInputStream(tabDelimitedDataFileLocation); FileOutputStream fos = new FileOutputStream(newFile); FileChannel fcin = fis.getChannel(); FileChannel fcout = fos.getChannel(); fcin.transferTo(0, fcin.size(), fcout); fcin.close(); fcout.close(); fis.close(); fos.close(); dbgLog.fine("newFile: abs path:\n" + newFile.getAbsolutePath()); // store the tab-file location file.setIngestedSystemFileLocation(newFile.getAbsolutePath()); // finally, if we have an extended variable map, let's replace the // labels that have been found in the data file: if (file.getExtendedVariableLabelMap() != null) { for (String varName : file.getExtendedVariableLabelMap().keySet()) { if (smd.getVariableLabel().containsKey(varName)) { smd.getVariableLabel().put(varName, file.getExtendedVariableLabelMap().get(varName)); } } } // return xmlToParse; DDIWriter dw = new DDIWriter(smd); ddi = dw.generateDDI(); return ddi; } return null; }
From source file:org.evilco.bot.powersweeper.platform.DriverManager.java
/** * Downloads all natives.//from w w w . j ava 2 s. co m */ public void downloadNatives() { getLogger().entry(); // skip download if unneeded if (this.configuration.getDriver() != Driver.CHROME) { // debug getLogger().debug("Driver download is not required for " + this.configuration.getDriver().toString() + " driver implementation."); // exit getLogger().exit(); return; } // check if (!this.configuration.isNativeDownloadEnabled()) { // log getLogger().info("Automatic download of driver natives is disabled."); // log possible error if (!this.getDriverNativeFile().exists()) getLogger().warn("The native could not be found! Proceed with caution."); // exit getLogger().exit(); return; } // check driver file if (this.getDriverNativeFile().exists()) { // log getLogger().info("Driver file seems to exist. Skipping download."); // exit getLogger().exit(); return; } // log getLogger().info("Starting download of natives."); // get platform Platform platform = Platform.guessPlatform(); boolean is64Bit = System.getProperty("os.arch").endsWith("64"); // build download URL String filename = "potato"; switch (platform) { case LINUX: case SOLARIS: case UNKNOWN: filename = "linux" + (is64Bit ? "64" : 32); break; case MAC_OS_X: filename = "mac32"; break; case WINDOWS: filename = "win32"; break; } // download and extract FileOutputStream outputStream = null; // ensure directory exists this.getDriverNativeFile().getParentFile().mkdirs(); try { URL downloadURL = new URL(String.format(CHROME_DRIVER_URL, filename)); // log getLogger().info("Downloading driver from " + downloadURL.toString() + " ..."); // create file reference File zipFile = new File(this.getDriverNativeFile().getParentFile(), filename + ".zip"); // start download ReadableByteChannel readableByteChannel = Channels.newChannel(downloadURL.openStream()); outputStream = new FileOutputStream(zipFile); outputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE); // extract zip contents this.extract(zipFile); // log getLogger().info("Finished native download."); // delete zip if (zipFile.delete()) getLogger().info("Removed temporary archive."); else getLogger().warn("Could not remove temporary archive."); } catch (IOException ex) { getLogger().error("Could not download file from URL \"" + String.format(CHROME_DRIVER_URL, filename) + "\": " + ex.getMessage(), ex); } finally { if (outputStream != null) IOUtils.closeQuietly(outputStream); } // trace getLogger().exit(); }