Here you can find the source of removeFirstBytes(ByteBuffer buffer, int num)
public static ByteBuffer removeFirstBytes(ByteBuffer buffer, int num)
//package com.java2s; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main { public static void removeFirstBytes(File file, long num) { try {/*from w ww. j av a2s . co m*/ // temporary file File tmp = new File(file.getAbsolutePath() + ".tmp"); // cut-off and copy to temporary file FileOutputStream fout = new FileOutputStream( tmp.getAbsolutePath()); FileInputStream fin = new FileInputStream( file.getAbsolutePath()); FileChannel chanOut = fout.getChannel(); FileChannel chanIn = fin.getChannel(); chanIn.transferTo(num, chanIn.size() - num, chanOut); fin.close(); fout.close(); // copy the temporary file to the original fout = new FileOutputStream(file.getAbsolutePath()); fin = new FileInputStream(tmp.getAbsolutePath()); chanOut = fout.getChannel(); chanIn = fin.getChannel(); chanIn.transferTo(0, chanIn.size(), chanOut); fin.close(); fout.close(); // delete temporary file tmp.delete(); } catch (IOException e) { e.printStackTrace(); } } public static ByteBuffer removeFirstBytes(ByteBuffer buffer, int num) { // set the marker at the initial position buffer.position(buffer.limit() - num); // temporal array for storing result byte[] tmp = new byte[buffer.remaining()]; // copy last bytes to temporary buffer.get(tmp); ByteBuffer res = ByteBuffer.wrap(tmp); res.rewind(); return res; } }