Example #1
1
  public B_Tree.Page<Key> load(int pageNo) {
    try {
      ByteBuffer buffer = ByteBuffer.allocate(pageSize);
      channel.read(buffer, pageNo * pageSize);
      buffer.rewind();

      B_Tree.Page<Key> page = new B_Tree.Page<Key>(pageNo);
      List<B_Tree.KeyPointer<Key>> keyPointers = page.keyPointers;

      char nodeType = buffer.getChar();
      int size = buffer.getInt();

      for (int i = 0; i < size; i++) {
        Key key = keyAccessor.read(buffer);

        if (nodeType == INTERNAL) {
          int branch = buffer.getInt();
          addBranch(keyPointers, key, branch);
        } else if (nodeType == LEAF) {
          Value value = valueAccessor.read(buffer);
          addLeaf(keyPointers, key, value);
        }
      }

      return page;
    } catch (IOException ex) {
      throw new RuntimeException(ex);
    }
  }
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
  @Override
  public void get(ByteBuffer key, Result result) throws IOException {
    result.requiresBufferSize(readBufferSize);

    keyfile.get(key, result);

    if (result.isFound()) {
      ByteBuffer buffer = result.getBuffer();
      long recordFileOffset = EncodingHelper.decodeLittleEndianFixedWidthLong(buffer);

      buffer.rewind();
      buffer.limit(readBufferSize);
      recordFile.read(buffer, recordFileOffset);
      buffer.rewind();
      int recordSize = EncodingHelper.decodeLittleEndianVarInt(buffer);
      int bytesInRecordSize = buffer.position();
      if (buffer.remaining() < recordSize) {
        int newSize = recordSize + EncodingHelper.MAX_VARINT_SIZE;
        result.requiresBufferSize(newSize);
        recordFile.read(buffer, recordFileOffset + bytesInRecordSize);
        buffer.position(0);
      }
      buffer.limit(recordSize + buffer.position());
    }
  }
Example #4
0
  public static void main(String args[]) {
    try {
      aServer asr = new aServer();

      // file channel.
      FileInputStream is = new FileInputStream("");
      is.read();
      FileChannel cha = is.getChannel();
      ByteBuffer bf = ByteBuffer.allocate(1024);
      bf.flip();

      cha.read(bf);

      // Path Paths
      Path pth = Paths.get("", "");

      // Files some static operation.
      Files.newByteChannel(pth);
      Files.copy(pth, pth);
      // file attribute, other different class for dos and posix system.
      BasicFileAttributes bas = Files.readAttributes(pth, BasicFileAttributes.class);
      bas.size();

    } catch (Exception e) {
      System.err.println(e);
    }

    System.out.println("hello ");
  }
Example #5
0
  @Test
  public void testRead() throws IOException {
    RegularFile file = regularFile(20);
    FileChannel channel = channel(file, READ);
    assertEquals(0, channel.position());

    ByteBuffer buf = buffer("1234567890");
    ByteBuffer buf2 = buffer("123457890");
    assertEquals(10, channel.read(buf));
    assertEquals(10, channel.position());

    buf.flip();
    assertEquals(10, channel.read(new ByteBuffer[] {buf, buf2}));
    assertEquals(20, channel.position());

    buf.flip();
    buf2.flip();
    file.write(20, new byte[10], 0, 10);
    assertEquals(10, channel.read(new ByteBuffer[] {buf, buf2}, 0, 2));
    assertEquals(30, channel.position());

    buf.flip();
    assertEquals(10, channel.read(buf, 5));
    assertEquals(30, channel.position());

    buf.flip();
    assertEquals(-1, channel.read(buf));
    assertEquals(30, channel.position());
  }
