001/* SSLEngineResult.java -- 
002   Copyright (C) 2006 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
01902111-1307 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package javax.net.ssl;
040
041/**
042 * A result from an {@link SSLEngine} <code>wrap</code> or
043 * <code>unwrap</code> operation. This class conveys a possibly
044 * intermediate result, and may ask for more input data or request
045 * that output data be sent over a connection.
046 */
047public class SSLEngineResult
048{
049  private final HandshakeStatus handshakeStatus;
050  private final Status status;
051  private final int bytesConsumed;
052  private final int bytesProduced;
053
054  /**
055   * Creates a new SSL engine result.
056   *
057   * @param status The status of the SSL connection.
058   * @param handshakeStatus The status of the SSL handshake.
059   * @param bytesConsumed The number of bytes consumed by the previous
060   * operation.
061   * @param bytesProduced The number of bytes produced by the previous
062   * operation.
063   * @throws IllegalArgumentException If either enum value is
064   * <code>null</code>, or if either integer is negative.
065   */
066  public SSLEngineResult (Status status, HandshakeStatus handshakeStatus,
067                          int bytesConsumed, int bytesProduced)
068  {
069    if (status == null)
070      throw new IllegalArgumentException ("'status' may not be null");
071    if (handshakeStatus == null)
072      throw new IllegalArgumentException ("'handshakeStatus' may not be null");
073    if (bytesConsumed < 0)
074      throw new IllegalArgumentException ("'bytesConumed' must be nonnegative");
075    if (bytesProduced < 0)
076      throw new IllegalArgumentException ("'bytesProduced' must be nonnegative");
077    this.status = status;
078    this.handshakeStatus = handshakeStatus;
079    this.bytesConsumed = bytesConsumed;
080    this.bytesProduced = bytesProduced;
081  }
082
083
084
085  /**
086   * An enumeration of possible general states.
087   */
088  public static enum Status
089  {
090
091    /**
092     * There were not enough input bytes available to complete the
093     * operation.
094     */
095    BUFFER_UNDERFLOW,
096
097    /**
098     * There was not enough space for the output message.
099     */
100    BUFFER_OVERFLOW,
101
102    /**
103     * Okay. No error.
104     */
105    OK,
106
107    /**
108     * The connection is closed.
109     */
110    CLOSED
111  }
112
113  /**
114   * An enumeration of possible handshake status states.
115   */
116  public static enum HandshakeStatus
117  {
118
119    /**
120     * Not currently handshaking.
121     */
122    NOT_HANDSHAKING,
123
124    /**
125     * The handshake is finished.
126     */
127    FINISHED,
128
129    /**
130     * Needs the status of one or more delegated tasks.
131     */
132    NEED_TASK,
133
134    /**
135     * Has data prepared for output, and needs a new call to
136     * <code>wrap</code>.
137     */
138    NEED_WRAP,
139
140    /**
141     * Is waiting for more input.
142     */
143    NEED_UNWRAP
144  }
145
146
147
148  /**
149   * Returns the number of bytes consumed by the previous operation.
150   *
151   * @return The number of bytes consumed.
152   */
153  public int bytesConsumed ()
154  {
155    return bytesConsumed;
156  }
157
158  /**
159   * Returns the number of bytes produced by the previous operation.
160   *
161   * @return The number of bytes produced.
162   */
163  public int bytesProduced ()
164  {
165    return bytesProduced;
166  }
167
168  /**
169   * Returns the handshake status.
170   *
171   * @return The handshake status.
172   */
173  public HandshakeStatus getHandshakeStatus ()
174  {
175    return handshakeStatus;
176  }
177
178  /**
179   * Returns the connection status.
180   *
181   * @return The connection status.
182   */
183  public Status getStatus ()
184  {
185    return status;
186  }
187
188  public String toString ()
189  {
190    return (super.toString () + " [ status: " + status + "; handshakeStatus: "
191            + handshakeStatus + "; bytesConsumed: " + bytesConsumed
192            + "; bytesProduced: " + bytesProduced + " ]");
193  }
194}