/**
  * Execute a single filesystem test against the given path.
  *
  * @param path The {@code Path} to test
  * @param test The character code of the test to perform.
  * @return {@code null} if the test passes, or a {@code FileSystemTestFailure} if it fails.
  */
 protected static FileSystemTestFailure testPath(final Path path, char test) {
   switch (test) {
     case 'e':
       return (Files.exists(path))
           ? null
           : new FileSystemTestFailure(path, test, "The path does not exist");
     case 'f':
       return (Files.isRegularFile(path))
           ? null
           : new FileSystemTestFailure(path, test, "The path is not a file");
     case 'd':
       return (Files.isDirectory(path))
           ? null
           : new FileSystemTestFailure(path, test, "The path is not a directory");
     case 'z':
       try {
         if (!Files.isRegularFile(path))
           return new FileSystemTestFailure(path, test, "The path is not a file");
         return (Files.size(path) < 1)
             ? null
             : new FileSystemTestFailure(path, test, "The path is not zero length");
       } catch (IOException ioe) {
         return new FileSystemTestFailure(path, test, "The path could not be read");
       }
     case 's':
       try {
         if (!Files.isRegularFile(path))
           return new FileSystemTestFailure(path, test, "The path is not a file");
         return (Files.size(path) > 0)
             ? null
             : new FileSystemTestFailure(path, test, "The path is a zero length file");
       } catch (IOException ioe) {
         return new FileSystemTestFailure(path, test, "The path could not be read");
       }
     case 'r':
       return (Files.isReadable(path))
           ? null
           : new FileSystemTestFailure(path, test, "The path is not readable");
     case 'w':
       return (Files.isWritable(path))
           ? null
           : new FileSystemTestFailure(path, test, "The path is not writable");
     case 'x':
       return (Files.isExecutable(path))
           ? null
           : new FileSystemTestFailure(path, test, "The path is not executable");
     default:
       throw new IllegalArgumentException("Unrecognized path test: " + test);
   }
 }
  private void sendModules(List<NetData.ModuleRequest> moduleRequestList) {
    for (NetData.ModuleRequest request : moduleRequestList) {
      NetData.ModuleDataHeader.Builder result = NetData.ModuleDataHeader.newBuilder();
      result.setId(request.getModuleId());
      Module module = moduleManager.getEnvironment().get(new Name(request.getModuleId()));
      if (module.isOnClasspath()
          || module.getLocations().size() != 1
          || !Files.isReadable(module.getLocations().get(0))) {
        result.setError("Module not available for download");
      } else {
        Path location = module.getLocations().get(0);
        try {
          result.setVersion(module.getVersion().toString());
          result.setSize(Files.size(location));
          channelHandlerContext
              .getChannel()
              .write(NetData.NetMessage.newBuilder().setModuleDataHeader(result).build());
        } catch (IOException e) {
          logger.error("Error sending module data header", e);
          channelHandlerContext.getChannel().close();
          break;
        }

        try (InputStream stream = new BufferedInputStream(Files.newInputStream(location))) {

          long remainingData = Files.size(location);
          byte[] data = new byte[1024];
          while (remainingData > 0) {
            int nextBlock = (int) Math.min(remainingData, 1024);
            ByteStreams.read(stream, data, 0, nextBlock);
            channelHandlerContext
                .getChannel()
                .write(
                    NetData.NetMessage.newBuilder()
                        .setModuleData(
                            NetData.ModuleData.newBuilder()
                                .setModule(ByteString.copyFrom(data, 0, nextBlock)))
                        .build());
            remainingData -= nextBlock;
          }
        } catch (IOException e) {
          logger.error("Error sending module", e);
          channelHandlerContext.getChannel().close();
          break;
        }
      }
    }
  }
  public static FileInfo pathToFileInfo(Path filePath, Path start) {
    FileInfo fileInfo = new FileInfo();
    if (!Files.exists(filePath)) {
      fileInfo.setName("Unknown");
      return fileInfo;
    }

    fileInfo.setName(start.relativize(filePath).toString());

    if (Files.isDirectory(filePath)) {
      fileInfo.setDir(true);
    } else {
      fileInfo.setDir(false);
      try {
        fileInfo.setSize(Files.size(filePath));
      } catch (IOException e) {
        fileInfo.setSize(0);
      }
      try {
        fileInfo.setMimeType(Files.probeContentType(filePath));
      } catch (IOException e) {
        fileInfo.setMimeType("application/octet-stream");
      }
      if (fileInfo.getMimeType() == null) {
        fileInfo.setMimeType("application/octet-stream");
      }
    }

    return fileInfo;
  }
 public PathContentProvider(Path filePath, int bufferSize) throws IOException {
   if (!Files.isRegularFile(filePath)) throw new NoSuchFileException(filePath.toString());
   if (!Files.isReadable(filePath)) throw new AccessDeniedException(filePath.toString());
   this.filePath = filePath;
   this.fileSize = Files.size(filePath);
   this.bufferSize = bufferSize;
 }
