}
private boolean processServer(InputStream inStream, OutputStream outStream) throws IOException {
DataInputStream dataIn = new DataInputStream(inStream);
ByteArrayOutputStream buff = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(buff);
int x = dataIn.read();
if (x < 0) {
println("end");
return false;
}
// System.out.println(" x=" + (char)x+" " +x);
dataOut.write(x);
int len = dataIn.readInt();
dataOut.writeInt(len);
len -= 4;
byte[] data = new byte[len];
dataIn.readFully(data, 0, len);
dataOut.write(data);
dataIn = new DataInputStream(new ByteArrayInputStream(data, 0, len));
switch (x) {
case 'R': {
println("Authentication");
int value = dataIn.readInt();
if (value == 0) {
println(" Ok");
} else if (value == 2) {
println(" KerberosV5");
} else if (value == 3) {
println(" CleartextPassword");
} else if (value == 4) {
println(" CryptPassword");
byte b1 = dataIn.readByte();
byte b2 = dataIn.readByte();
println(" salt1=" + b1 + " salt2=" + b2);
} else if (value == 5) {
println(" MD5Password");
byte b1 = dataIn.readByte();
byte b2 = dataIn.readByte();
byte b3 = dataIn.readByte();
byte b4 = dataIn.readByte();
println(" salt1=" + b1 + " salt2=" + b2 + " 3=" + b3 + " 4=" + b4);
} else if (value == 6) {
println(" SCMCredential");
}
break;
}
case 'K': {
println("BackendKeyData");
println(" process ID " + dataIn.readInt());
println(" key " + dataIn.readInt());
break;
}
case '2': {
println("BindComplete");
break;
}
case '3': {
println("CloseComplete");
break;
}
case 'C': {
println("CommandComplete");
println(" command tag: " + readStringNull(dataIn));
break;
}
case 'd': {
println("CopyData");
break;
}
case 'c': {
println("CopyDone");
break;
}
case 'G': {
println("CopyInResponse");
println(" format: " + dataIn.readByte());
int columns = dataIn.readShort();
for (int i = 0; i < columns; i++) {
println(" formatCode[" + i + "]: " + dataIn.readShort());
}
break;
}
case 'H': {
println("CopyOutResponse");
println(" format: " + dataIn.readByte());
int columns = dataIn.readShort();
for (int i = 0; i < columns; i++) {
println(" formatCode[" + i + "]: " + dataIn.readShort());
}
break;
}
case 'D': {
println("DataRow");
int columns = dataIn.readShort();
println(" columns : " + columns);
for (int i = 0; i < columns; i++) {
int l = dataIn.readInt();
if (l > 0) {
for (int j = 0; j < l; j++) {
dataIn.readByte();
}
}
// println(" ["+i+"] len: " + l);
}
break;
}
case 'I': {
println("EmptyQueryResponse");
break;
}
case 'E': {
println("ErrorResponse");
while (true) {
int fieldType = dataIn.readByte();
if (fieldType == 0) {
break;
}
String msg = readStringNull(dataIn);
// http://developer.postgresql.org/pgdocs/postgres/protocol-error-fields.html
// S Severity
// C Code: the SQLSTATE code
// M Message
// D Detail
// H Hint
// P Position
// p Internal position
// q Internal query
// W Where
// F File
// L Line
// R Routine
println(" fieldType: " + fieldType + " msg: " + msg);
}
break;
}
case 'V': {
println("FunctionCallResponse");
int resultLen = dataIn.readInt();
println(" len: " + resultLen);
break;
}
case 'n': {
println("NoData");
break;
}
case 'N': {
println("NoticeResponse");
while (true) {
int fieldType = dataIn.readByte();
if (fieldType == 0) {
break;
}
String msg = readStringNull(dataIn);
// http://developer.postgresql.org/pgdocs/postgres/protocol-error-fields.html
// S Severity
// C Code: the SQLSTATE code
// M Message
// D Detail
// H Hint
// P Position
// p Internal position
// q Internal query
// W Where
// F File
// L Line
// R Routine
println(" fieldType: " + fieldType + " msg: " + msg);
}
break;
}
case 'A': {
println("NotificationResponse");
println(" processID: " + dataIn.readInt());
println(" condition: " + readStringNull(dataIn));
println(" information: " + readStringNull(dataIn));
break;
}
case 't': {
println("ParameterDescription");
println(" processID: " + dataIn.readInt());
int count = dataIn.readShort();
for (int i = 0; i < count; i++) {
println(" [" + i + "] objectId: " + dataIn.readInt());
}
break;
}
case 'S': {
println("ParameterStatus");
println(" parameter " + readStringNull(dataIn) + " = " + readStringNull(dataIn));
break;
}
case '1': {
println("ParseComplete");
break;
}
case 's': {
println("ParseComplete");
break;
}
case 'Z': {
println("ReadyForQuery");
println(" status (I:idle, T:transaction, E:failed): " + (char) dataIn.readByte());
break;
}
case 'T': {
println("RowDescription");
int columns = dataIn.readShort();
println(" columns : " + columns);
for (int i = 0; i < columns; i++) {
println(" [" + i + "]");
println(" name:" + readStringNull(dataIn));
println(" tableId:" + dataIn.readInt());
println(" columnId:" + dataIn.readShort());
println(" dataTypeId:" + dataIn.readInt());
println(" dataTypeSize (pg_type.typlen):" + dataIn.readShort());
println(" modifier (pg_attribute.atttypmod):" + dataIn.readInt());
println(" format code:" + dataIn.readShort());
}
break;
}
default:
println("############## UNSUPPORTED: " + (char) x);
}
dataOut.flush();
byte[] buffer = buff.toByteArray();
printData(buffer, buffer.length);
try {
outStream.write(buffer, 0, buffer.length);
outStream.flush();