List of usage examples for java.nio.channels FileChannel size
public abstract long size() throws IOException;
From source file:org.paxle.crawler.fs.impl.FsCrawler.java
private File copyChanneled(final File file, final ICrawlerDocument cdoc, final boolean useFsync) { logger.info(String.format("Copying '%s' using the copy mechanism of the OS%s", file, (useFsync) ? " with fsync" : "")); final ITempFileManager tfm = this.contextLocal.getCurrentContext().getTempFileManager(); if (tfm == null) { cdoc.setStatus(ICrawlerDocument.Status.UNKNOWN_FAILURE, "Cannot access ITempFileMananger from " + Thread.currentThread().getName()); return null; }/*ww w . j a v a2s. c om*/ FileInputStream fis = null; FileOutputStream fos = null; File out = null; try { out = tfm.createTempFile(); fis = new FileInputStream(file); fos = new FileOutputStream(out); final FileChannel in_fc = fis.getChannel(); final FileChannel out_fc = fos.getChannel(); long txed = 0L; while (txed < in_fc.size()) txed += in_fc.transferTo(txed, in_fc.size() - txed, out_fc); if (useFsync) out_fc.force(false); out_fc.close(); try { detectFormats(cdoc, fis); } catch (IOException ee) { logger.warn( String.format("Error detecting format of '%s': %s", cdoc.getLocation(), ee.getMessage())); } } catch (IOException e) { logger.error(String.format("Error copying '%s' to '%s': %s", cdoc.getLocation(), out, e.getMessage()), e); cdoc.setStatus(ICrawlerDocument.Status.UNKNOWN_FAILURE, e.getMessage()); } finally { if (fis != null) try { fis.close(); } catch (IOException e) { /* ignore */} if (fos != null) try { fos.close(); } catch (IOException e) { /* ignore */} } return out; }
From source file:edu.harvard.iq.dvn.core.analysis.NetworkDataServiceBean.java
private void copyFile(StudyFileEditBean editBean) throws IOException { File tempFile = new File(editBean.getTempSystemFileLocation()); dbgLog.fine("begin copyFile()"); // create a sub-directory "ingested" File newDir = new File(tempFile.getParentFile(), "ingested"); if (!newDir.exists()) { newDir.mkdirs();/*from w w w . j a va 2 s . co m*/ } dbgLog.fine("newDir: abs path:\n" + newDir.getAbsolutePath()); File newFile = new File(newDir, tempFile.getName()); FileInputStream fis = new FileInputStream(tempFile); 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 editBean.setIngestedSystemFileLocation(newFile.getAbsolutePath()); }
From source file:org.ednovo.gooru.application.util.ResourceProcessor.java
public void postPdfUpdate(final String resourceGooruOid) throws Exception { new TransactionBox() { @Override// www . java2 s. c o m public void execute() { try { logger.info("Updating Resource: " + resourceGooruOid); Resource resource = resourceRepository.findResourceByContentGooruId(resourceGooruOid); String repoPath = resource.getOrganization().getNfsStorageArea().getInternalPath() + resource.getFolder(); /* * getGooruImageUtil().scaleImage(repoPath + * resource.getThumbnail(), repoPath, null, * ResourceImageUtil.RESOURCE_THUMBNAIL_SIZES); */ ResourceInfo resourceInfo = resourceRepository.findResourceInfo(resource.getGooruOid()); if (resourceInfo == null) { resourceInfo = new ResourceInfo(); resourceInfo.setResource(resource); } resourceInfo.setLastUpdated(new Date()); RandomAccessFile raf = new RandomAccessFile(new File(repoPath + resource.getUrl()), "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdffile = new PDFFile(buf); resourceInfo.setNumOfPages(pdffile.getNumPages()); resourceRepository.save(resourceInfo); resource.setResourceInfo(resourceInfo); resourceRepository.save(resource); s3ResourceApiHandler.uploadResourceFolder(resource); } catch (Exception ex) { logger.error("Error while generating thumbnail ", ex); s3ResourceApiHandler.uploadResourceFolderWithNewSession(resourceGooruOid); } } }; }
From source file:com.googlecode.onevre.utils.ServerClassLoader.java
/** * Creates a new ServerClassLoader/*from w ww. j a v a 2 s . c om*/ * @param parent The parent class loader * @param localCacheDirectory The directory to cache files to * @param remoteServer The URL of the remote server */ public ServerClassLoader(ClassLoader parent, File localCacheDirectory, URL remoteServer) { super(parent); this.localCacheDirectory = localCacheDirectory; this.localLibDirectory = new File(localCacheDirectory, LIB_DIR); File versionFile = new File(localCacheDirectory, "Version"); boolean versionCorrect = false; if (!localCacheDirectory.exists()) { localCacheDirectory.mkdirs(); } else { if (versionFile.exists()) { try { BufferedReader reader = new BufferedReader(new FileReader(versionFile)); String version = reader.readLine(); reader.close(); versionCorrect = Defaults.PAG_VERSION.equals(version); log.info(version + " == " + Defaults.PAG_VERSION + " = " + versionCorrect); } catch (IOException e) { // Do Nothing } } try { FileInputStream input = new FileInputStream(new File(localCacheDirectory, INDEX)); DataInputStream cacheFile = new DataInputStream(input); FileChannel channel = input.getChannel(); while (channel.position() < channel.size()) { URL url = new URL(cacheFile.readUTF()); String file = cacheFile.readUTF(); if (versionCorrect && url.getHost().equals(remoteServer.getHost()) && (url.getPort() == remoteServer.getPort())) { File jar = new File(localCacheDirectory, file); if (jar.exists()) { indexJar(url, jar); CHECKED.put(url, true); } } } input.close(); } catch (FileNotFoundException e) { // Do Nothing - cache will be recreated later } catch (IOException e) { // Do Nothing - as above } } localLibDirectory.mkdirs(); try { PrintWriter writer = new PrintWriter(versionFile); writer.println(Defaults.PAG_VERSION); writer.close(); } catch (IOException e) { e.printStackTrace(); } this.remoteServer = remoteServer; }
From source file:com.wwpass.connection.WWPassConnection.java
private static PKCS8EncodedKeySpec readKeyFile(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); try {//www.j a va 2 s . com FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); String pem = Charset.defaultCharset().decode(bb).toString(); pem = pem.replaceFirst("-----BEGIN (RSA )?PRIVATE KEY-----\r?\n?", "") .replace("-----END (RSA )?PRIVATE KEY-----", ""); Base64 dec1 = new Base64(); byte[] encoded = dec1.decode(pem); return new PKCS8EncodedKeySpec(encoded); } finally { stream.close(); } }
From source file:org.apache.nifi.file.FileUtils.java
/** * Copies the given source file to the given destination file. The given * destination will be overwritten if it already exists. * * @param source//from w ww.j a v a 2 s .co m * @param destination * @param lockInputFile if true will lock input file during copy; if false * will not * @param lockOutputFile if true will lock output file during copy; if false * will not * @param move if true will perform what is effectively a move operation * rather than a pure copy. This allows for potentially highly efficient * movement of the file but if not possible this will revert to a copy then * delete behavior. If false, then the file is copied and the source file is * retained. If a true rename/move occurs then no lock is held during that * time. * @param logger if failures occur, they will be logged to this logger if * possible. If this logger is null, an IOException will instead be thrown, * indicating the problem. * @return long number of bytes copied * @throws FileNotFoundException if the source file could not be found * @throws IOException * @throws SecurityException if a security manager denies the needed file * operations */ public static long copyFile(final File source, final File destination, final boolean lockInputFile, final boolean lockOutputFile, final boolean move, final Logger logger) throws FileNotFoundException, IOException { FileInputStream fis = null; FileOutputStream fos = null; FileLock inLock = null; FileLock outLock = null; long fileSize = 0L; if (!source.canRead()) { throw new IOException("Must at least have read permission"); } if (move && source.renameTo(destination)) { fileSize = destination.length(); } else { try { fis = new FileInputStream(source); fos = new FileOutputStream(destination); final FileChannel in = fis.getChannel(); final FileChannel out = fos.getChannel(); if (lockInputFile) { inLock = in.tryLock(0, Long.MAX_VALUE, true); if (null == inLock) { throw new IOException("Unable to obtain shared file lock for: " + source.getAbsolutePath()); } } if (lockOutputFile) { outLock = out.tryLock(0, Long.MAX_VALUE, false); if (null == outLock) { throw new IOException( "Unable to obtain exclusive file lock for: " + destination.getAbsolutePath()); } } long bytesWritten = 0; do { bytesWritten += out.transferFrom(in, bytesWritten, TRANSFER_CHUNK_SIZE_BYTES); fileSize = in.size(); } while (bytesWritten < fileSize); out.force(false); FileUtils.closeQuietly(fos); FileUtils.closeQuietly(fis); fos = null; fis = null; if (move && !FileUtils.deleteFile(source, null, 5)) { if (logger == null) { FileUtils.deleteFile(destination, null, 5); throw new IOException("Could not remove file " + source.getAbsolutePath()); } else { logger.warn( "Configured to delete source file when renaming/move not successful. However, unable to delete file at: " + source.getAbsolutePath()); } } } finally { FileUtils.releaseQuietly(inLock); FileUtils.releaseQuietly(outLock); FileUtils.closeQuietly(fos); FileUtils.closeQuietly(fis); } } return fileSize; }
From source file:org.apache.hadoop.hdfs.server.namenode.TestListCorruptFileBlocks.java
/** check if nn.getCorruptFiles() returns a file that has corrupted blocks */ @Test(timeout = 300000)/* ww w.ja va2 s .co m*/ public void testListCorruptFilesCorruptedBlock() throws Exception { MiniDFSCluster cluster = null; Random random = new Random(); try { Configuration conf = new HdfsConfiguration(); conf.setInt(DFSConfigKeys.DFS_DATANODE_DIRECTORYSCAN_INTERVAL_KEY, 1); // datanode scans directories conf.setInt(DFSConfigKeys.DFS_BLOCKREPORT_INTERVAL_MSEC_KEY, 3 * 1000); // datanode sends block reports // Set short retry timeouts so this test runs faster conf.setInt(DFSConfigKeys.DFS_CLIENT_RETRY_WINDOW_BASE, 10); cluster = new MiniDFSCluster.Builder(conf).build(); FileSystem fs = cluster.getFileSystem(); // create two files with one block each DFSTestUtil util = new DFSTestUtil.Builder().setName("testCorruptFilesCorruptedBlock").setNumFiles(2) .setMaxLevels(1).setMaxSize(512).build(); util.createFiles(fs, "/srcdat10"); // fetch bad file list from namenode. There should be none. final NameNode namenode = cluster.getNameNode(); Collection<FSNamesystem.CorruptFileBlockInfo> badFiles = namenode.getNamesystem() .listCorruptFileBlocks("/", null); assertTrue("Namenode has " + badFiles.size() + " corrupt files. Expecting None.", badFiles.size() == 0); // Now deliberately corrupt one block String bpid = cluster.getNamesystem().getBlockPoolId(); File storageDir = cluster.getInstanceStorageDir(0, 1); File data_dir = MiniDFSCluster.getFinalizedDir(storageDir, bpid); assertTrue("data directory does not exist", data_dir.exists()); List<File> metaFiles = MiniDFSCluster.getAllBlockMetadataFiles(data_dir); assertTrue("Data directory does not contain any blocks or there was an " + "IO error", metaFiles != null && !metaFiles.isEmpty()); File metaFile = metaFiles.get(0); RandomAccessFile file = new RandomAccessFile(metaFile, "rw"); FileChannel channel = file.getChannel(); long position = channel.size() - 2; int length = 2; byte[] buffer = new byte[length]; random.nextBytes(buffer); channel.write(ByteBuffer.wrap(buffer), position); file.close(); LOG.info("Deliberately corrupting file " + metaFile.getName() + " at offset " + position + " length " + length); // read all files to trigger detection of corrupted replica try { util.checkFiles(fs, "/srcdat10"); } catch (BlockMissingException e) { System.out.println("Received BlockMissingException as expected."); } catch (IOException e) { assertTrue("Corrupted replicas not handled properly. Expecting BlockMissingException " + " but received IOException " + e, false); } // fetch bad file list from namenode. There should be one file. badFiles = namenode.getNamesystem().listCorruptFileBlocks("/", null); LOG.info("Namenode has bad files. " + badFiles.size()); assertTrue("Namenode has " + badFiles.size() + " bad files. Expecting 1.", badFiles.size() == 1); util.cleanup(fs, "/srcdat10"); } finally { if (cluster != null) { cluster.shutdown(); } } }
From source file:voldemort.store.cachestore.impl.ChannelStore.java
private boolean checkSignature(FileChannel channel) throws IOException { ByteBuffer intBytes = ByteBuffer.allocate(OFFSET); if (channel.size() == 0) { intBytes.putInt(MAGIC);/*from w w w. j a v a2 s . co m*/ intBytes.flip(); channel.write(intBytes); } else { channel.read(intBytes); intBytes.rewind(); int s = intBytes.getInt(); if (s != MAGIC) throw new StoreException( "Header mismatch expect " + Integer.toHexString(MAGIC) + " read " + Integer.toHexString(s)); } return true; }
From source file:esg.node.components.monitoring.MonitorDAO.java
private void loadDiskInfoResource() { disk_info_dsroot_pat = Pattern.compile("thredds_dataset_roots\\s*=\\s*(.*?)\\s*\\w+\\s*?="); disk_info_dsroot_keyval_pat = Pattern.compile("\\s*(\\w+)\\s*\\|\\s*(\\S+)\\s*"); String filename = props.getProperty("monitor.esg.ini", System.getenv().get("ESG_USER_HOME") + "/.esgcet/esg.ini"); File iniFile = new File(filename); if (!iniFile.exists()) { log.warn("ESG publisher config file [" + filename + "] not found! Cannot provide disk info!"); return;/*from www . ja va 2s . c o m*/ } log.debug("Scanning for drives specified in: " + filename); try { FileInputStream fis = new FileInputStream(iniFile); FileChannel fc = fis.getChannel(); disk_info_byte_buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size()); } catch (FileNotFoundException e) { log.error(e); } catch (IOException e) { log.error(e); } }