package buzzerproxy.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketTimeoutException;
import buzzerproxy.Crypto;
public class ByteProtUtil {
private Socket sock;
private Crypto crypto;
/**
*
*/
public ByteProtUtil(Socket sock) {
this.sock = sock;
}
/**
* @param crypto
*/
public void setCrypt(Crypto crypto) {
this.crypto = crypto;
}
/**
* Read
* - Anzahl
* - Data
* @return
*/
public String readDataW() {
// Lenght
int anz = getInt();
return readData( anz );
}
/**
*
* @return
*/
public String readData(int anz) {
String res = "";
if ( sock != null && sock.isConnected() ) {
try {
InputStream g = sock.getInputStream();
// länge des Strings
if ( anz > 0 ) {
byte[] in = new byte[anz];
g.read( in );
res = new String( in, "ISO-8859-1" ); // bin für UTF8
}
return (crypto != null ? crypto.performDecrypt( res ) : res);
} catch ( Exception e ) {
e.printStackTrace();
return null;
}
}
return null;
}
/**
* Send Data
* - Lenght
* - Data
* @param data
*/
public void sendDataW(String data) {
// Lenght
sendInt( data.length() );
// Daten
sendData( data );
}
/**
* Send Data
* - Lenght
* - Data
* @param data
*/
public void sendDataW(byte[] data) {
// Lenght
sendInt( data.length );
// Daten
sendData( data );
}
/**
* Siehe sendData(byte[] data)
* @param data
*/
public void sendData(String data) {
sendData( data.getBytes() );
}
/**
* Senden
* @param data
*/
public void sendData(byte[] data) {
if ( sock != null && sock.isConnected() ) {
try {
OutputStream os = sock.getOutputStream();
os.write( crypto != null ? crypto.performEncrypt( data ).getBytes() : data );
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
/**
* sending Session ID
* @param i
*/
public void sendSession(long l) {
if ( sock != null && sock.isConnected() ) {
try {
byte[] ba = new byte[8];
for ( int i = 0; i < 64; i += 8 ) {
ba[i >> 3] = new Long( (l & (255L << i)) >> i ).byteValue();
}
sock.getOutputStream().write( ba );
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
/**
*
* @return
* @throws Exception
*/
public long readSession() throws Exception {
try {
InputStream in = sock.getInputStream();
byte[] ba = new byte[8];
long l = 0;
for ( int i = 0; (i < ba.length) && (i < 8); i++ ) {
l |= (((long) ba[i]) << (i << 3)) & (255L << (i << 3));
}
return l;
} catch ( SocketTimeoutException e ) {
throw new Exception();
}
}
/**
* @return
* @throws Exception
*/
public int getInt() {
try {
InputStream in = sock.getInputStream();
byte[] ba = new byte[4];
in.read( ba );
return (ba[0] << 24) + ((ba[1] & 0xFF) << 16) + ((ba[2] & 0xFF) << 8) + (ba[3] & 0xFF);
} catch ( Exception e ) {
}
return 0;
}
/**
* @param in
*/
public void sendInt(int in) {
if ( sock != null && sock.isConnected() ) {
try {
sock.getOutputStream().write( new byte[] { (byte) (in >>> 24), (byte) (in >>> 16), (byte) (in >>> 8), (byte) in } );
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
/**
*
* @return
* @throws Exception
*/
public byte getByte() throws Exception {
try {
InputStream in = sock.getInputStream();
byte[] b = new byte[1];
in.read( b );
return b[0];
} catch ( SocketTimeoutException e ) {
}
return 0;
}
/**
*
* @param by
*/
public void sendByte(byte by) {
if ( sock != null && sock.isConnected() ) {
try {
sock.getOutputStream().write( new byte[] { by } );
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
}