Example #6
0
  /**
   * Read, validate, and discard a single message, returning the next valid offset, and the message
   * being validated
   *
   * @throws IOException
   */
  private long validateMessage(FileChannel channel, long start, long len, ByteBuffer buffer)
      throws IOException {
    buffer.rewind();
    int read = channel.read(buffer, start);
    if (read < 4) return -1;

    // check that we have sufficient bytes left in the file
    int size = buffer.getInt(0);
    if (size < Message.MinHeaderSize) return -1;

    long next = start + 4 + size;
    if (next > len) return -1;

    // read the message
    ByteBuffer messageBuffer = ByteBuffer.allocate(size);
    long curr = start + 4;
    while (messageBuffer.hasRemaining()) {
      read = channel.read(messageBuffer, curr);
      if (read < 0) throw new IllegalStateException("File size changed during recovery!");
      else curr += read;
    }
    messageBuffer.rewind();
    Message message = new Message(messageBuffer);
    if (!message.isValid()) return -1;
    else return next;
  }
Example #7
0
  /**
   * Load enough bytes from channel to buffer. After the loading process, the caller can make sure
   * the amount in buffer is of size 'amount' if we haven't reached the end of channel.
   *
   * @param amount The amount of bytes in buffer after returning, no larger than bufferSize
   * @param reload Whether to reload or append
   */
  private void fillBuffer(long amount, boolean reload) {
    try {
      if (amount > bufferSize) {
        amount = bufferSize;
      }
      // Read all remaining bytes if the requested amount reach the end
      // of channel.
      if (channelSize - channel.position() < amount) {
        amount = channelSize - channel.position();
      }

      if (in == null) {
        switch (bufferType) {
          case HEAP:
            in = ByteBuffer.allocate(bufferSize, false);
            break;
          case DIRECT:
            in = ByteBuffer.allocate(bufferSize, true);
            break;
          case AUTO:
            in = ByteBuffer.allocate(bufferSize);
            break;
          default:
            in = ByteBuffer.allocate(bufferSize);
        }
        channel.read(in.buf());
        in.flip();
        useLoadBuf = true;
      }

      if (!useLoadBuf) {
        return;
      }

      if (reload || in.remaining() < amount) {
        if (!reload) {
          in.compact();
        } else {
          in.clear();
        }
        channel.read(in.buf());
        in.flip();
      }

    } catch (Exception e) {
      log.error("Error fillBuffer", e);
    }
  }
  public static String readInFile(final File file) {
    try {
      final FileChannel fc = new FileInputStream(file).getChannel();
      final ByteBuffer buf = allocateDirect(10);
      final StringBuilder appender = new StringBuilder();
      int read;

      while (true) {
        buf.rewind();
        if ((read = fc.read(buf)) != -1) {
          buf.rewind();
          for (; read != 0; read--) {
            appender.append((char) buf.get());
          }
        } else {
          break;
        }
      }

      fc.close();

      return appender.toString();
    } catch (final FileNotFoundException e) {
      throw new TemplateError("cannot include template '" + file.getName() + "': file not found.");
    } catch (final IOException e) {
      throw new TemplateError(
          "unknown I/O exception while including '" + file.getName() + "' (stacktrace nested)", e);
    }
  }
 public boolean storeFilesUpgradeable(File neoStoreFile) {
   File storeDirectory = neoStoreFile.getParentFile();
   for (String fileName : fileNamesToExpectedVersions.keySet()) {
     String expectedVersion = fileNamesToExpectedVersions.get(fileName);
     FileChannel fileChannel = null;
     byte[] expectedVersionBytes = UTF8.encode(expectedVersion);
     try {
       File storeFile = new File(storeDirectory, fileName);
       if (!fs.fileExists(storeFile)) {
         return false;
       }
       fileChannel = fs.open(storeFile, "r");
       if (fileChannel.size() < expectedVersionBytes.length) {
         return false;
       }
       fileChannel.position(fileChannel.size() - expectedVersionBytes.length);
       byte[] foundVersionBytes = new byte[expectedVersionBytes.length];
       fileChannel.read(ByteBuffer.wrap(foundVersionBytes));
       if (!expectedVersion.equals(UTF8.decode(foundVersionBytes))) {
         return false;
       }
     } catch (IOException e) {
       throw new RuntimeException(e);
     } finally {
       if (fileChannel != null) {
         try {
           fileChannel.close();
         } catch (IOException e) {
           // Ignore exception on close
         }
       }
     }
   }
   return true;
 }
