package net.nitroshare.android.transfer;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.SocketChannel;

/**
 * Individual packet of information in a transfer
 *
 * Transfers are (at a high level) essentially a stream of packets being
 * exchanged back and forth. The packet format is described here:
 * https://goo.gl/fL890p
 */
class Packet {

    /**
     * Transfer succeeded
     *
     * This packet is sent by the receiver to indicate the transfer succeeded
     * and the connection may be closed.
     */
    public static final int SUCCESS = 0;

    /**
     * Transfer failed
     *
     * This packet is sent by either end to indicate an error. The content of
     * packet describes the error.
     */
    public static final int ERROR = 1;

    /**
     * JSON data
     */
    public static final int JSON = 2;

    /**
     * Binary data
     */
    public static final int BINARY = 3;

    private int mType;
    private ByteBuffer mBuffer;
    private boolean mHaveSize = false;

    /**
     * Retrieve the packet type
     * @return packet type
     */
    public int getType() {
        return mType;
    }

    /**
     * Retrieve the buffer for the packet
     * @return byte array
     */
    public ByteBuffer getBuffer() {
        return mBuffer;
    }

    /**
     * Determine if the buffer is full
     * @return true if full
     */
    public boolean isFull() {
        return mBuffer.position() == mBuffer.capacity();
    }

    /**
     * Create an empty packet of the specified type
     * @param type
     */
    public Packet(int type) {
        mType = type;
        mBuffer = ByteBuffer.allocate(5);
        mBuffer.order(ByteOrder.LITTLE_ENDIAN);
    }

    /**
     * Create a packet of the specified type with the specified data
     */
    public Packet(int type, byte[] data) {
        mBuffer = ByteBuffer.allocate(5 + data.length);
        mBuffer.order(ByteOrder.LITTLE_ENDIAN);
        mBuffer.putInt(data.length + 1).put((byte) mType).put(data);
    }

    /**
     * Read a packet from a socket channel
     * @param socketChannel
     */
    public void read(SocketChannel socketChannel) throws IOException {

        // If the 32-bit size hasn't yet been read, do so
        if (!mHaveSize) {
            socketChannel.read(mBuffer);
            if (mBuffer.position() != 5) {
                return;
            }

            // Remaining data is 8-bit type and data
            mBuffer.flip();
            int size = mBuffer.getInt() - 1;
            mType = mBuffer.get();
            mBuffer = ByteBuffer.allocate(size);
            mBuffer.order(ByteOrder.LITTLE_ENDIAN);
            mHaveSize = true;
        }

        // The size is known, read data into the buffer
        socketChannel.read(mBuffer);
    }
}

@@ -564,39 +564,32 @@
 ed.%0A     */%0A    
-public 
 static final int
@@ -758,39 +758,32 @@
 or.%0A     */%0A    
-public 
 static final int
@@ -824,39 +824,32 @@
 ata%0A     */%0A    
-public 
 static final int
@@ -899,23 +899,16 @@
  */%0A    
-public 
 static f
@@ -1103,23 +1103,16 @@
  */%0A    
-public 
 int getT
@@ -1236,23 +1236,16 @@
  */%0A    
-public 
 ByteBuff
@@ -1379,23 +1379,16 @@
  */%0A    
-public 
 boolean 
@@ -1503,109 +1503,31 @@
 cket
- of the specified type%0A     * @param type%0A     */%0A    public Packet(int type) %7B%0A        mType = type;
+%0A     */%0A    Packet() %7B
 %0A   
@@ -1709,23 +1709,16 @@
  */%0A    
-public 
 Packet(i
@@ -1737,24 +1737,118 @@
 e%5B%5D data) %7B%0A
+        this(type, data, data.length);%0A    %7D%0A%0A    Packet(int type, byte%5B%5D data, int length) %7B%0A
         mBuf
@@ -1990,18 +1990,17 @@
 ((byte) 
-mT
+t
 ype).put
@@ -2004,16 +2004,27 @@
 put(data
+, 0, length
 );%0A    %7D
@@ -2086,48 +2086,13 @@
    *
- @param socketChannel%0A     */%0A    public
+/%0A   
  voi

Remove various warnings.