List of usage examples for java.nio.file Files size
public static long size(Path path) throws IOException
From source file:com.spectralogic.ds3client.integration.PutJobManagement_Test.java
@Test public void truncatePutJob() throws Exception { final int testTimeOutSeconds = 5; final String book1 = "beowulf.txt"; final Path objPath1 = ResourceUtils.loadFileResource(RESOURCE_BASE_NAME + book1); final Ds3Object obj1 = new Ds3Object(book1, Files.size(objPath1)); final Ds3Object obj2 = new Ds3Object("place_holder", 5000000); try {//w ww .java2s .c o m final Ds3ClientHelpers.Job putJob = HELPERS.startWriteJob(BUCKET_NAME, Lists.newArrayList(obj1, obj2)); final UUID jobId = putJob.getJobId(); final SeekableByteChannel book1Channel = new ResourceObjectPutter(RESOURCE_BASE_NAME) .buildChannel(book1); client.putObject( new PutObjectRequest(BUCKET_NAME, book1, book1Channel, jobId, 0, Files.size(objPath1))); ABMTestHelper.waitForJobCachedSizeToBeMoreThanZero(jobId, client, 20); final TruncateJobSpectraS3Response truncateJobSpectraS3Response = client .truncateJobSpectraS3(new TruncateJobSpectraS3Request(jobId.toString())); assertThat(truncateJobSpectraS3Response.getStatusCode(), is(204)); final GetJobSpectraS3Response truncatedJob = client .getJobSpectraS3(new GetJobSpectraS3Request(jobId.toString())); assertEquals(truncatedJob.getMasterObjectListResult().getOriginalSizeInBytes(), Files.size(objPath1)); } finally { deleteAllContents(client, BUCKET_NAME); } }
From source file:com.spectralogic.ds3client.integration.GetJobManagement_Test.java
@Test public void createReadJobWithBigFile() throws IOException, URISyntaxException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { final String tempPathPrefix = null; final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix); try {/*from w w w .ja v a 2 s . c o m*/ final String DIR_NAME = "largeFiles/"; final String FILE_NAME = "lesmis-copies.txt"; final Path objPath = ResourceUtils.loadFileResource(DIR_NAME + FILE_NAME); final long bookSize = Files.size(objPath); final Ds3Object obj = new Ds3Object(FILE_NAME, bookSize); final Ds3ClientShim ds3ClientShim = new Ds3ClientShim((Ds3ClientImpl) client); final int maxNumBlockAllocationRetries = 1; final int maxNumObjectTransferAttempts = 3; final Ds3ClientHelpers ds3ClientHelpers = Ds3ClientHelpers.wrap(ds3ClientShim, maxNumBlockAllocationRetries, maxNumObjectTransferAttempts); final Ds3ClientHelpers.Job readJob = ds3ClientHelpers.startReadJob(BUCKET_NAME, Arrays.asList(obj)); final AtomicBoolean dataTransferredEventReceived = new AtomicBoolean(false); final AtomicBoolean objectCompletedEventReceived = new AtomicBoolean(false); final AtomicBoolean checksumEventReceived = new AtomicBoolean(false); final AtomicBoolean metadataEventReceived = new AtomicBoolean(false); final AtomicBoolean waitingForChunksEventReceived = new AtomicBoolean(false); final AtomicBoolean failureEventReceived = new AtomicBoolean(false); readJob.attachDataTransferredListener(new DataTransferredListener() { @Override public void dataTransferred(final long size) { dataTransferredEventReceived.set(true); assertEquals(bookSize, size); } }); readJob.attachObjectCompletedListener(new ObjectCompletedListener() { @Override public void objectCompleted(final String name) { objectCompletedEventReceived.set(true); } }); readJob.attachChecksumListener(new ChecksumListener() { @Override public void value(final BulkObject obj, final ChecksumType.Type type, final String checksum) { checksumEventReceived.set(true); assertEquals("0feqCQBgdtmmgGs9pB/Huw==", checksum); } }); readJob.attachMetadataReceivedListener(new MetadataReceivedListener() { @Override public void metadataReceived(final String filename, final Metadata metadata) { metadataEventReceived.set(true); } }); readJob.attachWaitingForChunksListener(new WaitingForChunksListener() { @Override public void waiting(final int secondsToWait) { waitingForChunksEventReceived.set(true); } }); readJob.attachFailureEventListener(new FailureEventListener() { @Override public void onFailure(final FailureEvent failureEvent) { failureEventReceived.set(true); } }); final GetJobSpectraS3Response jobSpectraS3Response = ds3ClientShim .getJobSpectraS3(new GetJobSpectraS3Request(readJob.getJobId())); assertThat(jobSpectraS3Response.getMasterObjectListResult(), is(notNullValue())); readJob.transfer(new FileObjectGetter(tempDirectory)); final File originalFile = ResourceUtils.loadFileResource(DIR_NAME + FILE_NAME).toFile(); final File fileCopiedFromBP = Paths.get(tempDirectory.toString(), FILE_NAME).toFile(); assertTrue(FileUtils.contentEquals(originalFile, fileCopiedFromBP)); assertTrue(dataTransferredEventReceived.get()); assertTrue(objectCompletedEventReceived.get()); assertTrue(checksumEventReceived.get()); assertTrue(metadataEventReceived.get()); assertFalse(waitingForChunksEventReceived.get()); assertFalse(failureEventReceived.get()); } finally { FileUtils.deleteDirectory(tempDirectory.toFile()); } }
From source file:codes.writeonce.maven.plugins.soy.CompileMojo.java
private static void updateDigest(Path root, MessageDigest sourceDigest, Path soyFilePath) throws IOException { sourceDigest.update(soyFilePath.toString().getBytes(TEXT_DIGEST_CHARSET)); final Path soyFile = root.resolve(soyFilePath); sourceDigest.update(Longs.toByteArray(Files.size(soyFile))); sourceDigest.update(Longs.toByteArray(Files.getLastModifiedTime(soyFile).toMillis())); }
From source file:de.ncoder.studipsync.studip.jsoup.JsoupStudipAdapter.java
public boolean hasCookies() { try {/*from w w w . j a v a2s. c om*/ return cookiesPath != null && Files.isRegularFile(cookiesPath) && Files.size(cookiesPath) > 0; } catch (IOException e) { log.warn("Couldn't read cookies from " + cookiesPath + ", prompting for password", e); return false; } }
From source file:com.streamsets.datacollector.io.DataStore.java
/** * Check if the DataStore exists and contains data. * This method will check for the presence of the set of files that can be used to read data from the store. *///from w w w.j a v a 2s .c o m public boolean exists() throws IOException { verifyAndRecover(); return Files.exists(file) && Files.size(file) > 0; }
From source file:software.coolstuff.springframework.owncloud.service.impl.local.OwncloudLocalResourceServiceImpl.java
private OwncloudLocalResourceExtension createOwncloudResourceOf(Path path) { Path rootPath = getRootLocationOfAuthenticatedUser(); Path relativePath = rootPath.toAbsolutePath().relativize(path.toAbsolutePath()); URI href = URI.create(UriComponentsBuilder.fromPath("/").path(relativePath.toString()).toUriString()); String name = path.getFileName().toString(); MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM; if (Files.isDirectory(path)) { href = URI.create(UriComponentsBuilder.fromUri(href).path("/").toUriString()); mediaType = OwncloudUtils.getDirectoryMediaType(); } else {/*ww w. ja va 2s. c o m*/ FileNameMap fileNameMap = URLConnection.getFileNameMap(); String contentType = fileNameMap.getContentTypeFor(path.getFileName().toString()); if (StringUtils.isNotBlank(contentType)) { mediaType = MediaType.valueOf(contentType); } } try { LocalDateTime lastModifiedAt = LocalDateTime.ofInstant(Files.getLastModifiedTime(path).toInstant(), ZoneId.systemDefault()); Optional<String> checksum = checksumService.getChecksum(path); if (Files.isSameFile(rootPath, path)) { name = "/"; checksum = Optional.empty(); } OwncloudLocalResourceExtension resource = OwncloudLocalResourceImpl.builder().href(href).name(name) .eTag(checksum.orElse(null)).mediaType(mediaType).lastModifiedAt(lastModifiedAt).build(); if (Files.isDirectory(path)) { return resource; } return OwncloudLocalFileResourceImpl.fileBuilder().owncloudResource(resource) .contentLength(Files.size(path)).build(); } catch (NoSuchFileException e) { throw new OwncloudResourceNotFoundException(href, getUsername()); } catch (IOException e) { val logMessage = String.format("Cannot create OwncloudResource from Path %s", path); log.error(logMessage, e); throw new OwncloudLocalResourceException(logMessage, e); } }
From source file:codes.thischwa.c5c.impl.LocalConnector.java
@Override public StreamContent download(String backendPath) throws C5CException { Path file = buildRealPath(backendPath); try {// w ww .j a va 2 s . c om InputStream in = new BufferedInputStream(Files.newInputStream(file, StandardOpenOption.READ)); return buildStreamContent(in, Files.size(file)); } catch (FileNotFoundException e) { logger.error("Requested file not exits: {}", file.toAbsolutePath()); throw new FilemanagerException(FilemanagerAction.DOWNLOAD, FilemanagerException.Key.FileNotExists, backendPath); } catch (IOException | SecurityException e) { String msg = String.format("Error while downloading {}: {}", file.getFileName().toFile(), e.getMessage()); logger.error(msg, e); throw new C5CException(FilemanagerAction.DOWNLOAD, msg); } }
From source file:de.digiway.rapidbreeze.server.model.download.Download.java
/** * Returns the currently downloaded size of the file in bytes. * * @return//from w ww.ja va 2 s. co m */ public long getCurrentSize() { try { if (!Files.exists(tempFile)) { if (Files.exists(targetFile)) { return Files.size(targetFile); } return 0; } return Files.size(tempFile); } catch (IOException ex) { throw new IllegalStateException("Cannot retrieve size of temporary file:" + tempFile, ex); } }
From source file:dk.dma.msinm.common.repo.RepositoryService.java
/** * Returns a list of files in the folder specified by the path * @param path the path/*from www .j ava2 s .c om*/ * @return the list of files in the folder specified by the path */ @GET @javax.ws.rs.Path("/list/{folder:.+}") @Produces("application/json;charset=UTF-8") @NoCache public List<RepoFileVo> listFiles(@PathParam("folder") String path) throws IOException { List<RepoFileVo> result = new ArrayList<>(); Path folder = repoRoot.resolve(path); if (Files.exists(folder) && Files.isDirectory(folder)) { // Filter out directories, hidden files, thumbnails and map images DirectoryStream.Filter<Path> filter = file -> Files.isRegularFile(file) && !file.getFileName().toString().startsWith(".") && !file.getFileName().toString().matches(".+_thumb_\\d{1,3}\\.\\w+") && // Thumbnails !file.getFileName().toString().matches("map_\\d{1,3}\\.png"); // Map image try (DirectoryStream<Path> stream = Files.newDirectoryStream(folder, filter)) { stream.forEach(f -> { RepoFileVo vo = new RepoFileVo(); vo.setName(f.getFileName().toString()); vo.setPath(WebUtils.encodeURI(path + "/" + f.getFileName().toString())); vo.setDirectory(Files.isDirectory(f)); try { vo.setUpdated(new Date(Files.getLastModifiedTime(f).toMillis())); vo.setSize(Files.size(f)); } catch (Exception e) { log.trace("Error reading file attribute for " + f); } result.add(vo); }); } } return result; }
From source file:com.emc.ecs.smart.SmartUploader.java
/** * Performs a segmented upload to ECS using the SmartClient and the ECS byte range PUT extensions. The upload * URL will be parsed and the hostname will be enumerated in DNS to see if it contains multiple 'A' records. If * so, those will be used to populate the software load balancer. *///from ww w. java2s . c om private void doSegmentedUpload() { try { long start = System.currentTimeMillis(); fileSize = Files.size(fileToUpload); // Verify md5Save file path is legit. PrintWriter pw = null; try { if (saveMD5 != null) { pw = new PrintWriter(saveMD5); } } catch (IOException e) { System.err.println("Invalid path specified to save local file MD5: " + e.getMessage()); System.exit(3); } // Figure out which segment size to use. if (segmentSize == -1) { if (fileSize >= LARGE_SEGMENT) { segmentSize = LARGE_SEGMENT; } else { segmentSize = SMALL_SEGMENT; } } // Expand the host String host = uploadUrl.getHost(); InetAddress addr = InetAddress.getByName(host); List<String> ipAddresses = new ArrayList<>(); try { ipAddresses = getIPAddresses(host); } catch (NamingException e) { LogMF.warn(l4j, "Could not resolve hostname: {0}: {1}. Using as-is.", host, e); ipAddresses.add(host); } LogMF.info(l4j, "Host {0} resolves to {1}", host, ipAddresses); // Initialize the SmartClient SmartConfig smartConfig = new SmartConfig(ipAddresses.toArray(new String[ipAddresses.size()])); // We don't need to update the host list smartConfig.setHostUpdateEnabled(false); // Configure the load balancer Client pingClient = SmartClientFactory.createStandardClient(smartConfig, new URLConnectionClientHandler()); pingClient.addFilter(new HostnameVerifierFilter(uploadUrl.getHost())); LoadBalancer loadBalancer = smartConfig.getLoadBalancer(); EcsHostListProvider hostListProvider = new EcsHostListProvider(pingClient, loadBalancer, null, null); hostListProvider.setProtocol(uploadUrl.getProtocol()); if (uploadUrl.getPort() != -1) { hostListProvider.setPort(uploadUrl.getPort()); } smartConfig.setHostListProvider(hostListProvider); client = SmartClientFactory.createSmartClient(smartConfig, new URLConnectionClientHandler()); // Add our retry handler client.addFilter(new HostnameVerifierFilter(uploadUrl.getHost())); client.addFilter(new MD5CheckFilter()); client.addFilter(new RetryFilter(retryDelay, retryCount)); // Create a FileChannel for the upload fileChannel = new RandomAccessFile(fileToUpload.toFile(), "r").getChannel(); System.out.printf("Starting upload at %s\n", new Date().toString()); // The first upload is done without a range to create the initial object. doUploadSegment(0); // See how many more segments we have int segmentCount = (int) (fileSize / (long) segmentSize); long remainder = fileSize % segmentSize; if (remainder != 0) { // Additional bytes at end segmentCount++; } if (segmentCount > 1) { // Build a thread pool to upload the segments. ThreadPoolExecutor executor = new ThreadPoolExecutor(threadCount, threadCount, 15, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); for (int i = 1; i < segmentCount; i++) { executor.execute(new SegmentUpload(i)); } // Wait for completion while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if (failed) { // Abort! l4j.warn("Error detected, terminating upload"); executor.shutdownNow(); break; } if (executor.getQueue().isEmpty()) { l4j.info("All tasks complete, awaiting shutdown"); try { executor.shutdown(); executor.awaitTermination(1, TimeUnit.MINUTES); } catch (InterruptedException e) { e.printStackTrace(); } break; } } } // Done! long elapsed = System.currentTimeMillis() - start; printRate(fileSize, elapsed); // Release buffers LogMF.debug(l4j, "buffer count at end: {0}", buffers.size()); buffers = new LinkedList<>(); System.out.printf("\nUpload completed at %s\n", new Date().toString()); // Verify if (verifyUrl != null) { System.out.printf("starting remote MD5...\n"); String objectMD5 = computeObjectMD5(); System.out.printf("Object MD5 = %s\n", objectMD5); System.out.printf("Remote MD5 complete at %s\nStarting local MD5\n", new Date().toString()); // At this point we don't need the clients anymore. l4j.debug("Shutting down SmartClient"); SmartClientFactory.destroy(client); SmartClientFactory.destroy(pingClient); String fileMD5 = standardChecksum ? computeFileMD5Standard() : computeFileMD5(); System.out.printf("\nFile on disk MD5 = %s\n", fileMD5); System.out.printf("Local MD5 complete at %s\n", new Date().toString()); if (!fileMD5.equals(objectMD5)) { System.err.printf("ERROR: file MD5 does not match object MD5! %s != %s", fileMD5, objectMD5); System.exit(10); } if (saveMD5 != null && pw != null) { pw.write(fileMD5); pw.close(); } System.out.printf("\nObject verification passed!\n"); } } catch (IOException e) { e.printStackTrace(); System.exit(4); } }