Exemple #5
0
  public static Task createDownloadTask(
      ApiSession sess,
      String servicename,
      Path local,
      CommonPath remote,
      Struct storeParams,
      boolean allowResume) {
    DownloadFile work = new DownloadFile();
    work.session = sess;

    RecordStruct params =
        new RecordStruct(
            new FieldStruct("LocalPath", local),
            new FieldStruct("RemotePath", remote),
            new FieldStruct("ServiceName", servicename),
            new FieldStruct("TransferParams", storeParams));

    if (allowResume && Files.exists(local)) {
      try {
        params.setField("Offset", Files.size(local));
      } catch (IOException x) {
        Logger.error("Unable to get file size for: " + local);
        return null;
      }
    }

    return new Task()
        .withTitle("Download file " + local)
        .withWork(work)
        .withSubContext()
        .withParams(params)
        .withTimeout(1)
        .withDeadline(0);
  }
 /*
  * This will create a project and checkin the AC11 IFC file
  */
 @Test
 public void testUpload() {
   try {
     ServiceMap serviceMap = bimServer.getServiceFactory().get(AccessMethod.INTERNAL);
     ServiceInterface service = serviceMap.get(ServiceInterface.class);
     serviceMap.get(AuthInterface.class).login(username, password);
     SProject project =
         serviceMap.getServiceInterface().addProject("test " + new Random().nextInt(), "ifc4");
     Path sourceFile = TestFile.AC11.getFile();
     service.checkin(
         project.getOid(),
         "test",
         -1L,
         Files.size(sourceFile),
         "test",
         new DataHandler(new FileDataSource(sourceFile.toFile())),
         false,
         true); // TODO
   } catch (ServiceException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } catch (PublicInterfaceNotFoundException e) {
     e.printStackTrace();
   }
 }
  public static Result processFile(Path prev, Path cur, Path outDir, int num, int gzipFrom)
      throws IOException {
    Result deltaHashes = new Result();
    Path deltaFile = outDir.resolve(cur.getFileName().toString() + ".bpatch");
    deleteIfExists(deltaFile);
    deltaHashes.path = deltaFile;

    boolean isGzipping = num >= gzipFrom;
    try (HashingOutputStream hashingStream =
        new HashingOutputStream(
            Hashing.sha256(),
            new BufferedOutputStream(newOutputStream(deltaFile, StandardOpenOption.CREATE_NEW)))) {
      GZIPOutputStream zipStream = null;
      GDiffWriter writer;
      if (isGzipping) {
        // Just constructing this object writes to the stream.
        zipStream = new GZIPOutputStream(hashingStream);
        writer = new GDiffWriter(zipStream);
      } else {
        writer = new GDiffWriter(hashingStream);
      }
      Delta delta = new Delta();
      deltaHashes.preHash = sha256(readAllBytes(prev));
      delta.compute(prev.toFile(), cur.toFile(), writer);
      if (isGzipping) zipStream.close();
      deltaHashes.patchHash = hashingStream.hash().asBytes();
      deltaHashes.postHash = sha256(readAllBytes(cur));
    }
    long size = Files.size(deltaFile);
    deltaHashes.patchSize = size;
    println("... done: %s   (%.2fkb) %s", deltaFile, size / 1024.0, isGzipping ? "zipped" : "");
    return deltaHashes;
  }
 private long size() {
   try {
     return Files.size(path);
   } catch (IOException e) {
     return -1;
   }
 }
  /**
   * Returns list of files paths with given extension for a storage in HTTP form.
   *
   * @param storageData Storage.
   * @param extension Files extension.
   * @return Returns the map containing pair with file name with given extension for a storage in
   *     HTTP form and size of each file.
   * @throws IOException If {@link IOException} occurs.
   */
  public Map<String, Long> getFilesHttpLocation(StorageData storageData, final String extension)
      throws IOException {
    Path storagePath = getStoragePath(storageData);
    if (storagePath == null || !Files.isDirectory(storagePath)) {
      return Collections.emptyMap();
    }

    final List<Path> filesPaths = new ArrayList<Path>();
    Files.walkFileTree(
        storagePath,
        new SimpleFileVisitor<Path>() {
          @Override
          public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
              throws IOException {
            if (file.toString().endsWith(extension)) {
              filesPaths.add(file);
            }
            return super.visitFile(file, attrs);
          }
        });

    Map<String, Long> result = new HashMap<String, Long>();
    for (Path path : filesPaths) {
      result.put(getPathAsHttp(path), Files.size(path));
    }

    return result;
  }
