Example #1
0
  public void set(Object object, Calendar calendar, int scale) throws SQLException {
    // If it's a byte[] we can check for data truncation.
    if (object instanceof byte[]) {
      byte[] bytes = (byte[]) object;
      truncated_ = (bytes.length > maxLength_ ? bytes.length - maxLength_ : 0);
    } else if (object instanceof String) {
      byte[] bytes = null;
      try {
        bytes = BinaryConverter.stringToBytes((String) object);
      } catch (NumberFormatException nfe) {
        // the String contains non-hex characters
        JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH, nfe);
      }
      object = bytes;
      truncated_ = 0;
      outOfBounds_ = false;
    } else if (object instanceof Reader) {
      int length = scale; // hack to get the length into the set method
      byte[] bytes = null;
      if (length >= 0) {
        try {
          int blockSize =
              length < AS400JDBCPreparedStatement.LOB_BLOCK_SIZE
                  ? length
                  : AS400JDBCPreparedStatement.LOB_BLOCK_SIZE;
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          HexReaderInputStream stream = new HexReaderInputStream((Reader) object);
          byte[] byteBuffer = new byte[blockSize];
          int totalBytesRead = 0;
          int bytesRead = stream.read(byteBuffer, 0, blockSize);
          while (bytesRead > -1 && totalBytesRead < length) {
            baos.write(byteBuffer, 0, bytesRead);
            totalBytesRead += bytesRead;
            int bytesRemaining = length - totalBytesRead;
            if (bytesRemaining < blockSize) {
              blockSize = bytesRemaining;
            }
            bytesRead = stream.read(byteBuffer, 0, blockSize);
          }

          bytes = baos.toByteArray();

          if (bytes.length < length) {
            // a length longer than the stream was specified
            JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH);
          }

          int objectLength = bytes.length;
          if (bytes.length > maxLength_) {
            byte[] newValue = new byte[maxLength_];
            System.arraycopy(bytes, 0, newValue, 0, maxLength_);
            bytes = newValue;
          }
          stream.close(); // @scan1
          object = bytes;
          truncated_ = objectLength - bytes.length;
        } catch (ExtendedIOException eie) {
          // the Reader contains non-hex characters
          JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH, eie);
        } catch (IOException ie) {
          JDError.throwSQLException(JDError.EXC_INTERNAL, ie);
        }
      } else if (length == -2) // @readerlen new else-if block (read all data)
      {
        try {
          int blockSize = AS400JDBCPreparedStatement.LOB_BLOCK_SIZE;
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          HexReaderInputStream stream = new HexReaderInputStream((Reader) object);
          byte[] byteBuffer = new byte[blockSize];
          int totalBytesRead = 0;
          int bytesRead = stream.read(byteBuffer, 0, blockSize);
          while (bytesRead > -1) {
            baos.write(byteBuffer, 0, bytesRead);
            totalBytesRead += bytesRead;

            bytesRead = stream.read(byteBuffer, 0, blockSize);
          }

          bytes = baos.toByteArray();

          int objectLength = bytes.length;
          if (bytes.length > maxLength_) {
            byte[] newValue = new byte[maxLength_];
            System.arraycopy(bytes, 0, newValue, 0, maxLength_);
            bytes = newValue;
          }
          stream.close(); // @scan1
          object = bytes;
          truncated_ = objectLength - bytes.length;
        } catch (ExtendedIOException eie) {
          // the Reader contains non-hex characters
          JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH, eie);
        } catch (IOException ie) {
          JDError.throwSQLException(JDError.EXC_INTERNAL, ie);
        }
      } else {
        JDError.throwSQLException(JDError.EXC_DATA_TYPE_MISMATCH);
      }
    } else if (!(object instanceof String)
        && (JDUtilities.JDBCLevel_ >= 20 && !(object instanceof Blob))
        && !(object instanceof Reader)
        && !(object instanceof InputStream)) {
      JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH);
    }
    savedObject_ = object;
    if (scale != -1) scale_ = scale; // Skip resetting it if we don't know the real length
  }