package prj.sslfacade;

import java.nio.ByteBuffer;

class AppendableBuffer
{
    private ByteBuffer _;

    public ByteBuffer append(ByteBuffer data)
    {
        ByteBuffer nb = ByteBuffer.allocate(calculateSize(data));
        if (notNull())
        {
            nb.put(_);
            clear();
        }
        nb.put(data);
        return nb;
    }

    public void set(ByteBuffer data)
    {
        if (data.hasRemaining())
        {
            _ = ByteBuffer.allocate(data.remaining());
            _.put(data);
            _.rewind();
        }
    }

    public void clear()
    {
        _ = null;
    }

    /* private */

    private int calculateSize(ByteBuffer data)
    {
        int result = data.limit();
        if (notNull())
        {
            result += _.capacity();
        }
        return result;
    }

    private boolean notNull()
    {
        return _ != null;
    }

    public boolean hasRemaining()
    {
        if (notNull())
        {
            return _.hasRemaining();
        }
        return false;
    }

    public ByteBuffer get()
    {
        return _;
    }
}

@@ -94,17 +94,17 @@
 eBuffer 
-_
+b
 ;%0A%0A    p
@@ -268,17 +268,17 @@
  nb.put(
-_
+b
 );%0A     
@@ -449,17 +449,17 @@
         
-_
+b
  = ByteB
@@ -504,17 +504,17 @@
         
-_
+b
 .put(dat
@@ -529,17 +529,17 @@
         
-_
+b
 .rewind(
@@ -596,17 +596,17 @@
         
-_
+b
  = null;
@@ -775,17 +775,17 @@
 sult += 
-_
+b
 .capacit
@@ -880,17 +880,17 @@
  return 
-_
+b
  != null
@@ -990,17 +990,17 @@
  return 
-_
+b
 .hasRema
@@ -1100,9 +1100,9 @@
 urn 
-_
+b
 ;%0A  

rename field to comply with java8 warnings