示例#1
0
 public void write(FileOutputStream fos) throws IOException {
   FileChannel chan = fos.getChannel();
   // Create ByteBuffer for header in case the start of our
   // ByteBuffer isn't actually memory-mapped
   ByteBuffer hdr = ByteBuffer.allocate(Header.writtenSize());
   hdr.order(ByteOrder.LITTLE_ENDIAN);
   header.write(hdr);
   hdr.rewind();
   chan.write(hdr);
   buf.position(Header.writtenSize());
   chan.write(buf);
   chan.force(true);
   chan.close();
 }
示例#2
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");
  }
示例#3
0
  public static void main(String[] argv) {
    if (argv.length != 3) {
      usage();
    }

    String tempFile = argv[0];
    String testFile = argv[1];
    int fileSize = Integer.valueOf(argv[2]).intValue();
    int exitcode = 0;
    int numRead;
    int numThisBuf;
    int numWritten;

    if ((fileSize <= 0) || (fileSize % 4096 != 0)) {
      System.out.println("Error: size is not a multiple of 4096!!!!!!");
      System.out.println();
      usage();
    }

    try {
      int bufSize = 4096;
      byte[] inBytes = new byte[bufSize];
      byte[] outBytes = new byte[bufSize];
      Random ioRandom = new Random(2006);
      ioRandom.nextBytes(outBytes);
      ByteBuffer inBuf = ByteBuffer.allocate(bufSize);
      ByteBuffer outBuf = ByteBuffer.wrap(outBytes);

      //
      // Loop forever
      //
      while (true) {
        //
        // Write the temporary file
        //
        FileOutputStream fos = new FileOutputStream(tempFile);
        FileChannel foc = fos.getChannel();
        numWritten = 0;
        while (numWritten < fileSize) {
          outBuf.clear(); // sets limit to capacity & position to zero
          while (outBuf.hasRemaining()) {
            numWritten += foc.write(outBuf);
          }
        }

        //
        // Move to permanent location
        //
        FileChannel srcChannel = new FileInputStream(tempFile).getChannel();
        FileChannel dstChannel = new FileOutputStream(testFile).getChannel();
        dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
        srcChannel.close();
        dstChannel.close();
        boolean success = (new File(tempFile)).delete();
        if (!success) {
          System.out.println("Warning: unable to delete temporary file");
        }

        //
        // Read and compare
        //
        FileInputStream fis = new FileInputStream(testFile);
        FileChannel fic = fis.getChannel();

        for (numRead = 0, numThisBuf = 0; numRead < fileSize; numThisBuf = 0) {
          inBuf.rewind(); // Set the buffer position to 0
          numThisBuf = fic.read(inBuf);
          while (numThisBuf < bufSize) {
            numThisBuf += fic.read(inBuf);
          }
          numRead += bufSize;
          inBuf.rewind(); // Set the buffer position to 0
          inBuf.get(inBytes);
          boolean same = Arrays.equals(inBytes, outBytes);
          if (same = false) {
            System.out.println("Data read does not equal data written at " + numRead + " bytes");
          }
        }
      }

    } catch (FileNotFoundException fnfe) {
      fnfe.printStackTrace(System.err);
      exitcode = 1;
      // break;
    } catch (SecurityException se) {
      se.printStackTrace(System.err);
      exitcode = 1;
      // break;
    } catch (Throwable t) {
      t.printStackTrace(System.err);
      exitcode = 1;
    }

    System.exit(exitcode);
  }
示例#4
0
 @Override
 public void run() {
   try {
     if (!picDirectory.exists()) picDirectory.mkdirs();
     File csvPath = csvFile.getParentFile();
     if ((csvPath != null) && !csvPath.exists()) csvPath.mkdirs();
     PrintWriter csvWrite = new PrintWriter(csvFile);
     for (int i = 0; i < id.length; i++) {
       progressBar.setValue(i * 100 / id.length);
       csvWrite.print("'" + id[i] + "'");
       Detail info = new Detail(frame.database, id[i]);
       for (int x = 0; x < 7; x++)
         for (int y = 0; y < 7; y++) {
           String data = info.get(List.COLUMN_NAME[x][y]);
           switch (List.COLUMN_TYPE[x][y]) {
             case 1:
               data = "'" + data + "'";
               break;
             case 2:
               if (data == null) data = "null";
               break;
             case 3:
               if (data == null) data = "0000-00-00";
               data = "'" + data + "'";
               break;
             case 4:
               data = "'" + data + "'";
           }
           csvWrite.print("," + data);
         }
       csvWrite.println();
       String picAddress = info.get("pic");
       info.close();
       if (picAddress.length() == 32) {
         ReadableByteChannel url =
             Channels.newChannel(
                 new URL(
                         "http://"
                             + Configure.webserverAddress
                             + "/"
                             + Configure.picDirectory
                             + picAddress.substring(0, picAddress.length() - 5)
                             + "/"
                             + picAddress.substring(picAddress.length() - 5)
                             + ".jpg")
                     .openStream());
         FileOutputStream outStream =
             new FileOutputStream(picDirectory.getPath() + "/" + id[i] + ".jpg");
         FileChannel out = outStream.getChannel();
         ByteBuffer buffer = ByteBuffer.allocate(10000);
         while (url.read(buffer) != -1) {
           buffer.flip();
           out.write(buffer);
           buffer.clear();
         }
         out.close();
         outStream.close();
         url.close();
       }
     }
     csvWrite.close();
     progressBar.setValue(100);
     finish();
   } catch (Exception e) {
     JOptionPane.showMessageDialog(Port.this, "导出失败!", "错误", JOptionPane.ERROR_MESSAGE);
     dispose();
   }
 }