Android ByteBuffer Get removeFirstBytes(ByteBuffer buffer, int num)

Here you can find the source of removeFirstBytes(ByteBuffer buffer, int num)

Description

remove First Bytes

Declaration

public static ByteBuffer removeFirstBytes(ByteBuffer buffer, int num) 

Method Source Code

//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;
    }
}

Related

  1. getUnsignedByte(final ByteBuffer pByteBuffer, final int pPosition)
  2. getUnsignedInt(final ByteBuffer pByteBuffer)
  3. getUnsignedInt(final ByteBuffer pByteBuffer, final int pPosition)
  4. extractNullTerminatedString(ByteBuffer bb)
  5. allocateMore(ByteBuffer output)
  6. shorts(ByteBuffer buffer)
  7. getObject(ByteBuffer byteBuffer)
  8. skip(ByteBuffer buffer, int length)