Exemple #10
0
 public int run() throws IOException {
   try {
     int b = in.read();
     if (b != -1) {
       dOut.write(b);
       size++;
       return 1;
     } else {
       dOut.close();
       tmpOut.close();
       LongObjectId loid = LongObjectId.fromRaw(dOut.getMessageDigest().digest());
       Path mediaFile = lfsUtil.getMediaFile(loid);
       if (Files.isRegularFile(mediaFile)) {
         long fsSize = Files.size(mediaFile);
         if (fsSize != size) {
           throw new CorruptMediaFile(mediaFile, size, fsSize);
         }
       } else {
         FileUtils.mkdirs(mediaFile.getParent().toFile(), true);
         FileUtils.rename(tmpFile.toFile(), mediaFile.toFile());
       }
       LfsPointer lfsPointer = new LfsPointer(loid, size);
       lfsPointer.encode(out);
       out.close();
       return -1;
     }
   } catch (IOException e) {
     out.close();
     dOut.close();
     tmpOut.close();
     throw e;
   }
 }
Exemple #11
0
 public long getFileSize(Path pathRelativeToProjectRoot) throws IOException {
   Path path = getPathForRelativePath(pathRelativeToProjectRoot);
   if (!Files.isRegularFile(path)) {
     throw new IOException("Cannot get size of " + path + " because it is not an ordinary file.");
   }
   return Files.size(path);
 }
 public final long size() throws IOException {
   if (exists()) {
     return Files.size(getPath());
   } else {
     return -1L;
   }
 }
Exemple #13
0
 public long getFileSize() {
   try {
     return Files.size(path);
   } catch (IOException e) {
   }
   return 0L;
 }
 /**
  * Get file size
  *
  * @param filepath
  * @return size of given file
  */
 public long getFileSize(String filepath) {
   try {
     return Files.size(new File(filepath).toPath());
   } catch (IOException e) {
     return 0;
   }
 }
 private Entry toEntry(Path entryPath, Path basePath, EntryType section) {
   try {
     String path = basePath.relativize(entryPath).toString().replace('\\', '/');
     return new FileEntry(entryPath, path, section, false, Files.size(entryPath));
   } catch (IOException e) {
     throw new UncheckedIOException(e);
   }
 }
 @Override
 public long getSize() {
   try {
     return doPrivilegedIfNeeded(context, IOException.class, () -> Files.size(path));
   } catch (IOException e) {
     return 0;
   }
 }
