List of usage examples for java.io FileInputStream getChannel
public FileChannel getChannel()
From source file:org.h2gis.functions.io.osm.OSMParser.java
/** * Read the OSM file and create its corresponding tables. * * @param inputFile/*from ww w.j av a 2s.c o m*/ * @param tableName * @param connection * @param progress * @return * @throws SQLException */ public boolean read(Connection connection, String tableName, File inputFile, ProgressVisitor progress) throws SQLException { this.progress = progress.subProcess(100); // Initialisation final boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); boolean success = false; TableLocation requestedTable = TableLocation.parse(tableName, isH2); String osmTableName = requestedTable.getTable(); checkOSMTables(connection, isH2, requestedTable, osmTableName); createOSMDatabaseModel(connection, isH2, requestedTable, osmTableName); FileInputStream fs = null; try { fs = new FileInputStream(inputFile); this.fc = fs.getChannel(); this.fileSize = fc.size(); // Given the file size and an average node file size. // Skip how many nodes in order to update progression at a step of 1% readFileSizeEachNode = Math.max(1, (this.fileSize / AVERAGE_NODE_SIZE) / 100); nodeCountProgress = 0; XMLReader parser = XMLReaderFactory.createXMLReader(); parser.setErrorHandler(this); parser.setContentHandler(this); if (inputFile.getName().endsWith(".osm")) { parser.parse(new InputSource(fs)); } else if (inputFile.getName().endsWith(".osm.gz")) { parser.parse(new InputSource(new GZIPInputStream(fs))); } else if (inputFile.getName().endsWith(".osm.bz2")) { parser.parse(new InputSource(new BZip2CompressorInputStream(fs))); } else { throw new SQLException("Supported formats are .osm, .osm.gz, .osm.bz2"); } success = true; } catch (SAXException ex) { throw new SQLException(ex); } catch (IOException ex) { throw new SQLException("Cannot parse the file " + inputFile.getAbsolutePath(), ex); } finally { try { if (fs != null) { fs.close(); } } catch (IOException ex) { throw new SQLException("Cannot close the file " + inputFile.getAbsolutePath(), ex); } // When the reading ends, close() method has to be called if (nodePreparedStmt != null) { nodePreparedStmt.close(); } if (nodeTagPreparedStmt != null) { nodeTagPreparedStmt.close(); } if (wayPreparedStmt != null) { wayPreparedStmt.close(); } if (wayTagPreparedStmt != null) { wayTagPreparedStmt.close(); } if (wayNodePreparedStmt != null) { wayNodePreparedStmt.close(); } if (relationPreparedStmt != null) { relationPreparedStmt.close(); } if (relationTagPreparedStmt != null) { relationTagPreparedStmt.close(); } if (nodeMemberPreparedStmt != null) { nodeMemberPreparedStmt.close(); } if (wayMemberPreparedStmt != null) { wayMemberPreparedStmt.close(); } if (relationMemberPreparedStmt != null) { relationMemberPreparedStmt.close(); } if (tagPreparedStmt != null) { tagPreparedStmt.close(); } } return success; }
From source file:tachyon.command.TFsShell.java
private int copyPath(File src, TachyonFS tachyonClient, String dstPath) throws IOException { if (!src.isDirectory()) { int fileId = tachyonClient.createFile(dstPath); if (fileId == -1) { return -1; }/*from w w w .j a va 2s . c om*/ TachyonFile tFile = tachyonClient.getFile(fileId); OutStream os = tFile.getOutStream(WriteType.CACHE_THROUGH); FileInputStream in = new FileInputStream(src); FileChannel channel = in.getChannel(); ByteBuffer buf = ByteBuffer.allocate(Constants.KB); while (channel.read(buf) != -1) { buf.flip(); os.write(buf.array(), 0, buf.limit()); } os.close(); channel.close(); in.close(); return 0; } else { tachyonClient.mkdir(dstPath); for (String file : src.list()) { String newPath = FilenameUtils.concat(dstPath, file); File srcFile = new File(src, file); if (copyPath(srcFile, tachyonClient, newPath) == -1) { return -1; } } } return 0; }
From source file:org.h2gis.drivers.osm.OSMParser.java
/** * Read the OSM file and create its corresponding tables. * * @param inputFile//from ww w . j a v a 2 s .c o m * @param tableName * @param connection * @param progress * @return * @throws SQLException */ public boolean read(Connection connection, String tableName, File inputFile, ProgressVisitor progress) throws SQLException { this.progress = progress.subProcess(100); // Initialisation final boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); boolean success = false; TableLocation requestedTable = TableLocation.parse(tableName, isH2); String osmTableName = requestedTable.getTable(); checkOSMTables(connection, isH2, requestedTable, osmTableName); createOSMDatabaseModel(connection, isH2, requestedTable, osmTableName); FileInputStream fs = null; try { fs = new FileInputStream(inputFile); this.fc = fs.getChannel(); this.fileSize = fc.size(); // Given the file size and an average node file size. // Skip how many nodes in order to update progression at a step of 1% readFileSizeEachNode = Math.max(1, (this.fileSize / AVERAGE_NODE_SIZE) / 100); nodeCountProgress = 0; XMLReader parser = XMLReaderFactory.createXMLReader(); parser.setErrorHandler(this); parser.setContentHandler(this); if (inputFile.getName().endsWith(".osm")) { parser.parse(new InputSource(fs)); } else if (inputFile.getName().endsWith(".osm.gz")) { parser.parse(new InputSource(new GZIPInputStream(fs))); } else if (inputFile.getName().endsWith(".osm.bz2")) { parser.parse(new InputSource(new BZip2CompressorInputStream(fs))); } success = true; } catch (SAXException ex) { throw new SQLException(ex); } catch (IOException ex) { throw new SQLException("Cannot parse the file " + inputFile.getAbsolutePath(), ex); } finally { try { if (fs != null) { fs.close(); } } catch (IOException ex) { throw new SQLException("Cannot close the file " + inputFile.getAbsolutePath(), ex); } // When the reading ends, close() method has to be called if (nodePreparedStmt != null) { nodePreparedStmt.close(); } if (nodeTagPreparedStmt != null) { nodeTagPreparedStmt.close(); } if (wayPreparedStmt != null) { wayPreparedStmt.close(); } if (wayTagPreparedStmt != null) { wayTagPreparedStmt.close(); } if (wayNodePreparedStmt != null) { wayNodePreparedStmt.close(); } if (relationPreparedStmt != null) { relationPreparedStmt.close(); } if (relationTagPreparedStmt != null) { relationTagPreparedStmt.close(); } if (nodeMemberPreparedStmt != null) { nodeMemberPreparedStmt.close(); } if (wayMemberPreparedStmt != null) { wayMemberPreparedStmt.close(); } if (relationMemberPreparedStmt != null) { relationMemberPreparedStmt.close(); } if (tagPreparedStmt != null) { tagPreparedStmt.close(); } } return success; }
From source file:org.opennms.features.newts.converter.rrd.converter.JRobinConverter.java
public boolean moveFileSafely(final File in, final File out) throws IOException { FileInputStream fis = null; FileOutputStream fos = null;//from ww w. j a v a2 s . com FileChannel inChannel = null; FileChannel outChannel = null; final File tempOut = File.createTempFile("move", ".tmp"); try { fis = new FileInputStream(in); fos = new FileOutputStream(tempOut); inChannel = fis.getChannel(); outChannel = fos.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); } finally { try { if (inChannel != null) inChannel.close(); } catch (IOException e) { LogUtils.debugf(JRobinConverter.class, "failed to close channel %s", inChannel); } try { if (outChannel != null) outChannel.close(); } catch (IOException e) { LogUtils.debugf(JRobinConverter.class, "failed to close channel %s", outChannel); } try { if (fis != null) fis.close(); } catch (IOException e) { LogUtils.debugf(JRobinConverter.class, "failed to close stream %s", fis); } try { if (fos != null) fos.close(); } catch (IOException e) { LogUtils.debugf(JRobinConverter.class, "failed to close stream %s", fos); } } out.delete(); if (!out.exists()) { tempOut.renameTo(out); return in.delete(); } return false; }
From source file:org.codehaus.preon.buffer.DefaultBitBuffer.java
/** Read byte buffer containing binary stream and set the bit pointer position to 0. */ public DefaultBitBuffer(String fileName) { File file = new File(fileName); // Open the file and then get a org.codehaus.preon.channel.channel from the stream FileInputStream fis; try {/* w w w . j av a 2 s . co m*/ fis = new FileInputStream(file); FileChannel fc = fis.getChannel(); // Get the file's size and then map it into memory int fileSize = (int) fc.size(); ByteBuffer inputByteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fileSize); // Close the org.codehaus.preon.channel.channel and the stream fc.close(); this.byteBuffer = inputByteBuffer; bitBufBitSize = ((long) (inputByteBuffer.capacity())) << 3; bitPos = 0; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.totschnig.myexpenses.util.Utils.java
public static boolean copy(File src, File dst) { FileInputStream srcStream = null; FileOutputStream dstStream = null; try {/*w ww .j a v a2 s . c om*/ srcStream = new FileInputStream(src); dstStream = new FileOutputStream(dst); dstStream.getChannel().transferFrom(srcStream.getChannel(), 0, srcStream.getChannel().size()); return true; } catch (FileNotFoundException e) { Log.e("MyExpenses", e.getLocalizedMessage()); return false; } catch (IOException e) { Log.e("MyExpenses", e.getLocalizedMessage()); return false; } finally { try { srcStream.close(); } catch (Exception e) { } try { dstStream.close(); } catch (Exception e) { } } }
From source file:org.sakaiproject.search.index.impl.SegmentInfoImpl.java
/** * @param f/*from w ww .j a va 2 s.c o m*/ * @param message */ private void dumpFileInfo(File f, String message) { String fileInfo = message + "(" + f.getPath() + "):"; if (!f.exists()) { log.error(fileInfo + " File No longer exists"); } else { log.info(fileInfo + " size=[" + f.length() + "] lastModified=[" + f.lastModified() + "] read=[" + f.canRead() + "] write=[" + f.canWrite() + "] hidden=[" + f.isHidden() + "]"); try { FileInputStream fin = new FileInputStream(f); fin.read(new byte[4096]); log.info(fileInfo + " readOk"); FileLock fl = fin.getChannel().tryLock(); fl.release(); fin.close(); log.info(fileInfo + " lockOk"); } catch (Exception ex) { log.warn(fileInfo + " Lock or Read failed: ", ex); } } }
From source file:alluxio.shell.command.CpCommand.java
/** * Copies a file or directory specified by srcPath from the local filesystem to dstPath in the * Alluxio filesystem space.//from w w w .j a v a 2 s .c om * * @param srcPath the {@link AlluxioURI} of the source file in the local filesystem * @param dstPath the {@link AlluxioURI} of the destination * @throws AlluxioException when Alluxio exception occurs * @throws IOException when non-Alluxio exception occurs */ private void copyPath(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException { File src = new File(srcPath.getPath()); if (!src.isDirectory()) { // If the dstPath is a directory, then it should be updated to be the path of the file where // src will be copied to. if (mFileSystem.exists(dstPath) && mFileSystem.getStatus(dstPath).isFolder()) { dstPath = dstPath.join(src.getName()); } FileOutStream os = null; try (Closer closer = Closer.create()) { os = closer.register(mFileSystem.createFile(dstPath)); FileInputStream in = closer.register(new FileInputStream(src)); FileChannel channel = closer.register(in.getChannel()); ByteBuffer buf = ByteBuffer.allocate(8 * Constants.MB); while (channel.read(buf) != -1) { buf.flip(); os.write(buf.array(), 0, buf.limit()); } } catch (Exception e) { // Close the out stream and delete the file, so we don't have an incomplete file lying // around. if (os != null) { os.cancel(); if (mFileSystem.exists(dstPath)) { mFileSystem.delete(dstPath); } } throw e; } } else { mFileSystem.createDirectory(dstPath); List<String> errorMessages = new ArrayList<>(); File[] fileList = src.listFiles(); if (fileList == null) { String errMsg = String.format("Failed to list files for directory %s", src); errorMessages.add(errMsg); fileList = new File[0]; } int misFiles = 0; for (File srcFile : fileList) { AlluxioURI newURI = new AlluxioURI(dstPath, new AlluxioURI(srcFile.getName())); try { copyPath(new AlluxioURI(srcPath.getScheme(), srcPath.getAuthority(), srcFile.getPath()), newURI); } catch (IOException e) { errorMessages.add(e.getMessage()); if (!mFileSystem.exists(newURI)) { misFiles++; } } } if (errorMessages.size() != 0) { if (misFiles == fileList.length) { // If the directory doesn't exist and no files were created, then delete the directory if (mFileSystem.exists(dstPath)) { mFileSystem.delete(dstPath); } } throw new IOException(Joiner.on('\n').join(errorMessages)); } } }
From source file:alluxio.cli.fs.command.CpCommand.java
/** * Copies a file or directory specified by srcPath from the local filesystem to dstPath in the * Alluxio filesystem space./*from www . j av a2 s .c om*/ * * @param srcPath the {@link AlluxioURI} of the source file in the local filesystem * @param dstPath the {@link AlluxioURI} of the destination */ private void copyPath(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException { File src = new File(srcPath.getPath()); if (!src.isDirectory()) { // If the dstPath is a directory, then it should be updated to be the path of the file where // src will be copied to. if (mFileSystem.exists(dstPath) && mFileSystem.getStatus(dstPath).isFolder()) { dstPath = dstPath.join(src.getName()); } FileOutStream os = null; try (Closer closer = Closer.create()) { FileWriteLocationPolicy locationPolicy; locationPolicy = CommonUtils.createNewClassInstance( Configuration.<FileWriteLocationPolicy>getClass( PropertyKey.USER_FILE_COPY_FROM_LOCAL_WRITE_LOCATION_POLICY), new Class[] {}, new Object[] {}); os = closer.register(mFileSystem.createFile(dstPath, CreateFileOptions.defaults().setLocationPolicy(locationPolicy))); FileInputStream in = closer.register(new FileInputStream(src)); FileChannel channel = closer.register(in.getChannel()); ByteBuffer buf = ByteBuffer.allocate(8 * Constants.MB); while (channel.read(buf) != -1) { buf.flip(); os.write(buf.array(), 0, buf.limit()); } } catch (Exception e) { // Close the out stream and delete the file, so we don't have an incomplete file lying // around. if (os != null) { os.cancel(); if (mFileSystem.exists(dstPath)) { mFileSystem.delete(dstPath); } } throw e; } } else { mFileSystem.createDirectory(dstPath); List<String> errorMessages = new ArrayList<>(); File[] fileList = src.listFiles(); if (fileList == null) { String errMsg = String.format("Failed to list files for directory %s", src); errorMessages.add(errMsg); fileList = new File[0]; } int misFiles = 0; for (File srcFile : fileList) { AlluxioURI newURI = new AlluxioURI(dstPath, new AlluxioURI(srcFile.getName())); try { copyPath(new AlluxioURI(srcPath.getScheme(), srcPath.getAuthority(), srcFile.getPath()), newURI); } catch (IOException e) { errorMessages.add(e.getMessage()); if (!mFileSystem.exists(newURI)) { misFiles++; } } } if (errorMessages.size() != 0) { if (misFiles == fileList.length) { // If the directory doesn't exist and no files were created, then delete the directory if (mFileSystem.exists(dstPath)) { mFileSystem.delete(dstPath); } } throw new IOException(Joiner.on('\n').join(errorMessages)); } } }
From source file:com.mum.app.AutoSubmitPriceApp.java
public void SubmitPriceFromFile(String xmlFilename) { MyLog.log.log(Level.INFO, "Submit XML file:" + xmlFilename); SubmitFeedRequest request = new SubmitFeedRequest(); request.setMerchant(AutoSubmitPriceConfig.sellerId); request.setMWSAuthToken(AutoSubmitPriceConfig.sellerDevAuthToken); request.setMarketplaceIdList(new IdList(Arrays.asList(AutoSubmitPriceConfig.marketplaceId))); request.setFeedType("_POST_PRODUCT_PRICING_DATA_"); try {//from w ww. j a v a2s. c o m FileInputStream fis = new FileInputStream(xmlFilename); request.setFeedContent(fis); String contentMd5 = computeContentMD5HeaderValue(fis); fis.getChannel().position(0); request.setContentMD5(contentMd5); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return; } catch (Throwable e) { e.printStackTrace(); return; } SubmitPriceFeed(AutoSubmitPriceConfig.getServiceClient(), request); }