Example #1
0
  /**
   * Read the contents of a text file using a memory-mapped byte buffer.
   *
   * <p>A MappedByteBuffer, is simply a special ByteBuffer. MappedByteBuffer maps a region of a file
   * directly in memory. Typically, that region comprises the entire file, although it could map a
   * portion. You must, therefore, specify what part of the file to map. Moreover, as with the other
   * Buffer objects, no constructor exists; you must ask the java.nio.channels.FileChannel for its
   * map() method to get a MappedByteBuffer.
   *
   * <p>Direct buffers allocate their data directly in the runtime environment memory, bypassing the
   * JVM|OS boundary, usually doubling file copy speed. However, they generally cost more to
   * allocate.
   */
  private static String fastStreamCopy(String filename) {
    String s = "";
    FileChannel fc = null;
    try {
      fc = new FileInputStream(filename).getChannel();

      // int length = (int)fc.size();

      MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
      // CharBuffer charBuffer =
      // Charset.forName("ISO-8859-1").newDecoder().decode(byteBuffer);

      // ByteBuffer byteBuffer = ByteBuffer.allocate(length);
      // ByteBuffer byteBuffer = ByteBuffer.allocateDirect(length);
      // CharBuffer charBuffer = byteBuffer.asCharBuffer();

      // CharBuffer charBuffer =
      // ByteBuffer.allocateDirect(length).asCharBuffer();
      /*
       * int size = charBuffer.length(); if (size > 0) { StringBuffer sb =
       * new StringBuffer(size); for (int count=0; count<size; count++)
       * sb.append(charBuffer.get()); s = sb.toString(); }
       *
       * if (length > 0) { StringBuffer sb = new StringBuffer(length); for
       * (int count=0; count<length; count++) {
       * sb.append(byteBuffer.get()); } s = sb.toString(); }
       */
      int size = byteBuffer.capacity();
      if (size > 0) {
        // Retrieve all bytes in the buffer
        byteBuffer.clear();
        byte[] bytes = new byte[size];
        byteBuffer.get(bytes, 0, bytes.length);
        s = new String(bytes);
      }

      fc.close();
    } catch (FileNotFoundException fnfx) {
      System.err.println("File not found: " + fnfx);
    } catch (IOException iox) {
      System.err.println("I/O problems: " + iox);
    } finally {
      if (fc != null) {
        try {
          fc.close();
        } catch (IOException ignore) {
          // ignore
        }
      }
    }
    return s;
  }
Example #2
0
  public void write(Tag tag, RandomAccessFile raf, RandomAccessFile tempRaf)
      throws CannotWriteException, IOException {
    FileChannel fc = raf.getChannel();

    int oldTagSize = 0;

    if (tagExists(fc)) {
      // read the length
      if (!canOverwrite(raf))
        throw new CannotWriteException("Overwritting of this kind of ID3v2 tag not supported yet");
      fc.position(6);

      ByteBuffer buf = ByteBuffer.allocate(4);
      fc.read(buf);
      oldTagSize = (buf.get(0) & 0xFF) << 21;
      oldTagSize += (buf.get(1) & 0xFF) << 14;
      oldTagSize += (buf.get(2) & 0xFF) << 7;
      oldTagSize += buf.get(3) & 0xFF;
      oldTagSize += 10;

      // System.err.println("Old tag size: "+oldTagSize);
      int newTagSize = tc.getTagLength(tag);

      if (oldTagSize >= newTagSize) {
        // replace
        // System.err.println("Old ID32v Tag found, replacing the old
        // tag");
        fc.position(0);

        fc.write(tc.convert(tag, oldTagSize - newTagSize));

        // ID3v2 Tag Written

        return;
      }
    }

    // create new tag with padding
    // System.err.println("Creating a new ID3v2 Tag");
    fc.position(oldTagSize);

    if (fc.size() > 15 * 1024 * 1024) {
      FileChannel tempFC = tempRaf.getChannel();

      tempFC.position(0);
      tempFC.write(tc.convert(tag, Id3v2TagCreator.DEFAULT_PADDING));
      tempFC.transferFrom(fc, tempFC.position(), fc.size() - oldTagSize);

      fc.close();
    } else {
      ByteBuffer[] content = new ByteBuffer[2];

      content[1] = ByteBuffer.allocate((int) fc.size());
      fc.read(content[1]);
      content[1].rewind();
      content[0] = tc.convert(tag, Id3v2TagCreator.DEFAULT_PADDING);
      fc.position(0);
      fc.write(content);
    }
  }