Example #10
0
 @Override
 public int read(ByteBuffer dst, long position) throws IOException {
   long cachePos = getCachePos(position);
   int off = (int) (position - cachePos);
   int len = CACHE_BLOCK_SIZE - off;
   len = Math.min(len, dst.remaining());
   ByteBuffer buff = cache.get(cachePos);
   if (buff == null) {
     buff = ByteBuffer.allocate(CACHE_BLOCK_SIZE);
     long pos = cachePos;
     while (true) {
       int read = base.read(buff, pos);
       if (read <= 0) {
         break;
       }
       if (buff.remaining() == 0) {
         break;
       }
       pos += read;
     }
     int read = buff.position();
     if (read == CACHE_BLOCK_SIZE) {
       cache.put(cachePos, buff, CACHE_BLOCK_SIZE);
     } else {
       if (read <= 0) {
         return -1;
       }
       len = Math.min(len, read - off);
     }
   }
   dst.put(buff.array(), off, len);
   return len == 0 ? -1 : len;
 }
Example #11
0
  /**
   * Parses the FileChannel, in the range [start, end) and prints the elements found
   *
   * <p>Elements are printed, indented by "level" number of spaces. If an element is a container,
   * then its contents will be, recursively, printed, with a greater indentation.
   *
   * @param fc
   * @param level
   * @param start
   * @param end
   * @throws IOException
   */
  private void print(FileChannel fc, int level, long start, long end) throws IOException {
    fc.position(start);
    if (end <= 0) {
      end = start + fc.size();
      System.out.println("Setting END to " + end);
    }
    while (end - fc.position() > 8) {
      long begin = fc.position();
      ByteBuffer bb = ByteBuffer.allocate(8);
      fc.read(bb);
      bb.rewind();
      long size = IsoTypeReader.readUInt32(bb);
      String type = IsoTypeReader.read4cc(bb);
      long fin = begin + size;
      // indent by the required number of spaces
      for (int i = 0; i < level; i++) {
        System.out.print(" ");
      }

      System.out.println(type + "@" + (begin) + " size: " + size);
      if (containers.contains(type)) {
        print(fc, level + 1, begin + 8, fin);
        if (fc.position() != fin) {
          System.out.println("End of container contents at " + fc.position());
          System.out.println("  FIN = " + fin);
        }
      } else {

      }

      fc.position(fin);
    }
  }
  private synchronized String readNextChunk(int capacity) throws IOException {
    if (capacity == 0) {
      throw new EndOfFileException("Zero chunk size, possibly end of file reached.");
    }

    ByteBuffer buf = ByteBuffer.allocateDirect(capacity);
    byte[] dst = new byte[capacity];

    int cnt = file.read(buf);
    // log.debug("Read " + cnt);
    if (cnt != capacity) {
      throw new IOException(
          "Expected chunk size (" + capacity + ") differs from read bytes count (" + cnt + ")");
    }

    buf.flip();
    buf.get(dst);
    if (log.isDebugEnabled()) {
      log.debug("Chunk : " + new String(dst));
    }

    if (isHexEncode()) {
      return JOrphanUtils.baToHexString(dst);
    } else {
      return new String(dst, binaryCharset);
    }
  }
