Exemplo n.º 1
0
  public void convertFromRawBytes(byte[] rawBytes, int offset, ConvTable ccsidConverter)
      throws SQLException {
    length_ = BinaryConverter.byteArrayToUnsignedShort(rawBytes, offset);

    int bidiStringType = settings_.getBidiStringType();
    // if bidiStringType is not set by user, use ccsid to get value
    if (bidiStringType == -1) bidiStringType = ccsidConverter.bidiStringType_;
    BidiConversionProperties bidiConversionProperties =
        new BidiConversionProperties(bidiStringType);
    bidiConversionProperties.setBidiImplicitReordering(settings_.getBidiImplicitReordering());
    bidiConversionProperties.setBidiNumericOrderingRoundTrip(settings_.getBidiNumericOrdering());

    value_ =
        ccsidConverter.byteArrayToString(rawBytes, offset + 2, length_, bidiConversionProperties);
  }
Exemplo n.º 2
0
  public void convertToRawBytes(byte[] rawBytes, int offset, ConvTable ccsidConverter)
      throws SQLException {
    //   We originally padded with a single byte space.  We now have the
    //   ccsid so we can figure out if that was right or not.  If we should
    //   have use the double byte space, re-pad.
    int ccsid = ccsidConverter.getCcsid();
    if (ccsid != 13488 && ccsid != 1200) {
      int valueLength = originalValue_.length();
      int exactLength = getDisplaySize();
      if (valueLength < exactLength) {
        StringBuffer buffer = new StringBuffer(originalValue_);
        char c = '\u3000';
        for (int i = valueLength; i < exactLength; ++i) buffer.append(c);
        value_ = buffer.toString();
      }
    }

    int bidiStringType = settings_.getBidiStringType();

    // if bidiStringType is not set by user, use ccsid to get value
    if (bidiStringType == -1) bidiStringType = ccsidConverter.bidiStringType_;

    BidiConversionProperties bidiConversionProperties =
        new BidiConversionProperties(bidiStringType); // @KBA
    bidiConversionProperties.setBidiImplicitReordering(
        settings_.getBidiImplicitReordering()); // @KBA
    bidiConversionProperties.setBidiNumericOrderingRoundTrip(
        settings_.getBidiNumericOrdering()); // @KBA

    try {
      ccsidConverter.stringToByteArray(
          value_,
          rawBytes,
          offset,
          maxLength_,
          bidiConversionProperties); // @KBC changed to use bidiConversionProperties instead of
                                     // bidiStringType
    } catch (CharConversionException e) {
      maxLength_ =
          ccsidConverter.stringToByteArray(value_, bidiConversionProperties)
              .length; // @KBC changed to use bidiConversionProperties instead of bidiStringType
      JDError.throwSQLException(this, JDError.EXC_INTERNAL, e);
    }
  }
Exemplo n.º 3
0
  public void convertFromRawBytes(byte[] rawBytes, int offset, ConvTable ccsidConverter)
      throws SQLException {
    int bidiStringType = settings_.getBidiStringType();

    // if bidiStringType is not set by user, use ccsid to get value
    if (bidiStringType == -1) bidiStringType = ccsidConverter.bidiStringType_;

    BidiConversionProperties bidiConversionProperties =
        new BidiConversionProperties(bidiStringType); // @KBA
    bidiConversionProperties.setBidiImplicitReordering(
        settings_.getBidiImplicitReordering()); // @KBA
    bidiConversionProperties.setBidiNumericOrderingRoundTrip(
        settings_.getBidiNumericOrdering()); // @KBA

    value_ =
        ccsidConverter.byteArrayToString(
            rawBytes,
            offset,
            maxLength_,
            bidiConversionProperties); // @KBC changed to use bidiConversionProperties instead of
                                       // bidiStringType
  }
Exemplo n.º 4
0
  public void convertToRawBytes(byte[] rawBytes, int offset, ConvTable ccsidConverter)
      throws SQLException {
    try {
      int bidiStringType = settings_.getBidiStringType();
      // if bidiStringType is not set by user, use ccsid to get value
      if (bidiStringType == -1) bidiStringType = ccsidConverter.bidiStringType_;

      BidiConversionProperties bidiConversionProperties =
          new BidiConversionProperties(bidiStringType);
      bidiConversionProperties.setBidiImplicitReordering(settings_.getBidiImplicitReordering());
      bidiConversionProperties.setBidiNumericOrderingRoundTrip(settings_.getBidiNumericOrdering());

      // The length in the first 2 bytes is actually the length in characters.
      byte[] temp = ccsidConverter.stringToByteArray(value_, bidiConversionProperties);
      BinaryConverter.unsignedShortToByteArray(temp.length, rawBytes, offset);
      if (temp.length > maxLength_) {
        maxLength_ = temp.length;
        JDError.throwSQLException(this, JDError.EXC_INTERNAL);
      }
      System.arraycopy(temp, 0, rawBytes, offset + 2, temp.length);

      // The buffer we are filling with data is big enough to hold the entire field.
      // For varchar fields the actual data is often smaller than the field width.
      // That means whatever is in the buffer from the previous send is sent to the
      // system.  The data stream includes actual data length so the old bytes are not
      // written to the database, but the junk left over may decrease the affectiveness
      // of compression.  The following code will write hex 0s to the buffer when
      // actual length is less that field length.  Note the 0s are written only if
      // the field length is pretty big.  The data stream code (DBBaseRequestDS)
      // does not compress anything smaller than 1K.
      if ((maxLength_ > 256) && (maxLength_ - temp.length > 16)) {
        int stopHere = offset + 2 + maxLength_;
        for (int i = offset + 2 + temp.length; i < stopHere; i++) rawBytes[i] = 0x00;
      }
    } catch (Exception e) {
      JDError.throwSQLException(this, JDError.EXC_INTERNAL, e);
    }
  }
