Example #1
0
  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
Example #2
0
  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