Example #13
0
  public static void copyFile(File srcFile, File destFile) throws Exception {
    int bufferSize = 2048;

    FileInputStream in = new FileInputStream(srcFile);
    FileOutputStream out = new FileOutputStream(destFile);
    FileChannel inChannel = in.getChannel();
    FileChannel outChannel = out.getChannel();

    ByteBuffer buffer = null;
    int length = -1;
    try {
      while (true) {
        if (inChannel.position() == inChannel.size()) {
          // finish copying
          break;
        } else if (inChannel.size() - inChannel.position() < length) {
          // copy last chunk of data
          length = (int) (inChannel.size() - inChannel.position());
        } else {
          length = bufferSize;
        }

        buffer = ByteBuffer.allocateDirect(length);
        inChannel.read(buffer);
        buffer.flip();
        outChannel.write(buffer);
        outChannel.force(false);
      }
    } finally {
      _close(inChannel);
      _close(in);
      _close(outChannel);
      _close(out);
    }
  }
Example #14
0
  public static PyMarkObject Unpack(String filename) throws IOException {
    FileChannel channel = new FileInputStream(filename).getChannel();

    ByteBuffer bb = ByteBuffer.allocateDirect((int) channel.size());
    bb.order(ByteOrder.LITTLE_ENDIAN);

    channel.read(bb);
    bb.rewind();

    byte[] magiccmp = new byte[] {'P', 'Y', 'M', 'A', 'R', 'K'};
    byte[] magic = new byte[6];
    bb.get(magic);

    if (!Arrays.equals(magiccmp, magic)) {
      throw new IOException("Invalid Magic Number");
    }

    byte version = bb.get();
    if (version != 1) {
      throw new IOException("Invalid Version");
    }

    PyMarkObject obj = UnpackObject(bb);

    channel.close();

    return obj;
  }
Example #15
0
  private int getUncheckedSubPiece(int subpiece, byte[] bs, int off) throws IOException {
    // XXX - copy/paste code from putSubpiece().
    int start = subpiece * metainfo.getSubpieceSize(0);
    int subPieceSize = metainfo.getSubpieceSize(subpiece);

    int i = 0;
    long raflen = this.length;
    while (start > raflen) {
      i++;
      start -= raflen;
      raflen = this.length;
    }

    FileChannel fc = getFileChannel();

    ByteBuffer bb = ByteBuffer.wrap(bs);

    int read = 0;
    while (read < subPieceSize && read != -1) {
      fc.position(start);
      int nRead = fc.read(bb);
      if (nRead == -1) {
        read = -1;
      } else {
        read += nRead;
      }
    }

    return subPieceSize;
  }
 public static void main(String[] arguments) {
   try {
     // read byte data into a byte buffer
     String data = "friends.dat";
     FileInputStream inData = new FileInputStream(data);
     FileChannel inChannel = inData.getChannel();
     long inSize = inChannel.size();
     ByteBuffer source = ByteBuffer.allocate((int) inSize);
     inChannel.read(source, 0);
     source.position(0);
     System.out.println("Original byte data:");
     for (int i = 0; source.remaining() > 0; i++) {
       System.out.print(source.get() + " ");
     }
     // convert byte data into character data
     source.position(0);
     Charset ascii = Charset.forName("US-ASCII");
     CharsetDecoder toAscii = ascii.newDecoder();
     CharBuffer destination = toAscii.decode(source);
     destination.position(0);
     System.out.println("\n\nNew character data:");
     for (int i = 0; destination.remaining() > 0; i++) {
       System.out.print(destination.get());
     }
     System.out.println();
   } catch (FileNotFoundException fne) {
     System.out.println(fne.getMessage());
   } catch (IOException ioe) {
     System.out.println(ioe.getMessage());
   }
 }
Example #17
0
  public boolean copyFromChannel(FileChannel channel, TableStats stats) throws IOException {
    if (channel.position() < channel.size()) {
      clear();

      buffer.clear();
      channel.read(buffer);
      memorySize = buffer.position();

      while (position < memorySize) {
        long recordPtr = address + position;

        if (remain() < SizeOf.SIZE_OF_INT) {
          channel.position(channel.position() - remain());
          memorySize = (int) (memorySize - remain());
          return true;
        }

        int recordSize = UNSAFE.getInt(recordPtr);

        if (remain() < recordSize) {
          channel.position(channel.position() - remain());
          memorySize = (int) (memorySize - remain());
          return true;
        }

        position += recordSize;
        rowNum++;
      }

      return true;
    } else {
      return false;
    }
  }
