com.sun.cdc.io
Class ConnectionBaseAdapter

java.lang.Object
  extended by com.sun.cdc.io.ConnectionBaseAdapter
All Implemented Interfaces:
ConnectionBaseInterface, Connection, InputConnection, OutputConnection, StreamConnection
Direct Known Subclasses:
BufferedConnectionAdapter

public abstract class ConnectionBaseAdapter
extends Object
implements ConnectionBaseInterface, StreamConnection

Protocol classes extend this class to gain some of the common functionality needed to implement a CDC Generic Connection.

The common functionality includes:

Class Relationship Diagram


Field Summary
protected  boolean connectionOpen
          Flag indicating if the connection is open.
protected static int iStreams
          Number of input streams that were opened.
protected  int maxIStreams
          Maximum number of open input streams.
protected  int maxOStreams
          Maximum number of output streams.
protected static int oStreams
          Number of output streams were opened.
 
Constructor Summary
ConnectionBaseAdapter()
           
 
Method Summary
 int available()
          Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.
protected  void checkForPermission()
          Overridden by Protocols to check for permissions.
 void close()
          Close the connection.
protected  void closeInputStream()
          Called once by each child input stream.
protected  void closeOutputStream()
          Called once by each child output stream.
protected abstract  void connect(String name, int mode, boolean timeouts)
          Connect to a target.
protected abstract  void disconnect()
          Free up the connection resources.
protected  void ensureNoStreamsOpen()
          Check if the streams are open.
protected  void ensureOpen()
          Check if the connection is open.
protected  void flush()
          Forces any buffered output bytes to be written out.
 void mark(int readlimit)
          Marks the current position in input stream for a connection.
 boolean markSupported()
          Tests if input stream for a connection supports the mark and reset methods.
 DataInputStream openDataInputStream()
          Open and return a data input stream for a connection.
 DataOutputStream openDataOutputStream()
          Open and return a data output stream for a connection.
 InputStream openInputStream()
          Returns an input stream.
 OutputStream openOutputStream()
          Returns an output stream.
 Connection openPrim(String name)
          Check for required permission and open a connection to a target.
 Connection openPrim(String name, int mode, boolean timeouts)
          Check for required permission and open a connection to a target.
protected abstract  int readBytes(byte[] b, int off, int len)
          Reads up to len bytes of data from the input stream into an array of bytes, blocks until at least one byte is available.
 void reset()
          Repositions input stream for a connection to the position at the time the mark method was last called on this input stream.
protected abstract  int writeBytes(byte[] b, int off, int len)
          Writes len bytes from the specified byte array starting at offset off to this output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

connectionOpen

protected boolean connectionOpen
Flag indicating if the connection is open.


iStreams

protected static int iStreams
Number of input streams that were opened.


maxIStreams

protected int maxIStreams
Maximum number of open input streams. Set this to zero to prevent openInputStream from giving out a stream in write-only mode.


oStreams

protected static int oStreams
Number of output streams were opened.


maxOStreams

protected int maxOStreams
Maximum number of output streams. Set this to zero to prevent openOutputStream from giving out a stream in read-only mode.

Constructor Detail

ConnectionBaseAdapter

public ConnectionBaseAdapter()
Method Detail

openPrim

public Connection openPrim(String name,
                           int mode,
                           boolean timeouts)
                    throws IOException
Check for required permission and open a connection to a target.

Specified by:
openPrim in interface ConnectionBaseInterface
Parameters:
name - URL for the connection, without the without the protocol part
mode - I/O access mode, see Connector
timeouts - flag to indicate that the caller wants timeout exceptions
Returns:
this Connection object
Throws:
IllegalArgumentException - If a parameter is invalid.
ConnectionNotFoundException - If the connection cannot be found.
IOException - If some other kind of I/O error occurs.

checkForPermission

protected void checkForPermission()
                           throws SecurityException,
                                  InterruptedIOException
Overridden by Protocols to check for permissions. This implementation always throws a security exception. The subclass is responsible for checking permissions and maintaining the state (in private local fields) as to whether it was granted.

Parameters:
token - security token of the calling class or null
name - the URL of the connection without the protocol
Throws:
SecurityException - if permissions are not granted
InterruptedIOException - if I/O associated with permissions is interrupted

openPrim

public Connection openPrim(String name)
                    throws IOException
Check for required permission and open a connection to a target. This method can be used with permissions greater than the current app. Assume read/write and no timeouts.

Parameters:
token - security token of the calling class
name - URL for the connection, without the without the protocol part
Returns:
this Connection object
Throws:
IllegalArgumentException - If a parameter is invalid.
ConnectionNotFoundException - If the connection cannot be found.
IOException - If some other kind of I/O error occurs.

openInputStream

public InputStream openInputStream()
                            throws IOException
Returns an input stream.