Exemple #17
0
  public static boolean isValidChecksum(Path filePath) throws IOException {
    if (Files.notExists(filePath)
        || (Files.size(filePath) > PropsValues.SYNC_FILE_CHECKSUM_THRESHOLD_SIZE)) {

      return false;
    }

    return true;
  }
  public static byte[] encodeToBase64(Path path) throws IOException {
    Base64.Encoder encoder = Base64.getEncoder();
    InputStream buffer1 = Files.newInputStream(path);
    long size = Files.size(path);
    byte[] bytes = new byte[Long.valueOf(size).intValue()];

    Streams.readFully(buffer1, bytes);
    return encoder.encode(bytes);
  }
Exemple #19
0
  public void start() throws Exception {

    HttpClient client = vertx.createHttpClient().setPort(8080).setHost("localhost");

    final HttpClientRequest req =
        client.put(
            "/some-url",
            new Handler<HttpClientResponse>() {
              public void handle(HttpClientResponse response) {
                System.out.println("File uploaded " + response.statusCode);
              }
            });

    String filename = "upload/upload.txt";

    // For a non-chunked upload you need to specify size of upload up-front
    req.headers().put("Content-Length", Files.size(Paths.get(filename)));

    // For a chunked upload you don't need to specify size, just do:
    // req.setChunked(true);

    vertx
        .fileSystem()
        .open(
            filename,
            new AsyncResultHandler<AsyncFile>() {
              public void handle(AsyncResult<AsyncFile> ar) {
                final AsyncFile file = ar.result;
                Pump pump = Pump.createPump(file.getReadStream(), req);
                pump.start();

                file.getReadStream()
                    .endHandler(
                        new SimpleHandler() {
                          public void handle() {

                            file.close(
                                new AsyncResultHandler<Void>() {
                                  public void handle(AsyncResult<Void> ar) {
                                    if (ar.exception == null) {
                                      req.end();
                                      System.out.println("Sent request");
                                    } else {
                                      ar.exception.printStackTrace(System.err);
                                    }
                                  }
                                });
                          }
                        });
              }

              public void onException(Exception e) {
                e.printStackTrace();
              }
            });
  }
Exemple #20
0
  public static char[] readCharBuffer(Path path) {
    try {

      long bufSize = Files.size(path);
      return readCharBuffer(Files.newBufferedReader(path, DEFAULT_CHARSET), (int) bufSize);

    } catch (IOException ex) {
      return Exceptions.handle(char[].class, ex);
    }
  }
