net.sf.lpr
Class LPRUtils

java.lang.Object
  extended by net.sf.lpr.LPRUtils

public class LPRUtils
extends java.lang.Object

Utility class for doing some of the dumb work that is needed in more than one place but makes little sense to have in a parent class.

Author:
x_sid

Field Summary
static byte[] ZERO
           
 
Constructor Summary
LPRUtils()
           
 
Method Summary
static void checkResponse(byte[] response, java.lang.Class<?> request)
          This static method is used for clarity in code to provide a way to throw an exception if the response from the server does not match the expected two bytes of zero
static void copy(java.io.InputStream from, java.io.OutputStream to)
          Copies the contents of the InputStream to the OutputStream.
static int generateJobId()
          This method is used to in the high level API classes to generate job ID's that are required by the LPR protocol
static java.lang.String getHostname()
          Does it's best to return a host name for the machine that it is running on.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ZERO

public static final byte[] ZERO
Constructor Detail

LPRUtils

public LPRUtils()
Method Detail

checkResponse

public static void checkResponse(byte[] response,
                                 java.lang.Class<?> request)
                          throws IORuntimeException
This static method is used for clarity in code to provide a way to throw an exception if the response from the server does not match the expected two bytes of zero

Parameters:
response - byte array of response from the server to be compared.
request - Class that is making a call to this method. It is used for the purpose of filling out the exception that can be thrown if the the response from the server does not match.
Throws:
IORuntimeException - if the response does not match the two bytes of zero

copy

public static void copy(java.io.InputStream from,
                        java.io.OutputStream to)
                 throws IORuntimeException
Copies the contents of the InputStream to the OutputStream. A call to this method will create a buffer of 4096 bytes to do the copying. If something like this is needed it would be advisable to use Apache commons IO

Parameters:
from - stream to copy from
to - the stream to copy to.
Throws:
IORuntimeException

generateJobId

public static int generateJobId()
This method is used to in the high level API classes to generate job ID's that are required by the LPR protocol

Returns:
int that is the generated ID between 1 and 999

getHostname

public static java.lang.String getHostname()
Does it's best to return a host name for the machine that it is running on. Since I first implemented this library on Windows XP the way that I get the host name is by using the COMPUTERNAME environment variable. If this returns null then it tries using the HOSTNAME variable that my Fedora 9 box uses to store the host name of the machine. If that fails it then falls back to the InetAddress class and calls the getLocalHost() method and then the getHostName() method. If the final attempt fails because of an exception being thrown then the empty string "" is returned. A patch from someone who knows more about this would be much appreciated.

Returns:
String that is the host name of the machine or "" if it was unable to be retrieved.