Example #18
0
  public static void main(String[] args) throws IOException {
    System.err.println(df.format(new Date(System.currentTimeMillis())));

    File rfile = new File("D:/TDDOWNLOAD/17173_SC2-WingsOfLiberty-zhCN-Installer.rar");
    File wfile = new File("E:/17173_SC2-WingsOfLiberty-zhCN-InstallerCopy.rar");

    FileChannel rchannel = new FileInputStream(rfile).getChannel();
    FileChannel wchannel = new FileOutputStream(wfile).getChannel();
    ByteBuffer[] buffers = new ByteBuffer[1024];

    for (int i = 0; i < buffers.length; i++) {
      buffers[i] = ByteBuffer.allocate(1024);
    }
    boolean isLoop = true;
    while (isLoop) {
      for (ByteBuffer buffer : buffers) {
        buffer.rewind();
      }
      isLoop = rchannel.read(buffers) != -1L;
      for (ByteBuffer buffer : buffers) {
        buffer.flip();
      }
      wchannel.write(buffers);
    }
    rchannel.close();
    wchannel.close();
  }
Example #19
0
  /**
   * Picks a block with samplingRate probability Used in the first phase of up-front partitioning.
   *
   * @param filename
   * @param samplingRate
   */
  public void scanWithBlockSampling(String filename, double samplingRate, OutputStream out) {
    initScan(blockSampleSize);
    FileChannel ch = IOUtils.openFileChannel(filename);
    try {
      for (long position = 0; ; position += blockSampleSize) {
        while (sampleSucceed(samplingRate) == false) {
          position += blockSampleSize;
        }
        ch.position(position);
        if ((nRead = ch.read(bb)) == -1) {
          break;
        }

        byteArrayIdx = previous = 0;
        if (position != 0) {
          // skip the first tuple if not starting of file.
          while (byteArrayIdx < nRead && byteArray[byteArrayIdx] != newLine) {
            byteArrayIdx++;
          }
          previous = ++byteArrayIdx;
        }

        processByteBuffer(null, out);
        bb.clear();
        out.flush(); // It only helps get an exact profiling?
      }

    } catch (IOException e) {
      e.printStackTrace();
    }
    IOUtils.closeFileChannel(ch);
    firstPass = false;
  }
Example #20
0
  @Override
  public void start() {
    try {
      // open the data file
      String location = configuration.location();
      if (location == null || location.trim().length() == 0)
        location = "Infinispan-SingleFileStore";

      file = new File(location, ctx.getCache().getName() + ".dat");
      if (!file.exists()) {
        File dir = file.getParentFile();
        if (!dir.mkdirs() && !dir.exists()) {
          throw log.directoryCannotBeCreated(dir.getAbsolutePath());
        }
      }
      channel = new RandomAccessFile(file, "rw").getChannel();

      // initialize data structures
      entries = newEntryMap();
      freeList = Collections.synchronizedSortedSet(new TreeSet<FileEntry>());

      // check file format and read persistent state if enabled for the cache
      byte[] header = new byte[MAGIC.length];
      if (channel.read(ByteBuffer.wrap(header), 0) == MAGIC.length
          && Arrays.equals(MAGIC, header)) {
        rebuildIndex();
        processFreeEntries();
      } else clear(); // otherwise (unknown file format or no preload) just reset the file

      // Initialize the fragmentation factor
      fragmentationFactor = configuration.fragmentationFactor();
    } catch (Exception e) {
      throw new PersistenceException(e);
    }
  }
 private byte getOneByte() throws IOException {
   oneByte.rewind();
   if (file.read(oneByte) < 1) {
     throw new EndOfFileException(getFileName());
   }
   return oneByte.get(0);
 }