Specified by:
openInputStream in interface InputConnection
Returns:
an input stream for writing bytes to this port.
Throws:
IOException - if an I/O error occurs when creating the output stream.

openDataInputStream

public DataInputStream openDataInputStream()
                                    throws IOException
Open and return a data input stream for a connection.

Specified by:
openDataInputStream in interface InputConnection
Returns:
An input stream
Throws:
IOException - If an I/O error occurs

openOutputStream

public OutputStream openOutputStream()
                              throws IOException
Returns an output stream.

Specified by:
openOutputStream in interface OutputConnection
Returns:
an output stream for writing bytes to this port.
Throws:
IOException - if an I/O error occurs when creating the output stream.

openDataOutputStream

public DataOutputStream openDataOutputStream()
                                      throws IOException
Open and return a data output stream for a connection.

Specified by:
openDataOutputStream in interface OutputConnection
Returns:
An input stream
Throws:
IOException - If an I/O error occurs

close

public void close()
           throws IOException
Close the connection.

Specified by:
close in interface Connection
Throws:
IOException - if an I/O error occurs when closing the connection.

closeInputStream

protected void closeInputStream()
                         throws IOException
Called once by each child input stream. If the input stream is marked open, it will be marked closed and the if the connection and output stream are closed the disconnect method will be called.

Throws:
IOException - if the subclass throws one

closeOutputStream

protected void closeOutputStream()
                          throws IOException
Called once by each child output stream. If the output stream is marked open, it will be marked closed and the if the connection and input stream are closed the disconnect method will be called.

Throws:
IOException - if the subclass throws one

ensureOpen

protected void ensureOpen()
                   throws IOException
Check if the connection is open.

Throws:
IOException - is thrown, if the stream is not open.

ensureNoStreamsOpen

protected void ensureNoStreamsOpen()
                            throws IOException
Check if the streams are open.

Throws:
IOException - is thrown, if the stream is still open.

connect

protected abstract void connect(String name,
                                int mode,
                                boolean timeouts)
                         throws IOException
Connect to a target.

Parameters:
name - URL for the connection, without the protocol part
mode - I/O access mode, see Connector
timeouts - flag to indicate that the called wants timeout exceptions
Throws:
IllegalArgumentException - If a parameter is invalid.
ConnectionNotFoundException - If the connection cannot be found.
IOException - If some other kind of I/O error occurs.

disconnect

protected abstract void disconnect()
                            throws IOException
Free up the connection resources.

Throws:
IOException - if an I/O error occurs.

readBytes

protected abstract int readBytes(byte[] b,
                                 int off,
                                 int len)
                          throws IOException
Reads up to len bytes of data from the input stream into an array of bytes, blocks until at least one byte is available.

Parameters:
b - the buffer into which the data is read.
off - the start offset in array b at which the data is written.
len - the maximum number of bytes to read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
IOException - if an I/O error occurs.

available

public int available()
              throws IOException
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. The next caller might be the same thread or another thread. This classes implementation always returns 0. It is up to subclasses to override this method.

Returns:
the number of bytes that can be read from this input stream without blocking.
Throws:
IOException - if an I/O error occurs.

writeBytes

protected abstract int writeBytes(byte[] b,
                                  int off,
                                  int len)
                           throws IOException
Writes len bytes from the specified byte array starting at offset off to this output stream.

Polling the native code is done here to allow for simple asynchronous native code to be written. Not all implementations work this way (they block in the native code) but the same Java code works for both.

Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.
Returns:
number of bytes written
Throws:
IOException - if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.

flush

protected void flush()
              throws IOException
Forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written that have been buffered by the connection, should immediately be written to their intended destination.

The flush method of ConnectionBaseAdapter does nothing.

Throws:
IOException - if an I/O error occurs.

markSupported

public boolean markSupported()
Tests if input stream for a connection supports the mark and reset methods.

The markSupported method of ConnectionBaseAdapter returns false.

Subclasses should override this method if they support own mark/reset functionality.

Returns:
true if input stream for this connection supports the mark and reset methods; false otherwise.
See Also:
InputStream.mark(int), InputStream.reset()

mark

public void mark(int readlimit)
Marks the current position in input stream for a connection. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

The mark method of ConnectionBaseAdapter does nothing.

Subclasses should override this method if they support own mark/reset functionality.

Parameters:
readlimit - the maximum limit of bytes that can be read before the mark position becomes invalid.
See Also:
InputStream.reset()

reset

public void reset()
           throws IOException
Repositions input stream for a connection to the position at the time the mark method was last called on this input stream.

The method reset for ConnectionBaseAdapter class does nothing and always throws an IOException.

Subclasses should override this method if they support own mark/reset functionality.

Throws:
IOException - if this stream has not been marked or if the mark has been invalidated.
See Also:
InputStream.reset(), InputStream.mark(int), IOException