/*
 * UnpaddedByteArrayOutputStream.java
 *
 * Copyright 2011 John W Dawson
 *
 * This code is distributed under the terms of the GNU General Public License, version 3
 *
 * This filter removes zero padding from the end of a stream of bytes 
 */

import java.io.*;
import java.util.*;
public class UnpaddedByteArrayOutputStream extends ByteArrayOutputStream
{
  public UnpaddedByteArrayOutputStream ()
  {
    super ();
  }
  
  // Return contents of buffer as hex string 
  public String toString ()
  {

    // Find last non-zero byte
    int last = count;
    while (buf[--last] == '\000');
    
    // Copy remaining bytes into a new buffer
    byte [] b = new byte [last + 1];
    System.arraycopy (buf, 0, b, 0, last + 1);
    
    // Convert to string
    String s = new String (b);
    
    // Clear the buffer
    this.reset();
    
    return s;
  }
}
     