Example #22
0
 private static int a(FileChannel filechannel)
 {
     ByteBuffer bytebuffer;
     int i;
     try
     {
         bytebuffer = ByteBuffer.allocate(4);
         bytebuffer.order(ByteOrder.LITTLE_ENDIAN);
     }
     // Misplaced declaration of an exception variable
     catch (FileChannel filechannel)
     {
         filechannel.printStackTrace();
         return 0;
     }
     if (d != 0)
     {
         a = 47;
         d = 93;
     }
     bytebuffer.position(0);
     if (filechannel.read(bytebuffer, 16L) != 4)
     {
         return 0;
     }
       goto _L1
Example #23
0
  /**
   * @param startByte
   * @param endByte
   * @return
   * @throws Exception
   * @return true if all the bytes between in the file between startByte and endByte are null, false
   *     otherwise
   */
  private boolean isFilePortionNull(int startByte, int endByte) throws IOException {
    logger.config("Checking file portion:" + Hex.asHex(startByte) + ":" + Hex.asHex(endByte));
    FileInputStream fis = null;
    FileChannel fc = null;
    try {
      fis = new FileInputStream(file);
      fc = fis.getChannel();
      fc.position(startByte);
      ByteBuffer bb = ByteBuffer.allocateDirect(endByte - startByte);
      fc.read(bb);
      while (bb.hasRemaining()) {
        if (bb.get() != 0) {
          return false;
        }
      }
    } finally {
      if (fc != null) {
        fc.close();
      }

      if (fis != null) {
        fis.close();
      }
    }
    return true;
  }
  public void writeRequest(final OutputStream out) throws IOException {
    int readCount = 0;
    Iterator<OnDatatransferProgressListener> it = null;

    try {
      mChannel.position(mOffset);
      long size = mFile.length();
      if (size == 0) size = -1;
      long maxCount = Math.min(mOffset + mChunkSize, mChannel.size());
      while (mChannel.position() < maxCount) {
        readCount = mChannel.read(mBuffer);
        out.write(mBuffer.array(), 0, readCount);
        mBuffer.clear();
        if (mTransferred < maxCount) { // condition to avoid accumulate progress for repeated chunks
          mTransferred += readCount;
        }
        synchronized (mDataTransferListeners) {
          it = mDataTransferListeners.iterator();
          while (it.hasNext()) {
            it.next().onTransferProgress(readCount, mTransferred, size, mFile.getName());
          }
        }
      }

    } catch (IOException io) {
      Log.e(TAG, io.getMessage());
      throw new RuntimeException(
          "Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really",
          io);
    }
  }
Example #25
0
 /**
  * Sets the version for the given neostore file in {@code storeDir}.
  *
  * @param storeDir the store dir to locate the neostore file in.
  * @param version the version to set.
  * @return the previous version before writing.
  */
 public static long setVersion(String storeDir, long version) {
   RandomAccessFile file = null;
   try {
     file = new RandomAccessFile(new File(storeDir, NeoStore.DEFAULT_NAME), "rw");
     FileChannel channel = file.getChannel();
     channel.position(RECORD_SIZE * 2 + 1 /*inUse*/);
     ByteBuffer buffer = ByteBuffer.allocate(8);
     channel.read(buffer);
     buffer.flip();
     long previous = buffer.getLong();
     channel.position(RECORD_SIZE * 2 + 1 /*inUse*/);
     buffer.clear();
     buffer.putLong(version).flip();
     channel.write(buffer);
     return previous;
   } catch (IOException e) {
     throw new RuntimeException(e);
   } finally {
     try {
       if (file != null) file.close();
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
 }
Example #26
0
  private void performTransfer(FileChannel source, IoCallback callback) {
    if (outputStream instanceof BufferWritableOutputStream) {
      try {
        ((BufferWritableOutputStream) outputStream).transferFrom(source);
      } catch (IOException e) {
        callback.onException(exchange, this, e);
      }
    } else {
      ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
      try {
        long pos = source.position();
        long size = source.size();
        while (size - pos > 0) {
          int ret = source.read(buffer);
          if (ret <= 0) {
            break;
          }
          pos += ret;
          outputStream.write(buffer.array(), buffer.arrayOffset(), ret);
          buffer.clear();
        }

        if (pos != size) {
          throw new EOFException("Unexpected EOF reading file");
        }

      } catch (IOException e) {
        callback.onException(exchange, this, e);
      }
    }
  }
Example #27
0
 private void insertRecord(int recordPosition, long value) throws IOException {
   try {
     FileChannel channel = getFileChannel();
     long previousPosition = channel.position();
     channel.position(RECORD_SIZE * recordPosition);
     int trail = (int) (channel.size() - channel.position());
     ByteBuffer trailBuffer = null;
     if (trail > 0) {
       trailBuffer = ByteBuffer.allocate(trail);
       channel.read(trailBuffer);
       trailBuffer.flip();
     }
     ByteBuffer buffer = ByteBuffer.allocate(RECORD_SIZE);
     buffer.put(Record.IN_USE.byteValue());
     buffer.putLong(value);
     buffer.flip();
     channel.position(RECORD_SIZE * recordPosition);
     channel.write(buffer);
     if (trail > 0) {
       channel.write(trailBuffer);
     }
     channel.position(previousPosition);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
Example #28
0
  /**
   * 读取到字节数组2
   *
   * @param filePath
   * @return
   * @throws IOException
   */
  public static byte[] toByteArray2(String filePath) throws IOException {

    File f = new File(filePath);
    if (!f.exists()) {
      throw new FileNotFoundException(filePath);
    }

    FileChannel channel = null;
    FileInputStream fs = null;
    try {
      fs = new FileInputStream(f);
      channel = fs.getChannel();
      ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
      while ((channel.read(byteBuffer)) > 0) {
        // do nothing
        // System.out.println("reading");
      }
      return byteBuffer.array();
    } catch (IOException e) {
      e.printStackTrace();
      throw e;
    } finally {
      try {
        channel.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
      try {
        fs.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
Example #29
0
 private static long getRecord(String storeDir, long recordPosition) {
   RandomAccessFile file = null;
   try {
     file = new RandomAccessFile(new File(storeDir), "rw");
     FileChannel channel = file.getChannel();
     /*
      * We have to check size, because the store version
      * field was introduced with 1.5, so if there is a non-clean
      * shutdown we may have a buffer underflow.
      */
     if (recordPosition > 3 && channel.size() < RECORD_SIZE * 5) {
       return -1;
     }
     channel.position(RECORD_SIZE * recordPosition + 1 /*inUse*/);
     ByteBuffer buffer = ByteBuffer.allocate(8);
     channel.read(buffer);
     buffer.flip();
     long previous = buffer.getLong();
     return previous;
   } catch (IOException e) {
     throw new RuntimeException(e);
   } finally {
     try {
       if (file != null) file.close();
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
 }
Example #30
0
 /**
  * 监测复制进度(留意buffer的大小,对速度有很大影响)
  *
  * @param source
  * @param target
  */
 public static void nioBufferCopy(File source, File target) {
   FileChannel in = null;
   FileChannel out = null;
   FileInputStream inStream = null;
   FileOutputStream outStream = null;
   try {
     inStream = new FileInputStream(source);
     outStream = new FileOutputStream(target);
     in = inStream.getChannel();
     out = outStream.getChannel();
     ByteBuffer buffer = ByteBuffer.allocate(4096);
     while (in.read(buffer) != -1) {
       buffer.flip();
       out.write(buffer);
       buffer.clear();
     }
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     close(inStream);
     close(in);
     close(outStream);
     close(out);
   }
 }