public String readString(int length) throws EdbcEx { String str; if (length <= in_msg_len) // Is entire string available? if (length <= 0) str = new String(); else { str = in.readString(length, char_set); in_msg_len -= length; } else { /* ** Entire string is not available. Collect the fragments ** (readBytes() does this automatically) and convert to string. */ byte buff[] = readBytes(length); try { str = char_set.getString(buff); } catch (Exception e) { // Should not happen! throw new EdbcEx(title + ": character encoding failed"); } } return (str); } // readString
protected void close() { super.close(); try { while (in.receive() != JDBC_TL_DC) ; if (trace.enabled(2)) trace.write(title + ": Disconnect Confirm received"); } catch (Exception ignore) { } return; } // close
public int readBytes(byte bytes[], int offset, int length) throws EdbcEx { int len, total = 0; while (length > 0) { need(length, false); len = Math.min(in_msg_len, length); len = in.readBytes(bytes, offset, len); offset += len; length -= len; in_msg_len -= len; total += len; } return (total); } // readBytes
public int skip(int length) throws EdbcEx { int total = 0; if (trace.enabled(3)) trace.write(title + ": skipping " + length + " bytes"); while (length > 0) { int len; need(length, false); len = Math.min(in_msg_len, length); len = in.skip(len); length -= len; in_msg_len -= len; total += len; } return (total); } // skip
protected void disconnect() { /* ** We don't set the input buffer reference to null ** here so that we don't have to check it on each ** use. I/O buffer functions will continue to work ** until a request results in a stream I/O request, ** in which case an exception will be thrown by the ** I/O buffer. ** ** We must, however, test the reference for null ** since we may be called by the constructor with ** a null input buffer. */ if (in != null) { try { in.close(); } catch (Exception ignore) { } } super.disconnect(); return; } // disconnect
public double readDouble() throws EdbcEx { need(JDBC_TL_F8_LEN, true); in_msg_len -= JDBC_TL_F8_LEN; return (in.readDouble()); } // readDouble
public float readFloat() throws EdbcEx { need(JDBC_TL_F4_LEN, true); in_msg_len -= JDBC_TL_F4_LEN; return (in.readFloat()); } // readFloat
public int readInt() throws EdbcEx { need(4, true); in_msg_len -= 4; return (in.readInt()); } // readInt
public short readShort() throws EdbcEx { need(2, true); in_msg_len -= 2; return (in.readShort()); } // readShort
public byte readByte() throws EdbcEx { need(1, true); in_msg_len -= 1; return (in.readByte()); } // readByte
public byte receive() throws EdbcEx { int msg_id; if (trace.enabled(3)) trace.write(title + ": reading next message"); /* ** Check to see if message header is in the input ** buffer (handles message concatenation). */ while (in.avail() < 8) { short tl_id; /* ** Headers are not split across continued messages. ** Data remaining in the current buffer indicates ** a message processing error. */ if (in.avail() > 0) { if (trace.enabled(1)) trace.write(title + ": invalid header length " + in.avail() + " bytes"); disconnect(); throw EdbcEx.get(E_IO0002_PROTOCOL_ERR); } try { tl_id = in.receive(); } catch (EdbcEx ex) { disconnect(); throw ex; } if (tl_id != JDBC_TL_DT) { if (trace.enabled(1)) trace.write(title + ": invalid TL packet ID 0x" + Integer.toHexString(tl_id)); disconnect(); throw EdbcEx.get(E_IO0002_PROTOCOL_ERR); } } /* ** Read and validate message header. */ if ((msg_id = in.readInt()) != JDBC_MSG_ID) { if (trace.enabled(1)) trace.write(title + ": invalid header ID 0x" + Integer.toHexString(msg_id)); disconnect(); throw EdbcEx.get(E_IO0002_PROTOCOL_ERR); } in_msg_len = in.readShort(); in_msg_id = in.readByte(); in_msg_flg = in.readByte(); if (trace.enabled(2)) trace.write( title + ": received message " + IdMap.map(in_msg_id, MsgConst.msgMap) + " length " + in_msg_len + ((in_msg_flg & JDBC_MSG_EOD) == 0 ? "" : " EOD") + ((in_msg_flg & JDBC_MSG_EOG) == 0 ? "" : " EOG")); return (in_msg_id); } // receive
protected void setBuffSize(int size) { in.setBuffSize(size); super.setBuffSize(size); return; } // setBuffSize
protected DbConnIn(String host_id, byte info[], short info_len) throws EdbcEx { super(host_id, info, info_len); try { in = new InBuff(socket.getInputStream(), conn_id, 1 << JDBC_TL_PKT_MIN); } catch (Exception ex) { if (trace.enabled(1)) trace.write(title + ": error creating input buffer: " + ex.getMessage()); disconnect(); throw EdbcEx.get(E_IO0001_CONNECT_ERR, ex); } String cs = null; // Character set connection parameter int size = JDBC_TL_PKT_MIN; short tl_id; try { if (trace.enabled(2)) trace.write(title + ": reading Connection Confirmation"); if ((tl_id = in.receive()) != JDBC_TL_CC) { if (trace.enabled(1)) trace.write(title + ": invalid TL CC packet ID " + tl_id); throw EdbcEx.get(E_IO0001_CONNECT_ERR); } /* ** Read negotiated values from server (if available). */ while (in.avail() >= 2) { byte param_id = in.readByte(); short param_len = (short) (in.readByte() & 0xff); if (param_len == 255) if (in.avail() >= 2) param_len = in.readShort(); else throw EdbcEx.get(E_IO0001_CONNECT_ERR); if (in.avail() < param_len) throw EdbcEx.get(E_IO0001_CONNECT_ERR); switch (param_id) { case JDBC_TL_CP_PROTO: if (param_len == 1) tl_proto = in.readByte(); else throw EdbcEx.get(E_IO0001_CONNECT_ERR); break; case JDBC_TL_CP_SIZE: if (param_len == 1) size = in.readByte(); else throw EdbcEx.get(E_IO0001_CONNECT_ERR); break; case JDBC_TL_CP_CHRSET: /* ** !!!!! FIX-ME !!!!! ** Bootstrapping character set will fail if DBMS ** character set and driver default encoding are ** not minimally compatible. */ { byte ba[] = new byte[param_len]; if (in.readBytes(ba, 0, param_len) == param_len) { try { cs = new String(ba, "US-ASCII"); } catch (Exception ex) { if (trace.enabled(1)) trace.write(title + ": TL CP CHARSET: " + ex.toString()); throw EdbcEx.get(E_IO0001_CONNECT_ERR); } } else throw EdbcEx.get(E_IO0001_CONNECT_ERR); } break; case JDBC_TL_CP_MSG: if (info == null || param_len > info.length || in.readBytes(info, 0, param_len) != param_len) throw EdbcEx.get(E_IO0001_CONNECT_ERR); break; default: if (trace.enabled(1)) trace.write(title + ": TL CR param ID " + param_id); throw EdbcEx.get(E_IO0001_CONNECT_ERR); } } if (in.avail() > 0) throw EdbcEx.get(E_IO0001_CONNECT_ERR); /* ** Validate message parameters. */ if (tl_proto < JDBC_TL_PROTO_1 || tl_proto > JDBC_TL_DFLT_PROTO || size < JDBC_TL_PKT_MIN || size > JDBC_TL_PKT_MAX) { if (trace.enabled(1)) trace.write(title + ": invalid TL parameter: protocol " + tl_proto + ", size " + size); throw EdbcEx.get(E_IO0001_CONNECT_ERR); } try { char_set = CharSet.getCharSet(cs); } catch (Exception ex) { if (trace.enabled(1)) trace.write(title + ": incompatible character set: " + cs); throw EdbcEx.get(E_IO0001_CONNECT_ERR); } } catch (EdbcEx ex) { if (trace.enabled(1)) trace.write(title + ": error negotiating parameters"); disconnect(); throw ex; } /* ** We initialized with minimum buffer size, then requested ** maximum size for the connection. If the server accepts ** anything more than minimum, adjust the buffers accordingly. */ if (size != JDBC_TL_PKT_MIN) setBuffSize(1 << size); if (trace.enabled(3)) { trace.write(title + ": TL connection parameters negotiated"); trace.write(" TL protocol level : " + tl_proto); trace.write(" TL buffer size : " + (1 << size)); trace.write(" Character encoding: " + cs + " => " + char_set); } } // DbConnIn