Exemple #21
0
  public void readFile(String path, Interfaces.Callback<ByteBuffer> cb) {
    Path file = Paths.get(path);
    AsynchronousFileChannel channel = openFileChannel(cb, file);

    if (channel == null) {
      return;
    }

    eventLoop.runInBackground(() -> Files.size(file), makeReader(channel, true, 0, cb));
  }
  @GET
  @Path("{url : .+}")
  public Response get(@PathParam("url") String url) throws IOException {

    java.nio.file.Path path = LocalImageCache.getInstance().resolveLocal(url);
    if (!Files.isReadable(path) || Files.size(path) == 0) {

      if (url.endsWith(".jpg")) {

        String[] groups = RegExp.parseGroups(url, "([\\w]+)/([\\d]+)\\.[\\w]+");
        if (groups != null) {
          String type = groups[0];
          Long downloadableId = Long.parseLong(groups[1]);
          Downloadable instance = DownloadableFactory.getInstance().createInstance(downloadableId);

          if (instance != null) {
            FindDownloadableImageTask instanceTask =
                DynamoObjectFactory.createInstance(FindDownloadableImageTask.class, instance);
            if (instanceTask != null) {
              BackLogProcessor.getInstance().schedule(instanceTask, false);
            }
          }
        }

        InputStream in = this.getClass().getResourceAsStream("/ring.svg");
        return Response.ok(in).header("Content-Type", "image/svg+xml").build();
      }

      return Response.status(404).build();
    } else {

      Calendar calendar = Calendar.getInstance();
      calendar.add(Calendar.DAY_OF_MONTH, 2);

      InputStream in = Files.newInputStream(path, StandardOpenOption.READ);
      return Response.ok(in)
          .header("Content-Length", Files.size(path))
          .header("Content-Type", Files.probeContentType(path))
          .header("Expires", calendar.getTimeInMillis())
          .build();
    }
  }
 public static void main(String[] args) throws IOException {
   Path path = Paths.get(FileService.FILE_WITH_LINES);
   System.out.println("Files.isDirectory = " + Files.isDirectory(path));
   System.out.println("Files.isExecutable = " + Files.isExecutable(path));
   System.out.println("Files.isHidden = " + Files.isHidden(path));
   System.out.println("Files.isReadable = " + Files.isReadable(path));
   System.out.println("Files.isWritable = " + Files.isWritable(path));
   System.out.println("Files.getLastModifiedTime = " + Files.getLastModifiedTime(path));
   System.out.println("Files.getOwner = " + Files.getOwner(path));
   System.out.println("Files.size = " + Files.size(path));
   // change attribute: Files.setAttribute(path, "dos:hidden", false);
 }
 /** downloadメソッドのテスト、クラスUrlDownloaderのテスト。 */
 @Test
 public void testDownload() throws Exception {
   System.out.println("download");
   URL url = new URL("http://nbandroid.org/release72/updates/com-android-ide_common.nbm");
   Path path = FileSystems.getDefault().getPath("R:\\Temp");
   UrlDownloader.download(url, path);
   // TODO: 生成されたテスト・コードを確認し、失敗するデフォルト呼出しを削除します。
   Path fileName = Paths.get(url.getPath()).getFileName();
   Path target = path.resolve(fileName);
   assertEquals(Files.exists(target), true);
   assertEquals(Files.size(target), 36476);
 }
Exemple #25
0
 // Lists the files in the selected directories that match the predefined extensions.
 public static List<File> getFilesFromDisk(Path p) {
   List<File> result = new ArrayList<>();
   try (DirectoryStream<Path> stream =
       Files.newDirectoryStream(p, "*.{wmv,mp4,flv,mov,avi,mkv}")) {
     for (Path entry : stream) {
       result.add(new File(entry.getFileName().toString(), Files.size(entry), null));
     }
   } catch (IOException | DirectoryIteratorException ex) {
     ex.printStackTrace();
   }
   return result;
 }
