Exemplo n.º 1
0
 public void testWindows() throws Exception {
   // ==== Testing Windows filesystem ===
   // two options to write - WRITE, WRITE + APPEND. Simple writes just rewrite current values
   SeekableByteChannel s =
       Files.newByteChannel(
           Paths.get("test.txt"),
           StandardOpenOption.WRITE,
           StandardOpenOption.READ,
           StandardOpenOption.CREATE); // , StandardOpenOption.APPEND);
   ByteBuffer bb = ByteBuffer.wrap("12345".getBytes());
   s.truncate(0); // clear file
   s.position(0); // start position
   s.write(bb);
   bb.position(2);
   s.write(bb);
   //
   s.position(0);
   byte[] bbrb = new byte[(int) s.size()];
   s.read(ByteBuffer.wrap(bbrb));
   System.out.println(new String(bbrb)); // should be 12345345
   s.close();
 }
Exemplo n.º 2
0
 private static void testReopenAndSeek() throws IOException {
   long position = 0;
   long start = System.currentTimeMillis();
   while (true) {
     try (SeekableByteChannel c = Files.newByteChannel(testFile, READ)) {
       b.clear();
       c.position(position);
       int nbytes = c.read(b);
       if (nbytes <= 0) break;
       position += nbytes;
     }
   }
   long end = System.currentTimeMillis();
   logger.info("testReopenAndSeek {} ms", end - start);
 }
  public void checkRanges() throws IOException {
    if (currentRange != null) {
      if (counter >= currentRange[1]) {
        counter = -1;
      }
    }

    if (counter == -1) {
      if (!ranges.isEmpty()) {
        currentRange = ranges.remove(0);
        // reset channel
        channel.position(currentRange[0]);
        counter = 0;
      } else {
        currentRange = null;
      }
    }
  }
Exemplo n.º 4
0
 // this method read every char at every 10 length position.
 private void executeRead() throws IOException {
   System.out.println("executeRead started");
   try (SeekableByteChannel channel =
       Files.newByteChannel(
           Paths.get("c:/temp/channelExample.txt"), EnumSet.of(StandardOpenOption.READ))) {
     ByteBuffer buffer = ByteBuffer.allocate(1);
     long position = 0;
     while (true) {
       buffer.clear();
       channel.position(position);
       if (channel.read(buffer) < 0) break;
       buffer.flip();
       System.out.println(
           "position : "
               + position
               + ", char : "
               + Charset.defaultCharset().decode(buffer)); // print chars from every 10 positions.
       position += 10;
     }
   }
 }
Exemplo n.º 5
0
  private static SeekableByteChannel addHeader(SeekableByteChannel input) {
    try {
      int total = (int) input.size();
      final String comment =
          "@HD\tVN:1.0  SO:unsorted\n"
              + "@SQ\tSN:chr1\tLN:101\n"
              + "@SQ\tSN:chr2\tLN:101\n"
              + "@SQ\tSN:chr3\tLN:101\n"
              + "@RG\tID:0\tSM:JP was here\n";

      byte[] commentBuf = comment.getBytes();
      ByteBuffer buf = ByteBuffer.allocate(total + commentBuf.length);
      buf.put(commentBuf);
      input.position(0);
      while (input.read(buf) > 0) {
        // read until EOF
      }
      buf.flip();
      return new SeekableByteChannelFromBuffer(buf);
    } catch (IOException x) {
      throw new RuntimeException(x);
    }
  }