Exemplo n.º 5
0
  private void writeToServer() throws SQLException {
    try {
      Object object = savedObject_;
      if (object instanceof String) {
        String string = (String) object;
        int bidiStringType = settings_.getBidiStringType();
        if (bidiStringType == -1) bidiStringType = converter_.bidiStringType_;

        BidiConversionProperties bidiConversionProperties =
            new BidiConversionProperties(bidiStringType); // @KBA
        bidiConversionProperties.setBidiImplicitReordering(
            settings_.getBidiImplicitReordering()); // @KBA
        bidiConversionProperties.setBidiNumericOrderingRoundTrip(
            settings_.getBidiNumericOrdering()); // @KBA

        byte[] bytes =
            converter_.stringToByteArray(
                string,
                bidiConversionProperties); // @KBC changed to use bidiConversionProperties instead
                                           // of bidiStringType
        locator_.writeData(0L, bytes, true); // @K1C
      } else if (object instanceof Reader) {
        int length = scale_; // hack to get the length into the set method
        // Need to write even if there are 0 bytes in case we are batching and
        // the host server reuses the same handle for the previous locator; otherwise,
        // we'll have data in the current row from the previous row.
        if (length == 0) {
          locator_.writeData(0, new byte[0], 0, 0, true); // @K1C
        } else if (length > 0) {
          try {
            int blockSize =
                length < AS400JDBCPreparedStatement.LOB_BLOCK_SIZE
                    ? length
                    : AS400JDBCPreparedStatement.LOB_BLOCK_SIZE;
            int bidiStringType = settings_.getBidiStringType();
            if (bidiStringType == -1) bidiStringType = converter_.bidiStringType_;

            BidiConversionProperties bidiConversionProperties =
                new BidiConversionProperties(bidiStringType); // @KBA
            bidiConversionProperties.setBidiImplicitReordering(
                settings_.getBidiImplicitReordering()); // @KBA
            bidiConversionProperties.setBidiNumericOrderingRoundTrip(
                settings_.getBidiNumericOrdering()); // @KBA

            ReaderInputStream stream =
                new ReaderInputStream(
                    (Reader) savedObject_,
                    converter_.getCcsid(),
                    bidiConversionProperties,
                    blockSize); // @KBC changed to use bidiConversionProperties instead of
                                // bidiStringType
            byte[] byteBuffer = new byte[blockSize];
            int totalBytesRead = 0;
            int bytesRead = stream.read(byteBuffer, 0, blockSize);
            while (bytesRead > -1 && totalBytesRead < length) {
              locator_.writeData(
                  (long) totalBytesRead,
                  byteBuffer,
                  0,
                  bytesRead,
                  true); // totalBytesRead is our offset.  @K1C
              totalBytesRead += bytesRead;
              int bytesRemaining = length - totalBytesRead;
              if (bytesRemaining < blockSize) {
                blockSize = bytesRemaining;
                if (stream.available() == 0 && blockSize != 0) {
                  stream.close(); // @scan1
                  stream =
                      new ReaderInputStream(
                          (Reader) savedObject_,
                          converter_.getCcsid(),
                          bidiConversionProperties,
                          blockSize); // do this so we don't read more chars out of the Reader than
                                      // we have to. //@KBC changed to use bidiConversionProperties
                                      // instead of bidiStringType
                }
              }
              bytesRead = stream.read(byteBuffer, 0, blockSize);
            }
            stream.close(); // @scan1

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

          } catch (IOException ie) {
            JDError.throwSQLException(this, JDError.EXC_INTERNAL, ie);
          }
        } else if (length == -2) // @readerlen new else-if block (read all data)
        {
          try {
            // String readerStr = JDUtilities.readerToString((Reader)savedObject_);
            int blockSize = AS400JDBCPreparedStatement.LOB_BLOCK_SIZE;
            int bidiStringType = settings_.getBidiStringType();
            if (bidiStringType == -1) bidiStringType = converter_.bidiStringType_;

            BidiConversionProperties bidiConversionProperties =
                new BidiConversionProperties(bidiStringType); // @KBA
            bidiConversionProperties.setBidiImplicitReordering(
                settings_.getBidiImplicitReordering()); // @KBA
            bidiConversionProperties.setBidiNumericOrderingRoundTrip(
                settings_.getBidiNumericOrdering()); // @KBA

            ReaderInputStream stream =
                new ReaderInputStream(
                    (Reader) savedObject_,
                    converter_.getCcsid(),
                    bidiConversionProperties,
                    blockSize); // @KBC changed to use bidiConversionProperties instead of
                                // bidiStringType
            byte[] byteBuffer = new byte[blockSize];
            int totalBytesRead = 0;
            int bytesRead = stream.read(byteBuffer, 0, blockSize);
            while (bytesRead > -1) {
              locator_.writeData(
                  (long) totalBytesRead,
                  byteBuffer,
                  0,
                  bytesRead,
                  true); // totalBytesRead is our offset.  @K1C
              totalBytesRead += bytesRead;
              bytesRead = stream.read(byteBuffer, 0, blockSize);
            }
            stream.close(); // @scan1

          } catch (IOException ie) {
            JDError.throwSQLException(this, JDError.EXC_INTERNAL, ie);
          }
        } else {
          JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH);
        }
      } else if (object instanceof InputStream) {
        int length = scale_; // hack to get the length into the set method
        // Need to write even if there are 0 bytes in case we are batching and
        // the host server reuses the same handle for the previous locator; otherwise,
        // we'll have data in the current row from the previous row.
        if (length == 0) {
          locator_.writeData(0, new byte[0], 0, 0, true); // @K1C
        } else if (length > 0) {
          InputStream stream = (InputStream) savedObject_;
          int blockSize =
              length < AS400JDBCPreparedStatement.LOB_BLOCK_SIZE
                  ? length
                  : AS400JDBCPreparedStatement.LOB_BLOCK_SIZE;
          byte[] byteBuffer = new byte[blockSize];
          try {
            int totalBytesRead = 0;
            int bytesRead = stream.read(byteBuffer, 0, blockSize);
            while (bytesRead > -1 && totalBytesRead < length) {
              locator_.writeData(
                  (long) totalBytesRead,
                  byteBuffer,
                  0,
                  bytesRead,
                  true); // totalBytesRead is our offset.  @K1C
              totalBytesRead += bytesRead;
              int bytesRemaining = length - totalBytesRead;
              if (bytesRemaining < blockSize) {
                blockSize = bytesRemaining;
              }
              bytesRead = stream.read(byteBuffer, 0, blockSize);
            }

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

          } catch (IOException ie) {
            JDError.throwSQLException(this, JDError.EXC_INTERNAL, ie);
          }
        } else if (length == -2) // @readerlen new else-if block (read all data)
        {
          InputStream stream = (InputStream) savedObject_;
          int blockSize = AS400JDBCPreparedStatement.LOB_BLOCK_SIZE;
          byte[] byteBuffer = new byte[blockSize];
          try {
            int totalBytesRead = 0;
            int bytesRead = stream.read(byteBuffer, 0, blockSize);
            while (bytesRead > -1) {
              locator_.writeData(
                  (long) totalBytesRead,
                  byteBuffer,
                  0,
                  bytesRead,
                  true); // totalBytesRead is our offset.  @K1C
              totalBytesRead += bytesRead;

              bytesRead = stream.read(byteBuffer, 0, blockSize);
            }
          } catch (IOException ie) {
            JDError.throwSQLException(this, JDError.EXC_INTERNAL, ie);
          }
        } else {
          JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH);
        }
      } else if (JDUtilities.JDBCLevel_ >= 20
          && object instanceof Clob) // @H0A check for jdbc level to know if lobs exist
      {
        // @G5A Start new code for updateable locator case
        boolean set = false;
        if (object instanceof AS400JDBCClobLocator) {
          AS400JDBCClobLocator clob = (AS400JDBCClobLocator) object;

          // Synchronize on a lock so that the user can't keep making updates
          // to the clob while we are taking updates off the vectors.
          synchronized (clob) {
            // See if we saved off our real object from earlier.
            if (clob.savedObject_ != null) {
              savedObject_ = clob.savedObject_;
              scale_ = clob.savedScale_;
              clob.savedObject_ = null;
              writeToServer();
              return;
            }
          }
        }

        // @G5A If the code for updateable lob locators did not run, then run old code.
        if (!set) {
          Clob clob = (Clob) object;
          int length = (int) clob.length();
          String substring = clob.getSubString(1, length);
          locator_.writeData(0L, converter_.stringToByteArray(substring), 0, length, true); // @K1C
          set = true;
        } else {
          JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH);
        }
      }
      /*ifdef JDBC40
      else if( object instanceof SQLXML ) //@PDA jdbc40
      {
          SQLXML xml = (SQLXML)object;

          String stringVal = xml.getString();
          locator_.writeData(0L, converter_.stringToByteArray(stringVal), 0, stringVal.length(), true);
      }
      endif */
      else {
        JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH);
      }
    } finally {
      savedObject_ = null;
    }
    scale_ = (int) locator_.getLength();
  }