net.i2p.util

## Class ByteArrayStream

- java.lang.Object
- - java.io.OutputStream
  - - java.io.ByteArrayOutputStream
    - - net.i2p.util.ByteArrayStream

- All Implemented Interfaces:
    Closeable, Flushable, AutoCloseable

      public class ByteArrayStream
      extends ByteArrayOutputStream

  OutputStream to InputStream adapter. Zero-copy where possible. Unsynchronized. This is NOT a Pipe. Do NOT reset after writing.

- - ### Field Summary

    - ### Fields inherited from class java.io.ByteArrayOutputStream

      `buf, count`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ByteArrayStream()` Creates a new byte array output stream.
    `ByteArrayStream(int size)` Creates a new byte array output stream with the specified size.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `ByteArrayInputStream` | `asInputStream()` Creates an input stream from the written data.
    `void` | `copyTo(byte[] target, int offset)` Copies all written data to the target array.
    `void` | `reset()` Resets this stream to the beginning.
    `Signature` | `sign(I2PAppContext ctx, SigningPrivateKey signingKey)` Signs the written data.
    `Signature` | `sign(SigningPrivateKey signingKey)` Signs the written data.
    `byte[]` | `toByteArray()` Returns the byte array containing the written data.
    `boolean` | `verifySignature(I2PAppContext ctx, Signature signature, SigningPublicKey verifyingKey)` Verifies the signature of the written data.
    `boolean` | `verifySignature(Signature signature, SigningPublicKey verifyingKey)` Verifies the signature of the written data.

    - ### Methods inherited from class java.io.ByteArrayOutputStream

      `close, size, toString, toString, toString, write, write, writeTo`

    - ### Methods inherited from class java.io.OutputStream

      `flush, write`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### ByteArrayStream

          public ByteArrayStream()

      Creates a new byte array output stream.

    - #### ByteArrayStream

          public ByteArrayStream(int size)

      Creates a new byte array output stream with the specified size.
      Parameters:
        `size` - the initial size of the internal buffer

  - ### Method Detail

    - #### asInputStream

          public ByteArrayInputStream asInputStream()

      Creates an input stream from the written data. All data previously written. Zero-copy. Not a Pipe. Data written after this call will not appear.
      Returns:
        an input stream containing the written data

    - #### copyTo

          public void copyTo(byte[] target,
                             int offset)

      Copies all written data to the target array.
      Parameters:
        `target` - the target array to copy to
        `offset` - the offset in the target array

    - #### reset

          public void reset()

      Resets this stream to the beginning.
      Overrides:
        `reset` in class `ByteArrayOutputStream`
      Throws:
        `IllegalStateException` - if previously written

    - #### sign

          public Signature sign(I2PAppContext ctx,
                                SigningPrivateKey signingKey)

      Signs the written data.
      Parameters:
        `ctx` - the application context
        `signingKey` - the private key to sign with
      Returns:
        the signature, or null on error

    - #### sign

          public Signature sign(SigningPrivateKey signingKey)

      Signs the written data.
      Parameters:
        `signingKey` - the private key to sign with
      Returns:
        the signature, or null on error

    - #### toByteArray

          public byte[] toByteArray()

      Returns the byte array containing the written data. Zero-copy only if the data fills the buffer. Use asInputStream() for guaranteed zero-copy.
      Overrides:
        `toByteArray` in class `ByteArrayOutputStream`
      Returns:
        the byte array containing the written data

    - #### verifySignature

          public boolean verifySignature(I2PAppContext ctx,
                                         Signature signature,
                                         SigningPublicKey verifyingKey)

      Verifies the signature of the written data.
      Parameters:
        `ctx` - the application context
        `signature` - the signature to verify
        `verifyingKey` - the public key to verify with
      Returns:
        true if the signature is valid

    - #### verifySignature

          public boolean verifySignature(Signature signature,
                                         SigningPublicKey verifyingKey)

      Verifies the signature of the written data.
      Parameters:
        `signature` - the signature to verify
        `verifyingKey` - the public key to verify with
      Returns:
        true if the signature is valid