Exemple #26
0
  // Test borrowed from BytesAndLines
  public void testLines() throws IOException {
    final Charset US_ASCII = Charset.forName("US-ASCII");
    Path tmpfile = Files.createTempFile("blah", "txt");

    try {
      // zero lines
      assertTrue(Files.size(tmpfile) == 0, "File should be empty");
      try (Stream<String> s = Files.lines(tmpfile)) {
        checkLines(s, Collections.emptyList());
      }
      try (Stream<String> s = Files.lines(tmpfile, US_ASCII)) {
        checkLines(s, Collections.emptyList());
      }

      // one line
      List<String> oneLine = Arrays.asList("hi");
      Files.write(tmpfile, oneLine, US_ASCII);
      try (Stream<String> s = Files.lines(tmpfile)) {
        checkLines(s, oneLine);
      }
      try (Stream<String> s = Files.lines(tmpfile, US_ASCII)) {
        checkLines(s, oneLine);
      }

      // two lines using platform's line separator
      List<String> twoLines = Arrays.asList("hi", "there");
      Files.write(tmpfile, twoLines, US_ASCII);
      try (Stream<String> s = Files.lines(tmpfile)) {
        checkLines(s, twoLines);
      }
      try (Stream<String> s = Files.lines(tmpfile, US_ASCII)) {
        checkLines(s, twoLines);
      }

      // MalformedInputException
      byte[] bad = {(byte) 0xff, (byte) 0xff};
      Files.write(tmpfile, bad);
      try (Stream<String> s = Files.lines(tmpfile)) {
        checkMalformedInputException(s);
      }
      try (Stream<String> s = Files.lines(tmpfile, US_ASCII)) {
        checkMalformedInputException(s);
      }

      // NullPointerException
      checkNullPointerException(() -> Files.lines(null));
      checkNullPointerException(() -> Files.lines(null, US_ASCII));
      checkNullPointerException(() -> Files.lines(tmpfile, null));

    } finally {
      Files.delete(tmpfile);
    }
  }
 public ImmutableTranslogReader openReader(Path path, long id) throws IOException {
   FileChannel channel = FileChannel.open(path, StandardOpenOption.READ);
   try {
     final ChannelReference raf = new ChannelReference(path, id, channel, null);
     ImmutableTranslogReader reader =
         ImmutableTranslogReader.open(
             raf, new Checkpoint(Files.size(path), TranslogReader.UNKNOWN_OP_COUNT, id), null);
     channel = null;
     return reader;
   } finally {
     IOUtils.close(channel);
   }
 }
  @Test
  public void
      cuandoSePasaUnaListaDeDosArchivosElProcesadorOnDemandDebeGenerarDosArchivosDeSalidaConTamanoMayorACero()
          throws IOException {

    List<String> listaDeArchivosAProcesar = new ArrayList<>();
    listaDeArchivosAProcesar.add(DIRECTORIO_CON_DOS_ZIPS + "/CSVConTresLineas.zip");
    listaDeArchivosAProcesar.add(DIRECTORIO_CON_DOS_ZIPS + "/CSVConUnaLinea.zip");

    ProcesadorEstadistico procesadorEstadistico = new ProcesadorEstadistico();
    procesadorEstadistico.procesarModoDaemon(listaDeArchivosAProcesar);
    Path reporteCSVConTresLineas = Paths.get("reportes/CSVConTresLineas.salida.yml");
    Path reporteCSVConUnaLinea = Paths.get("reportes/CSVConUnaLinea.salida.yml");

    Assert.assertTrue(Files.size(reporteCSVConTresLineas) > 0);
    Assert.assertTrue(Files.size(reporteCSVConUnaLinea) > 0);

    volverArchivoAlOrigen(
        "archivosProcesados/CSVConTresLineas.zip",
        DIRECTORIO_CON_DOS_ZIPS + "/CSVConTresLineas.zip");
    volverArchivoAlOrigen(
        "archivosProcesados/CSVConUnaLinea.zip", DIRECTORIO_CON_DOS_ZIPS + "/CSVConUnaLinea.zip");
  }
  @Test
  public void
      cuandoElDirectorioDeEntradaTieneContenidoElProcesadorOnDemandDebeGenerarUnArchivoDeSalidaConTamanoMayorACero()
          throws IOException {

    ProcesadorEstadistico procesadorEstadistico = new ProcesadorEstadistico();
    procesadorEstadistico.procesarModoOnDemand(DIRECTORIO_CON_UN_ZIP);
    Path reporte = Paths.get("reportes/salidaUnica.yml");

    Assert.assertTrue(Files.size(reporte) > 0);

    volverArchivoAlOrigen(
        "archivosProcesados/CSVConUnaLinea.zip", DIRECTORIO_CON_UN_ZIP + "/CSVConUnaLinea.zip");
  }
Exemple #30
0
  @Test
  public void readWritePath() throws Exception {
    Path path = temporaryFolder.newFile().toPath();

    BufferedSink sink = Okio.buffer(Okio.sink(path));
    sink.writeUtf8("Hello, java.nio file!");
    sink.close();
    assertTrue(Files.exists(path));
    assertEquals(21, Files.size(path));

    BufferedSource source = Okio.buffer(Okio.source(path));
    assertEquals("Hello, java.nio file!", source.readUtf8());
    source.close();
  }