Exemplo n.º 1
0
 public static void main(String[] args) throws Exception {
   if (args.length != 2) {
     System.out.println("arguments: sourcefile destfile");
     System.exit(1);
   }
   FileChannel in = new FileInputStream(args[0]).getChannel(),
       out = new FileOutputStream(args[1]).getChannel();
   in.transferTo(0, in.size(), out);
   // Or:
   // out.transferFrom(in, 0, in.size());
 }
Exemplo n.º 2
0
 /**
  * copies file "in" to file "out"
  *
  * @param in
  * @param out
  * @throws java.io.IOException
  */
 public static boolean copy(File in, File out) {
   Boolean returnVal = true;
   try {
     FileChannel inChannel = new FileInputStream(in).getChannel();
     FileChannel outChannel = new FileOutputStream(out).getChannel();
     try {
       inChannel.transferTo(0, inChannel.size(), outChannel);
     } catch (IOException e) {
       returnVal = false;
     } finally {
       if (inChannel != null) inChannel.close();
       if (outChannel != null) outChannel.close();
     }
   } catch (Exception ea) {
     returnVal = false;
   }
   return returnVal;
 }
Exemplo n.º 3
0
  public static void copyFileNIO(String filenameIn, String filenameOut, long kbchunks)
      throws IOException {

    FileInputStream in = new FileInputStream(filenameIn);
    FileChannel inChannel = in.getChannel();

    FileOutputStream out = new FileOutputStream(filenameOut);
    FileChannel outChannel = out.getChannel();

    long size = inChannel.size();
    // outChannel.position(size-2);
    // outChannel.write(ByteBuffer.allocate(1));
    // outChannel.position(0);

    if (debug)
      System.out.println(
          "read " + filenameIn + " len = " + size + " out starts at=" + outChannel.position());

    long start = System.currentTimeMillis();
    long done = 0;
    while (done < size) {
      long need = Math.min(kbchunks * 1000, size - done);
      done += inChannel.transferTo(done, need, outChannel);
    }

    outChannel.close();
    inChannel.close();

    double took = .001 * (System.currentTimeMillis() - start);
    if (debug) System.out.println(" write file= " + filenameOut + " len = " + size);

    double rate = size / took / (1000 * 1000);
    System.out.println(
        " copyFileNIO("
            + kbchunks
            + " kb chunk) took = "
            + took
            + " sec; rate = "
            + rate
            + "Mb/sec");
  }
Exemplo n.º 4
0
  /*
   * Method used to move a file from sourceFile to destFile
   */
  public static void moveFile(File sourceFile, File destFile) throws IOException {
    FileChannel source = null;
    FileChannel destination = null;

    try {
      source = new FileInputStream(sourceFile).getChannel();
      destination = new FileOutputStream(destFile).getChannel();

      long count = 0;
      long size = source.size();
      source.transferTo(count, size, destination);
    } finally {
      if (source != null) {
        source.close();
      }

      if (destination != null) {
        destination.close();
      }
    }
  }
Exemplo n.º 5
0
  public void onWrite(SelectionKey key) throws Exception {

    String accepts = req.headerString(Accept$2dEncoding);
    String ceString = null;
    String finalFname = scrub(rootPath + "/./" + req.path());
    file = new File(finalFname);
    if (null != accepts) {
      //              String accepts = UTF8.decode((ByteBuffer)
      // addHeaderInterest.clear().limit(ints[1]).position(ints[0])).toString().trim();
      for (CompressionTypes compType : CompressionTypes.values()) {
        if (accepts.contains(compType.name())) {
          File f = new File(finalFname + "." + compType.suffix);
          if (f.isFile() && f.canRead()) {

            if (BlobAntiPatternObject.DEBUG_SENDJSON)
              System.err.println("sending compressed archive: " + f.getAbsolutePath());
            ceString = (compType.name());
            file = f;
            break;
          }
        }
      }
    }
    boolean send200 = false;
    try {
      send200 = file.canRead() && file.isFile();
    } finally {

    }

    if (send200) {
      final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
      final long total = randomAccessFile.length();
      final FileChannel fileChannel = randomAccessFile.getChannel();

      String substring = finalFname.substring(finalFname.lastIndexOf('.') + 1);
      MimeType mimeType = MimeType.valueOf(substring);
      long length = randomAccessFile.length();

      final HttpResponse responseHeader = new Rfc822HeaderState().$res();

      responseHeader
          .status(HttpStatus.$200)
          .headerString(Content$2dType, (null == mimeType ? MimeType.bin : mimeType).contentType)
          .headerString(Content$2dLength, String.valueOf(length));
      if (null != ceString) responseHeader.headerString(Content$2dEncoding, ceString);
      ByteBuffer response = (ByteBuffer) responseHeader.as(ByteBuffer.class);
      int write = channel.write(response);
      final int sendBufferSize = BlobAntiPatternObject.getSendBufferSize();
      final long[] progress = {fileChannel.transferTo(0, sendBufferSize, channel)};
      key.interestOps(OP_WRITE | OP_CONNECT);
      key.selector().wakeup();
      key.attach(
          new Impl() {

            public void onWrite(SelectionKey key) throws Exception {
              long remaining = total - progress[0];
              progress[0] +=
                  fileChannel.transferTo(progress[0], min(sendBufferSize, remaining), channel);
              remaining = total - progress[0];
              if (0 == remaining) {
                fileChannel.close();
                randomAccessFile.close();
                key.selector().wakeup();
                key.interestOps(OP_READ);
                key.attach(null);
              }
            }
          });
    } else {
      key.selector().wakeup();
      key.interestOps(OP_WRITE)
          .attach(
              new Impl() {

                public void onWrite(SelectionKey key) throws Exception {

                  String response = "HTTP/1.1 404 Not Found\n" + "Content-Length: 0\n\n";
                  System.err.println("!!! " + file.getAbsolutePath());
                  int write = channel.write(UTF8.encode(response));
                  key.selector().wakeup();
                  key.interestOps(OP_READ).attach(null);
                }
              });
    }
  }
Exemplo n.º 6
0
 /*
  * Perform a FileChannel.TransferTo on the socket channel.
  */
 long transferTo(FileChannel fc, long pos, long len) throws IOException {
   return fc.transferTo(pos, len, sc);
 }