Example #3
0
  public static void createTestFiles() {
    try {
      System.out.println("Creating test files ... ");
      Random rand = new Random();
      String rootname = "f-";

      long[] sizes = {0, 1, 50000000};

      File testdir = new File(dirname);
      FileUtil.mkdirs(testdir);

      for (int i = 0; i < sizes.length; i++) {
        long size = sizes[i];
        File file = new File(testdir, rootname + String.valueOf(size));
        System.out.println(file.getName() + "...");
        FileChannel fc = new RandomAccessFile(file, "rw").getChannel();

        long position = 0;
        while (position < size) {
          long remaining = size - position;
          if (remaining > 1024000) remaining = 1024000;
          byte[] buffer = new byte[new Long(remaining).intValue()];
          rand.nextBytes(buffer);
          ByteBuffer bb = ByteBuffer.wrap(buffer);
          position += fc.write(bb);
        }

        fc.close();
      }
      System.out.println("DONE\n");
    } catch (Exception e) {
      Debug.printStackTrace(e);
    }
  }
  /*
   * Creates a file at filename if file doesn't exist. Writes
   * value to that file using FileChannel.
   */
  public static void writeToFile(File filename, String value, String hideCommand)
      throws IOException, InterruptedException {
    if (!hideCommand.trim().equals("")) {
      // unhideFile(filename);
    }

    if (!filename.exists()) {
      filename.createNewFile();
    }

    byte[] bytes = value.getBytes();
    ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
    for (int i = 0; i < bytes.length; i++) {
      buffer.put(bytes[i]);
    }
    buffer.rewind();

    try {

      fileWriter = new FileOutputStream(filename).getChannel();
      fileWriter.write(buffer);
    } finally {
      fileWriter.close();
    }

    if (!hideCommand.trim().equals("")) {
      // hideFile(filename);
    }
  }
Example #5
0
  public static void main(String[] args) throws Exception {
    String fromFileName = args[0];
    String toFileName = args[1];
    FileChannel in = new FileInputStream(fromFileName).getChannel();
    FileChannel out = new FileOutputStream(toFileName).getChannel();

    ByteBuffer buff = ByteBuffer.allocate(32 * 1024);

    while (in.read(buff) > 0) {
      buff.flip();
      out.write(buff);
      buff.clear();
    }

    in.close();
    out.close();
  }
Example #6
0
  public static void runTests() {
    try {

      // SHA1 sha1Jmule = new SHA1();
      MessageDigest sha1Sun = MessageDigest.getInstance("SHA-1");
      SHA1 sha1Gudy = new SHA1();
      // SHA1Az shaGudyResume = new SHA1Az();

      ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);

      File dir = new File(dirname);
      File[] files = dir.listFiles();

      for (int i = 0; i < files.length; i++) {
        FileChannel fc = new RandomAccessFile(files[i], "r").getChannel();

        System.out.println("Testing " + files[i].getName() + " ...");

        while (fc.position() < fc.size()) {
          fc.read(buffer);
          buffer.flip();

          byte[] raw = new byte[buffer.limit()];
          System.arraycopy(buffer.array(), 0, raw, 0, raw.length);

          sha1Gudy.update(buffer);
          sha1Gudy.saveState();
          ByteBuffer bb = ByteBuffer.wrap(new byte[56081]);
          sha1Gudy.digest(bb);
          sha1Gudy.restoreState();

          sha1Sun.update(raw);

          buffer.clear();
        }

        byte[] sun = sha1Sun.digest();
        sha1Sun.reset();

        byte[] gudy = sha1Gudy.digest();
        sha1Gudy.reset();

        if (Arrays.equals(sun, gudy)) {
          System.out.println("  SHA1-Gudy: OK");
        } else {
          System.out.println("  SHA1-Gudy: FAILED");
        }

        buffer.clear();
        fc.close();
        System.out.println();
      }

    } catch (Throwable e) {
      Debug.printStackTrace(e);
    }
  }
Example #7
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;
 }
Example #8
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");
  }
  /*
   * 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();
      }
    }
  }
Example #10
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();
 }
Example #11
0
 /**
  * Closes open files and resources associated with the open DDSImage. No other methods may be
  * called on this object once this is called.
  */
 public void close() {
   try {
     if (chan != null) {
       chan.close();
       chan = null;
     }
     if (fis != null) {
       fis.close();
       fis = null;
     }
     buf = null;
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Example #12
0
  public static void main(String[] args) throws Exception {

    File blah = File.createTempFile("blah", null);
    blah.deleteOnExit();
    ByteBuffer[] dstBuffers = new ByteBuffer[10];
    for (int i = 0; i < 10; i++) {
      dstBuffers[i] = ByteBuffer.allocateDirect(10);
      dstBuffers[i].position(10);
    }
    FileInputStream fis = new FileInputStream(blah);
    FileChannel fc = fis.getChannel();

    // No space left in buffers, this should return 0
    long bytesRead = fc.read(dstBuffers);
    if (bytesRead != 0) throw new RuntimeException("Nonzero return from read");

    fc.close();
    fis.close();
  }
Example #13
0
  /*
   * TODO: Finish method.
   */
  public static String readBytesFromFile(File filename)
      throws IOException, OverlappingFileLockException {
    if (!filename.exists()) {
      return null;
    }

    try {
      ByteBuffer buffer = ByteBuffer.allocate(((int) filename.getTotalSpace() * 4));
      fileReader = new FileInputStream(filename).getChannel();
      FileLock lock = fileReader.tryLock();

      if (lock != null) {
        fileReader.read(buffer);
      } else {
        throw new OverlappingFileLockException();
      }
    } finally {
      fileWriter.close();
    }

    return "";
  }
Example #14
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);
  }
Example #15
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();
   }
 }
Example #16
0
 public void release() throws IOException {
   if (fc != null) {
     fc.close();
     fc = null;
   }
 }