/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intel.chimera.input;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

/**
 * The StreamInput class takes a <code>InputStream</code> object and
 * wraps it as <code>Input</code> object acceptable by <code>CryptoInputStream</code>.
 */
public class StreamInput implements Input {
  private byte[] buf;
  private int bufferSize;
  InputStream in;

  public StreamInput(InputStream inputStream, int bufferSize) {
    this.in = inputStream;
    this.bufferSize = bufferSize;
  }

  @Override
  public int read(ByteBuffer dst) throws IOException {
    final int remaining = dst.remaining();
    final byte[] tmp = getBuf();
    int total = 0;
    while (remaining > total) {
      final int n = in.read(tmp, 0, Math.min(remaining, bufferSize));
      if (n == -1) {
        if (total == 0) {
          total = -1;
        }
        break;
      } else if (n > 0) {
        dst.put(tmp, 0, n);
        total += n;
      }
    }
    return total;
  }

  @Override
  public long skip(long n) throws IOException {
    return in.skip(n);
  }

  @Override
  public int available() throws IOException {
    return in.available();
  }

  @Override
  public void close() throws IOException {
    in.close();
  }
  
  private byte[] getBuf() {
    if (buf == null) {
      buf = new byte[bufferSize];
    }
    return buf;
  }
}

@@ -1391,30 +1391,24 @@
 ption %7B%0A    
-final 
 int remainin
@@ -1469,21 +1469,20 @@
     int 
-total
+read
  = 0;%0A  
@@ -1502,21 +1502,17 @@
 ining %3E 
-total
+0
 ) %7B%0A    
@@ -1610,21 +1610,20 @@
     if (
-total
+read
  == 0) %7B
@@ -1633,21 +1633,20 @@
         
-total
+read
  = -1;%0A 
@@ -1735,20 +1735,43 @@
     
-total
+read
  += n;%0A
+        remaining -= n;%0A
     
@@ -1795,13 +1795,12 @@
 urn 
-total
+read
 ;%0A  

Fix bug relates to StreamInput. This closes #46 from dianfu/bugfix_stream_input