DNet
Class DataConnection

java.lang.Object
  |
  +--DNet.DataConnection

public class DataConnection
extends java.lang.Object

This class is used to represent a connection. It manages all I/O between the client and the connection [Proxy Pattern]. The class is not synchronized - it is assumed that only one client will be using the connection.


Field Summary
private  java.net.Socket connection
           
private  java.io.PushbackInputStream input
           
private  java.io.DataOutputStream output
           
private  long timeOut
           
private  long timeOutStart
           
 
Constructor Summary
DataConnection(java.net.Socket connection)
           
 
Method Summary
 int available()
           
 boolean close()
           
 int getByte()
          Reads one byte.
 int getBytes(byte[] buffer)
          Read bytes into the array.
 byte[] getBytes(int size)
          Read a number of bytes.
 Data getData(int size)
           
 java.lang.String getHostAddress()
           
 java.lang.String getIP()
           
 java.lang.String getLine()
          Read a line.
 java.lang.String getLine(char firstChar)
          Read a line
 java.util.LinkedList getLines()
          Read lines, terminated by an empty line.
 java.util.LinkedList getLines(char firstChar)
          Read lines, terminated by an empty
 int getPort()
           
private  void initTimeOut()
          Resets timeout
private  boolean isTimeOut()
          Has a timeout occured
 Data peekData(int size)
           
 boolean peekString(java.lang.String s, boolean ignoreCase)
           
 void setIdleTimeout(int timeoutSecs)
           
private  boolean waitForByte()
           
private  boolean waitForBytes(int size)
          Wait for a number of bytes to be available
 void write(byte[] b)
           
 void write(byte[] b, int offset, int pos)
           
 void write(int i)
           
 void writeBytes(java.lang.String s)
           
 void writeCRLF()
           
 void writeLFLF()
           
 void writeLine(java.lang.String s)
           
 void writeLines(java.util.LinkedList lines)
           
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

connection

private java.net.Socket connection

input

private java.io.PushbackInputStream input

output

private java.io.DataOutputStream output

timeOutStart

private long timeOutStart

timeOut

private long timeOut
Constructor Detail

DataConnection

public DataConnection(java.net.Socket connection)
               throws java.io.IOException
Method Detail

setIdleTimeout

public void setIdleTimeout(int timeoutSecs)

getHostAddress

public java.lang.String getHostAddress()

close

public boolean close()

available

public int available()

getIP

public java.lang.String getIP()

getPort

public int getPort()

getData

public Data getData(int size)
             throws java.io.IOException

peekData

public Data peekData(int size)
              throws java.io.IOException

peekString

public boolean peekString(java.lang.String s,
                          boolean ignoreCase)

getLine

public java.lang.String getLine()
                         throws java.io.IOException
Read a line. The newline delimiter is CDxx (usual CDLF)
Returns:
The line

getLine

public java.lang.String getLine(char firstChar)
                         throws java.io.IOException
Read a line
Parameters:
firstChar - The first of the newline delimiters. The second is assumed existing, but ignored. The usual parameters are 0x0D (CDxx, CDLF) or 0x0A (LFxx, LFLF)
Returns:
The line

getLines

public java.util.LinkedList getLines()
                              throws java.io.IOException
Read lines, terminated by an empty line. The newline delimiter is CDxx (usual CDLF)
Returns:
The lines

getLines

public java.util.LinkedList getLines(char firstChar)
                              throws java.io.IOException
Read lines, terminated by an empty
Parameters:
firstChar - The first of the newline delimiters. The second is assumed existing, but ignored. The usual parameters are 0x0D (CDxx, CDLF) or 0x0A (LFxx, LFLF)
Returns:
The lines

getBytes

public byte[] getBytes(int size)
                throws java.io.IOException
Read a number of bytes. Non-hanging
Parameters:
size - The number of wanted bytes
Returns:
The array of read bytes

getBytes

public int getBytes(byte[] buffer)
             throws java.io.IOException
Read bytes into the array. Non-blocking
Parameters:
buffer - The array
Returns:
Number of read bytes

getByte

public int getByte()
            throws java.io.IOException
Reads one byte. Non-hanging
Returns:
The byte read, -1 if none

initTimeOut

private void initTimeOut()
Resets timeout

isTimeOut

private boolean isTimeOut()
Has a timeout occured

waitForBytes

private boolean waitForBytes(int size)
Wait for a number of bytes to be available
Parameters:
size - The wanted number of bytes
Returns:
true if the number of bytes are available, false if there was a timeout

waitForByte

private boolean waitForByte()

writeLines

public void writeLines(java.util.LinkedList lines)
                throws java.io.IOException

writeLine

public void writeLine(java.lang.String s)
               throws java.io.IOException

writeCRLF

public void writeCRLF()
               throws java.io.IOException

writeLFLF

public void writeLFLF()
               throws java.io.IOException

writeBytes

public void writeBytes(java.lang.String s)
                throws java.io.IOException

write

public void write(byte[] b,
                  int offset,
                  int pos)
           throws java.io.IOException

write

public void write(int i)
           throws java.io.IOException

write

public void write(byte[] b)
           throws java.io.IOException