|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.indy.BaseComponent | +--org.indy.IndyComponent | +--org.indy.Connection
Provides extended functionality for using org.indy.IOHandler
s,
such as reading and writing primitives, checking response codes and
extracting RFC compliant messages.
Inner Class Summary | |
protected static interface |
Connection.Capturable
An interface that supports having lines of text input to it. |
Inner classes inherited from class org.indy.IndyComponent |
IndyComponent.WorkMode |
Field Summary | |
protected IOHandler |
ioHandler
The IOHandler used by this connection |
protected RFCReply |
lastCmdResult
Holds the result of the last command for reply checking |
Constructor Summary | |
Connection(IOHandler handler)
Constructs a new instance of TCPConnection |
Method Summary | |
java.lang.String |
allData()
Reads all data from the connection until disconnection occurs, and returns it as a String using the platform character
encoding. |
java.lang.String |
allData(java.lang.String encoding)
Reads all data from the connection until disconnection occurs, and returns it as a String using the character
encoding specified by encoding , or the platform default
if encoding is null . |
int |
capture(java.lang.StringBuffer strings,
java.lang.String delim,
boolean isRFC)
Reads lines of text from the connection into a StringBuffer
until delim is found and returns the number of lines read. |
int |
capture(StringList strings,
java.lang.String delim,
boolean isRFC)
Reads lines of text from the connection until into a StringList
delim is found and returns the number of lines read. |
int |
checkResponse(int response,
int[] allowedResponses)
Checks whether a numeric response code is within a desired list of responses. |
byte[] |
currentReadBuffer()
Returns the current data that can be read without blocking. |
void |
disconnect()
Disconnects this connection. |
void |
disconnectSocket()
|
protected void |
getInternalResponse()
Used to implement the processing required for getResponse(int[]) . |
IOHandler |
getIOHandler()
Gets the iOHandler attribute of the TCPConnection object |
int |
getMaximumLineLength()
Gets the maxLineLength attribute of the IdTCPConnection object |
int |
getReadTimeOut()
Returns the timeout for read operations. |
int |
getResponse(int allowedResponse)
Retreives a response from the connection using getInternalResponse
and checks it for validity. |
int |
getResponse(int[] allowedResponses)
Reads a response from the remote peer, typically of the form of a response code and some text, and checks that the code is within allowedResponses . |
boolean |
isConnected()
Checks whether this TCP connection is still connected. |
protected int |
performCapture(Connection.Capturable dest,
java.lang.String delim,
boolean isRFCMessage)
Reads lines from the connection into dest until delim is hit,
and returns the number of lines read into dest |
int |
readBuffer(byte[] b,
int len)
Fills a buffer, b , with up to len bytes of
data from the connection. |
char |
readChar()
Read a char from the connection |
int |
readInt()
Reads an int from this connection. |
java.lang.String |
readLine()
Reads a line of text from the connection. |
java.lang.String |
readLine(int timeout,
int maxLineLength)
Reads a line of text from the connection. |
java.lang.String |
readLineWait()
DOCUMENT ME! |
java.lang.String |
readLineWait(int failCount)
Invokes readLine and will allow upto
failCount time outs before throwing
ReadTimedOutException . |
long |
readLong()
Reads a long from the connection |
short |
readShort()
Reads a short from the connection |
java.io.InputStream |
readStream(int byteCount,
boolean readUntilDisconnect)
Reads bytes from the socket and returns them in an InputStream. |
protected byte[] |
readTillDisconnect()
DOCUMENT ME! |
protected void |
resetConnection()
Resets this connection |
void |
setIOHandler(IOHandler handler)
Sets the iOHandler attribute of the IdTCPConnection object |
void |
setMaxLineLength(int newLen)
Sets the maxLineLength attribute of the TCPConnection object |
void |
setReadTimeOut(int timeout)
Sets the timeout for read operations. |
void |
write(java.lang.String s)
Write a String to this connection. |
void |
writeBuffer(byte[] buf)
Write a buffer of bytes to this connection. |
void |
writeFile(java.io.File f)
Writes a File object to the stream |
void |
writeHeader(StringList header)
Write the contents of a StringList
as an RFC header, translating strings of the form
|
void |
writeInt(int value)
|
void |
writeLine(java.lang.String line)
Write a line of text to the connection. |
void |
writeLong(long value)
Write a long to the connection |
void |
writeRFCReply(RFCReply reply)
Write an RFCReply to this connection. |
void |
writeRFCStrings(StringList strings)
Write the contents of a StringList instance
to this connection. |
void |
writeShort(short value)
Write a short to the connection |
void |
writeStream(java.io.InputStream stream,
int len,
boolean writeByteCount)
Writes the contents of an InputStream to the connection |
void |
writeStrings(StringList strings,
boolean writeLineCount)
Write a the contents of a StringList instance
to the connection as individual lines. |
Methods inherited from class org.indy.IndyComponent |
addComponentListener, doBeginWork, doBeginWork, doEndWork, doStatus, doStatus, doWork, getLocalName, removeComponentListener |
Methods inherited from class org.indy.BaseComponent |
getVersion |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected IOHandler ioHandler
protected RFCReply lastCmdResult
Constructor Detail |
public Connection(IOHandler handler)
TCPConnection
handler
- The IOHandler for this connection to useMethod Detail |
public void setMaxLineLength(int newLen)
newLen
- The new maxLineLength valuepublic void setReadTimeOut(int timeout) throws IndyIOException
timeout
- The new readTimeOut valuepublic void setIOHandler(IOHandler handler)
handler
- The new iOHandler valuepublic int getMaximumLineLength()
public int getReadTimeOut() throws IndyIOException
public boolean isConnected()
true
if the connection is still active, false otherwise.ConnectionClosedGracefullyException
- If the connection closes gracefullypublic IOHandler getIOHandler()
protected final int performCapture(Connection.Capturable dest, java.lang.String delim, boolean isRFCMessage) throws IndyIOException, ReadTimedOutException, PeerDisconnectedException, MaxLineLengthExceededException, NotConnectedException
dest
until delim
is hit,
and returns the number of lines read into dest
dest
- The Captureable
to add lines todelim
- The delimiter to used demarcate the messageisRFCMessage
- Whether the incoming text should be treated as part of an RFC messageIndyIOException
- if an IO error occursReadTimedOutException
- if the read operation times outPeerDisconnectedException
- if the remote connection disconnectsMaxLineLengthExceededException
- if the maximum line lentgh is exceededNotConnectedException
- if not connectedpublic final int capture(StringList strings, java.lang.String delim, boolean isRFC) throws IndyIOException, ReadTimedOutException, PeerDisconnectedException, MaxLineLengthExceededException, NotConnectedException
StringList
delim is found and returns the number of lines read.strings
- The StringList
to filldelim
- The delimter to read up toisRFC
- Whether the text should be interpreted as part of an RFC messageIndyIOException
- if an IO error occursReadTimedOutException
- if the read operation times outPeerDisconnectedException
- if the remote peer disconnectsMaxLineLengthExceededException
- if the maximum line length is exceededNotConnectedException
- if not connectedpublic final int capture(java.lang.StringBuffer strings, java.lang.String delim, boolean isRFC) throws IndyIOException, ReadTimedOutException, PeerDisconnectedException, MaxLineLengthExceededException, NotConnectedException
StringBuffer
until delim
is found and returns the number of lines read.strings
- The StringBuffer
to filldelim
- the delimiter to look forisRFC
- true
if this should be treated as an RFC messageIndyIOException
- if an IO error occursReadTimedOutException
- if the read operation times outPeerDisconnectedException
- if the remote peer disconnectsMaxLineLengthExceededException
- if the maximum line length is exceededNotConnectedException
- if not connectedprotected void resetConnection()
public void disconnect()
IndyIOException
- If an IO error occurspublic void disconnectSocket()
public int checkResponse(int response, int[] allowedResponses) throws ProtocolException
ProtocolException
is thrown.response
- The response being examinedallowedResponses
- The allowed responsesProtocolException
- if the response is not in the allowed listpublic int getResponse(int[] allowedResponses) throws PeerDisconnectedException, ReadTimedOutException, IndyIOException, ProtocolException, MaxLineLengthExceededException, NotConnectedException
allowedResponses
.
If not a ProtocolException
is thrown, otherwise the response
encountered is returned.allowedResponses
- The response codes allowedPeerDisconnectedException
- If the remote host disconnectsReadTimedOutException
- If the read operation times outIndyIOException
- If an IO error occursProtocolException
- If the response is not in allowed setMaxLineLengthExceededException
- If the maximum line length is exceededNotConnectedException
- If not connectedpublic int getResponse(int allowedResponse) throws PeerDisconnectedException, ReadTimedOutException, IndyIOException, ProtocolException, MaxLineLengthExceededException, NotConnectedException
getInternalResponse
and checks it for validity. This is an alias for getResponse(int[])
allowedResponse
- The expected responsePeerDisconnectedException
- if the remote peer disconnectedReadTimedOutException
- if the read operation timed outIndyIOException
- if an IO error occuredProtocolException
- if the desired response was not returnedMaxLineLengthExceededException
- if the maximum line length was exceededNotConnectedException
- if not connectedprotected void getInternalResponse() throws IndyIOException, ReadTimedOutException, PeerDisconnectedException, MaxLineLengthExceededException, NotConnectedException
getResponse(int[])
.
A response, typically expected to be comptrised of a three digit code and
some text is read and stored using readLineWait()
IndyIOException
- if an IO error occursReadTimedOutException
- if the read operation times outPeerDisconnectedException
- if the remote peer disconnectesMaxLineLengthExceededException
- if the maximum line length is exceededNotConnectedException
- if not connectedpublic java.lang.String readLineWait(int failCount) throws IndyIOException, ReadTimedOutException, PeerDisconnectedException, MaxLineLengthExceededException, NotConnectedException
readLine
and will allow upto
failCount
time outs before throwing
ReadTimedOutException
.failCount
- The number of time outs to allow before failingIndyIOException
- If an IO error occurs.ReadTimedOutException
- If the operation times out more than failCount
timesPeerDisconnectedException
- If the remote peer disconnectsMaxLineLengthExceededException
- If the maximum line length is exceededNotConnectedException
- If not connectedpublic java.lang.String readLineWait() throws IndyIOException, ReadTimedOutException, PeerDisconnectedException, MaxLineLengthExceededException, NotConnectedException
IndyIOException
- DOCUMENT ME!ReadTimedOutException
- DOCUMENT ME!PeerDisconnectedException
- DOCUMENT ME!MaxLineLengthExceededException
- DOCUMENT ME!NotConnectedException
- DOCUMENT ME!public java.lang.String readLine() throws PeerDisconnectedException, ReadTimedOutException, IndyIOException, MaxLineLengthExceededException, NotConnectedException
IOHandler.readLine()
/PeerDisconnectedException
- If the remote peer disconnectsReadTimedOutException
- If the read operation times outIndyIOException
- If an IO error occursMaxLineLengthExceededException
- If the maximum line length is exceededNotConnectedException
- If not connectedpublic java.lang.String readLine(int timeout, int maxLineLength) throws IndyIOException, ReadTimedOutException, PeerDisconnectedException, NotConnectedException, MaxLineLengthExceededException
IOHandler.readLine(int,int)
timeout
- The timeout to allow for this read operationmaxLineLength
- The maximum line lentgh to acceptIndyIOException
- If an IO error occursReadTimedOutException
- If the operation times outPeerDisconnectedException
- If the remote peer disonnectsMaxLineLengthExceededException
- If the maximum line length is exceededNotConnectedException
- If not connectedpublic final int readBuffer(byte[] b, int len) throws PeerDisconnectedException, ReadTimedOutException, IndyIOException, NotConnectedException
b
, with up to len
bytes of
data from the connection.b
- The buffer to filllen
- The amount of data to readPeerDisconnectedException
- If the remote peer disconnectsReadTimedOutException
- If the read operation times outIndyIOException
- If an IOError occursNotConnectedException
- if not connectedpublic final int readInt() throws PeerDisconnectedException, ReadTimedOutException, IndyIOException, NotConnectedException
int
from this connection.int
PeerDisconnectedException
- If the remote peer disconnectsReadTimedOutException
- If the read operation timed outIndyIOException
- If an IO error occurs.NotConnectedException
- If not connectedpublic final long readLong() throws PeerDisconnectedException, ReadTimedOutException, IndyIOException, NotConnectedException
long
from the connectionlong
from the connectionPeerDisconnectedException
- If the remote peer disconnectsReadTimedOutException
- If the read operation times outNotConnectedException
- If not connectedIndyIOException
- If an IO error occurspublic final char readChar() throws PeerDisconnectedException, ReadTimedOutException, IndyIOException, NotConnectedException
char
from the connectionchar
from the connectionPeerDisconnectedException
- If the remote peer disconnectsReadTimedOutException
- If the read operation times outNotConnectedException
- If not connectedIndyIOException
- If an IO error occurspublic final short readShort() throws PeerDisconnectedException, ReadTimedOutException, IndyIOException, NotConnectedException
short
from the connectionPeerDisconnectedException
- If the remote peer disconnectsReadTimedOutException
- If the read operation times outNotConnectedException
- If not connectedIndyIOException
- If an IO error occurspublic final java.io.InputStream readStream(int byteCount, boolean readUntilDisconnect) throws PeerDisconnectedException, ReadTimedOutException, NotConnectedException, IndyIOException
byteCount
- The number of bytes to readreadUntilDisconnect
- Whether byteCount
should be ignored and the read operation should contniue until disconnectedInputStream
containing bytes from the connection.PeerDisconnectedException
- If the remote peer disconnectsReadTimedOutException
- If the read operation times outNotConnectedException
- If not connectedIndyIOException
- If an IO error occurspublic final void writeRFCStrings(StringList strings) throws IndyIOException
StringList
instance
to this connection.
This method ensures that individual lines in strings
beginning
with the response termination character '.' are converted to '..',
and call WriteLn using the updated values. When all lines have been
written, WriteRFCStrings calls WriteLn to send the '.' termination
character to indicate the end of RFC response messagesstrings
- A StringList
to writeIndyIOException
- If an IO error occursNotConnectedException
- If not connectedpublic final void writeRFCReply(RFCReply reply) throws IndyIOException
RFCReply
to this connection.reply
- The RFCReply
to writeIndyIOException
- If an IO error occursNotConnectedException
- If not connectedpublic final void write(java.lang.String s) throws IndyIOException, NotConnectedException
String
to this connection.s
- The String
to writeIndyIOException
- If an IO error occursNotConnectedException
- If not connectedpublic final void writeBuffer(byte[] buf) throws IndyIOException, NotConnectedException
buf
- The bytes to writeIndyIOException
- If an IO error occursNotConnectedException
- If not connectedpublic final void writeLine(java.lang.String line) throws IndyIOException
line
- The text to write as a lineIndyIOException
- If an IO error occursNotConnectedException
- If not connectedpublic final void writeHeader(StringList header) throws IndyIOException
StringList
as an RFC header, translating strings of the form
name=valueto
name:value
header
- The StringList
to write as a headerIndyIOException
- If an IO error occursNotConnectedException
- If not connectedpublic final void writeInt(int value) throws IndyIOException
value
- The int
to writeIndyIOException
- If an IO error occursNotConnectedException
- If not connectedIndyIOException
- If an IO error occursNotConnectedException
- If not connectedpublic final void writeLong(long value) throws IndyIOException
long
to the connectionvalue
- The long
to writeIndyIOException
- If an IO error occursNotConnectedException
- If not connectedpublic final void writeShort(short value) throws IndyIOException
short
to the connectionvalue
- The short
to writeIndyIOException
- If an IO error occursNotConnectedException
- If not connectedpublic final void writeStrings(StringList strings, boolean writeLineCount) throws IndyIOException
StringList
instance
to the connection as individual lines.strings
- The StringList
to writewriteLineCount
- Whether to prepend the text with a int representing the number of linesIndyIOException
- If an IO error occursNotConnectedException
- If not connectedpublic final void writeStream(java.io.InputStream stream, int len, boolean writeByteCount) throws IndyIOException
InputStream
to the connectionstream
- The InputStream
to writelen
- The number of bytes to writewriteByteCount
- Whether the bytes should be prepended with an int representing the byte countIndyIOException
- If an IO error occursNotConnectedException
- If not connectedpublic final void writeFile(java.io.File f) throws java.io.FileNotFoundException, IndyIOException
File
object to the streamf
- The File
to writejava.io.FileNotFoundException
- If the File
doesn't existIndyIOException
- If an IO error occursNotConnectedException
- If not connectedpublic final byte[] currentReadBuffer() throws PeerDisconnectedException, IndyIOException
IOHandler.currentReadBuffer()
PeerDisconnectedException
- If the remote peer has disconnectedReadTimedOutException
- If the read operation times outIndyIOException
- If an IO error occursNotConnectedException
- If not connectedprotected byte[] readTillDisconnect() throws PeerDisconnectedException, ReadTimedOutException, IndyIOException
PeerDisconnectedException
- DOCUMENT ME!ReadTimedOutException
- DOCUMENT ME!IndyIOException
- DOCUMENT ME!public java.lang.String allData() throws ReadTimedOutException, IndyIOException, NotConnectedException
String
using the platform character
encoding.
This is equivalent to allData(null)
.ReadTimedOutException
- If the read operation times outIndyIOException
- If an IO error occursNotConnectedException
- If not connectedallData(String)
public java.lang.String allData(java.lang.String encoding) throws IndyIOException, ReadTimedOutException, NotConnectedException
String
using the character
encoding specified by encoding
, or the platform default
if encoding
is null
.encoding
- The character encoding to useIndyIOException
- If an IO error occursReadTimedOutException
- If the read operation times outNotConnectedException
- If not connected
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |