mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

matthew_swift
05.42.2009 22094368c2865dcfb6daf8366425212b721a4657
opends/src/server/org/opends/server/protocols/asn1/ASN1Writer.java
@@ -22,135 +22,340 @@
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 *      Copyright 2006-2009 Sun Microsystems, Inc.
 */
package org.opends.server.protocols.asn1;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import org.opends.server.types.ByteSequence;
import static org.opends.server.loggers.debug.DebugLogger.*;
import org.opends.server.loggers.debug.DebugTracer;
import org.opends.server.types.DebugLogLevel;
import java.io.Closeable;
import java.io.IOException;
import java.io.Flushable;
/**
 * This class defines a utility that can be used to write ASN.1 elements over a
 * provided socket or output stream.
 * An interface for encoding ASN.1 elements to a data source.
 * <p>
 * Methods for creating {@link ASN1Writer}s are provided in the
 * {@link ASN1} class.
 */
@org.opends.server.types.PublicAPI(
     stability=org.opends.server.types.StabilityLevel.UNCOMMITTED,
     mayInstantiate=true,
     mayExtend=false,
     mayInvoke=true)
public final class ASN1Writer
public interface ASN1Writer extends Closeable, Flushable
{
  /**
   * The tracer object for the debug logger.
   * Closes this ASN.1 writer, flushing it first. Closing a
   * previously closed ASN.1 writer has no effect. Any unfinished
   * sequences and/or sets will be ended.
   *
   * @throws IOException
   *           If an error occurs while closing the writer.
   */
  private static final DebugTracer TRACER = getTracer();
  public void close() throws IOException;
  // The output stream to which the encoded elements should be written.
  private OutputStream outputStream;
  // The socket with which the output stream is associated.
  private Socket socket;
  /**
   * Flushes the writer. If the writer has saved any elements from the
   * previous write methods in a buffer, write them immediately to their
   * intended destination. Then, if that destination is another byte stream,
   * flush it. Thus one flush() invocation will flush all the buffers in a
   * chain of streams.
   * <p/>
   * If the intended destination of this stream is an abstraction provided
   * by the underlying operating system, for example a file, then flushing
   * the stream guarantees only that bytes previously written to the stream
   * are passed to the operating system for writing; it does not guarantee
   * that they are actually written to a physical device such as a disk drive.
   *
   * @throws IOException If an I/O error occurs
   */
  public void flush() throws IOException;
  /**
   * Creates a new ASN.1 writer that will write elements over the provided
   * socket.
   * Writes a boolean element using the Universal Boolean ASN.1 type
   * tag.
   *
   * @param  socket  The socket to use to write ASN.1 elements.
   *
   * @throws  IOException  If a problem occurs while trying to get the output
   *                       stream for the socket.
   * @param booleanValue
   *          The boolean value to write.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  public ASN1Writer(Socket socket)
         throws IOException
  {
    this.socket  = socket;
    outputStream = socket.getOutputStream();
  }
  ASN1Writer writeBoolean(boolean booleanValue) throws IOException;
  /**
   * Creates a new ASN.1 writer that will write elements over the provided
   * output stream.
   * Writes a boolean element using the provided type tag.
   *
   * @param  outputStream  The output stream to use to write ASN.1 elements.
   * @param type
   *          The type tag to use.
   * @param booleanValue
   *          The boolean value to write.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  public ASN1Writer(OutputStream outputStream)
  {
    this.outputStream = outputStream;
    socket            = null;
  }
  ASN1Writer writeBoolean(byte type, boolean booleanValue) throws IOException;
  /**
   * Writes the provided ASN.1 element over the output stream associated with
   * this ASN.1 writer.
   * Finish writing a sequence.
   *
   * @param  element  The element to be written.
   *
   * @return  The number of bytes actually written over the output stream.
   *
   * @throws  IOException  If a problem occurs while trying to write the
   *                       information over the output stream.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  public int writeElement(ASN1Element element)
         throws IOException
  {
    byte[] elementBytes = element.encode();
    outputStream.write(elementBytes);
    outputStream.flush();
    return elementBytes.length;
  }
  ASN1Writer writeEndSequence() throws IOException;
  /**
   * Closes this ASN.1 writer and the underlying output stream/socket.
   * Finish writing a set.
   *
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  public void close()
  {
    try
    {
      outputStream.close();
    }
    catch (Exception e)
    {
      if (debugEnabled())
      {
        TRACER.debugCaught(DebugLogLevel.ERROR, e);
      }
    }
  ASN1Writer writeEndSet() throws IOException;
    if (socket != null)
    {
      try
      {
        socket.close();
      }
      catch (Exception e)
      {
        if (debugEnabled())
        {
          TRACER.debugCaught(DebugLogLevel.ERROR, e);
        }
      }
    }
  }
  /**
   * Writes an integer element using the provided type tag.
   *
   * @param type
   *          The type tag to use.
   * @param intValue
   *          The integer value to write.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeInteger(byte type, int intValue) throws IOException;
  /**
   * Writes an integer element using the provided type tag.
   *
   * @param type
   *          The type tag to use.
   * @param longValue
   *          The integer value to write.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeInteger(byte type, long longValue) throws IOException;
  /**
   * Writes an integer element using the Universal Integer ASN.1 type
   * tag.
   *
   * @param intValue
   *          The integer value to write.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeInteger(int intValue) throws IOException;
  /**
   * Writes an integer element using the Universal Integer ASN.1 type
   * tag.
   *
   * @param longValue
   *          The integer value to write.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeInteger(long longValue) throws IOException;
  /**
   * Writes an enumerated element using the Universal Enumerated ASN.1 type
   * tag.
   *
   * @param intValue
   *          The integer value of the enumeration to write.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeEnumerated(int intValue) throws IOException;
  /**
   * Writes a null element using the Universal Null ASN.1 type tag.
   *
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeNull() throws IOException;
  /**
   * Writes a null element using the provided type tag.
   *
   * @param type
   *          The type tag to use.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeNull(byte type) throws IOException;
  /**
   * Writes an octet string element using the provided type tag and
   * byte array.
   *
   * @param type
   *          The type tag to use.
   * @param value
   *          The byte array containing the data to write.
   * @param offset
   *          The offset in the byte array.
   * @param length
   *          The number of bytes to write.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeOctetString(byte type, byte[] value, int offset, int length)
      throws IOException;
  /**
   * Writes an octet string element using the provided type tag and
   * byte sequence.
   *
   * @param type
   *          The type tag to use.
   * @param value
   *          The byte sequence containing the data to write.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeOctetString(byte type, ByteSequence value) throws IOException;
  /**
   * Writes an octet string element using the provided type tag and
   * the UTF-8 encoded bytes of the provided string.
   *
   * @param type
   *          The type tag to use.
   * @param value
   *          The string to write.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeOctetString(byte type, String value) throws IOException;
  /**
   * Writes an octet string element using the Universal Octet String
   * ASN.1 type tag and the provided byte array.
   *
   * @param value
   *          The byte array containing the data to write.
   * @param offset
   *          The offset in the byte array.
   * @param length
   *          The number of bytes to write.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeOctetString(byte[] value, int offset, int length)
      throws IOException;
  /**
   * Writes an octet string element using the Universal Octet String
   * ASN.1 type tag and the provided byte sequence.
   *
   * @param value
   *          The byte sequence containing the data to write.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeOctetString(ByteSequence value) throws IOException;
  /**
   * Writes an octet string element using the Universal Octet String
   * ASN.1 type tag and the UTF-8 encoded bytes of the provided
   * string.
   *
   * @param value
   *          The string to write.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeOctetString(String value) throws IOException;
  /**
   * Writes a sequence element using the Universal Sequence ASN.1 type
   * tag. All further writes will be part of this set until
   * {@link #writeEndSequence()} is called.
   *
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeStartSequence() throws IOException;
  /**
   * Writes a sequence element using the provided type tag. All
   * further writes will be part of this set until
   * {@link #writeEndSequence()} is called.
   *
   * @param type
   *          The type tag to use.
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeStartSequence(byte type) throws IOException;
  /**
   * Writes a set element using the Universal Set type tag. All
   * further writes will be part of this set until
   * {@link #writeEndSet()} is called.
   *
   * @return a reference to this object.
   * @throws IOException
   *           If an error occurs while writing.
   */
  ASN1Writer writeStartSet() throws IOException;
}