Ejemplo n.º 1
0
 /**
  * Checks if a given file exists and, if not, create it by copying a default template from
  * resources; used to create default conf files.
  *
  * @param file The path of the file that needs to exist
  * @param template The path of the template for the file
  */
 public static void ensureFileExists(final String file, final String template) {
   if (LAUNCHED_FROM_JAR && !Files.exists(Paths.get(file))) {
     if (Debug.on) printDebug("[Meta] " + file + " does not exist: creating a default one.");
     InputStream stream = Meta.class.getResourceAsStream(template);
     if (stream == null) {
       printDebug(
           "[ WARNING ] template for "
               + template
               + " not found. Won't create a default "
               + file
               + ".");
     } else {
       int readBytes;
       byte[] buffer = new byte[4096];
       try (OutputStream outStream = new FileOutputStream(new File(file))) {
         while ((readBytes = stream.read(buffer)) > 0) {
           outStream.write(buffer, 0, readBytes);
         }
         if (Debug.on)
           printDebug("[Meta] created default file " + file + " from " + template + ".");
       } catch (IOException e) {
         e.printStackTrace();
       } finally {
         try {
           stream.close();
         } catch (IOException ignore) {
         }
       }
     }
   } else {
     if (Meta.LAUNCHED_FROM_JAR && Debug.on) printDebug("[Meta] file exists: " + file + ".");
   }
 }
Ejemplo n.º 2
0
  private void uploadFile(String relPath, Path fullPath) throws DropboxException, IOException {
    if (!fullPath.toFile().exists()) return;

    /*if(api.metadata(relPath, 1, null, false, null).rev == MetaHandler.getRev(relPath)){
    	logger.debug("File "+relPath+" not changed");
    	return;
    }*/

    VOSync.debug("Uploading " + relPath);

    String rev = MetaHandler.getRev(relPath);

    InputStream inp = new FileInputStream(fullPath.toFile());
    Entry fileEntry = api.putFile(relPath, inp, fullPath.toFile().length(), rev, null);
    inp.close();

    Path destFilePath =
        FileSystems.getDefault()
            .getPath(fullPath.toFile().getParentFile().getPath(), fileEntry.fileName());

    MetaHandler.setFile(relPath, destFilePath.toFile(), fileEntry.rev);

    logger.debug(relPath + " put to db");

    // if the file was renamed, move the file on disk and download the current from server
    if (!fileEntry.fileName().equals(fullPath.toFile().getName())) {
      logger.error(fileEntry.fileName() + " != " + fullPath.toFile().getName());
      fullPath.toFile().renameTo(destFilePath.toFile());
    }
  }
Ejemplo n.º 3
0
  public static long checksumBufferedInputStream(Path filename) throws IOException {
    try (InputStream in = new BufferedInputStream(Files.newInputStream(filename))) {
      CRC32 crc = new CRC32();

      int c;
      while ((c = in.read()) != -1) crc.update(c);
      return crc.getValue();
    }
  }
Ejemplo n.º 4
0
 void copy(InputStream in, File dest) throws IOException {
   dest.getParentFile().mkdirs();
   OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
   try {
     byte[] data = new byte[8192];
     int n;
     while ((n = in.read(data, 0, data.length)) > 0) out.write(data, 0, n);
   } finally {
     out.close();
     in.close();
   }
 }
Ejemplo n.º 5
0
  // computes simple hash of the given file
  static int computeHash(Path file) throws IOException {
    int h = 0;

    try (InputStream in = newInputStream(file)) {
      byte[] buf = new byte[1024];
      int n;
      do {
        n = in.read(buf);
        for (int i = 0; i < n; i++) {
          h = 31 * h + (buf[i] & 0xff);
        }
      } while (n > 0);
    }
    return h;
  }
Ejemplo n.º 6
0
  static void testCopyInputStreamToFile(int size) throws IOException {
    Path tmpdir = createTempDirectory("blah");
    Path source = tmpdir.resolve("source");
    Path target = tmpdir.resolve("target");
    try {
      boolean testReplaceExisting = rand.nextBoolean();

      // create source file
      byte[] b = new byte[size];
      rand.nextBytes(b);
      write(source, b);

      // target file might already exist
      if (testReplaceExisting && rand.nextBoolean()) {
        write(target, new byte[rand.nextInt(512)]);
      }

      // copy from stream to file
      InputStream in = new FileInputStream(source.toFile());
      try {
        long n;
        if (testReplaceExisting) {
          n = copy(in, target, StandardCopyOption.REPLACE_EXISTING);
        } else {
          n = copy(in, target);
        }
        assertTrue(in.read() == -1); // EOF
        assertTrue(n == size);
        assertTrue(size(target) == size);
      } finally {
        in.close();
      }

      // check file
      byte[] read = readAllBytes(target);
      assertTrue(Arrays.equals(read, b));

    } finally {
      deleteIfExists(source);
      deleteIfExists(target);
      delete(tmpdir);
    }
  }
Ejemplo n.º 7
0
Archivo: IO.java Proyecto: nremond/boon
  public static byte[] input(InputStream inputStream) {

    try (InputStream is = inputStream) {

      ByteBuf buf = ByteBuf.create(DEFAULT_BUFFER_SIZE);
      byte[] bytes = new byte[DEFAULT_BUFFER_SIZE];

      int read = -2;

      while (read != -1) {

        read = inputStream.read(bytes);

        if (read == DEFAULT_BUFFER_SIZE) {
          buf.add(bytes);
        } else if (read > 0) {
          buf.add(bytes, read);
        }
      }
      return buf.toBytes();
    } catch (Exception ex) {
      return Exceptions.handle(byte[].class, ex);
    }
  }
Ejemplo n.º 8
0
 public String generateMD5(File f) throws IOException {
   InputStream fis = new BufferedInputStream(new FileInputStream(f));
   String s = this.generateMD5(fis);
   fis.close();
   return s;
 }