/*
 * Class: ByteArrayDataWriter
 *
 * Author: Alex Usun
 *
 * Date: 1/9/2005
 */
package com.jscape.inet.ssh.util.keyreader.putty;

import java.io.ByteArrayOutputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Putty format byte array data writer.
 *
 * @author Alex Usun
 * @version 1.0
 * @see <a href="ByteArrayDataWriter.java.html">Source code</a>
 */
public class ByteArrayDataWriter {

    private final ByteArrayOutputStream buffer;
    private final DataOutput out;

    /**
     * Creates a new writer instance.
     */
    public ByteArrayDataWriter() {
        this.buffer = new ByteArrayOutputStream();
        this.out = new DataOutputStream(this.buffer);
    }

    /**
     * Writes integer value.
     *
     * @param value a value to write
     *
     * @throws IOException if an I/O error occurs
     */
    public void writeInt(int value)
            throws IOException {
        this.out.writeInt(value);
    }

    /**
     * Returns underlayed byte buffer and resets the stream.
     *
     * @return underlayed byte buffer
     */
    public byte[] toByteArray() {
        byte[] data = this.buffer.toByteArray();
        this.buffer.reset();

        return data;
    }

}
