public byte[] getBytes() throws SQLException { truncated_ = 0; try { return BinaryConverter.stringToBytes(getString()); } catch (NumberFormatException nfe) { // this Clob contains non-hex characters JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH, nfe); return null; } }
public Blob getBlob() throws SQLException { truncated_ = 0; try { return new AS400JDBCBlob(BinaryConverter.stringToBytes(value_), maxLength_); } catch (NumberFormatException nfe) { // this field contains non-hex characters JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH, nfe); return null; } }
public Blob getBlob() throws SQLException { truncated_ = 0; try { byte[] bytes = BinaryConverter.stringToBytes(getString()); return new AS400JDBCBlob(bytes, bytes.length); } catch (NumberFormatException nfe) { // this Clob contains non-hex characters JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH, nfe); return null; } }
public byte[] getBytes() throws SQLException { truncated_ = 0; outOfBounds_ = false; try { return BinaryConverter.stringToBytes(value_); } catch (NumberFormatException nfe) { // this field contains non-hex characters JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH, nfe); return null; } }
public void set(Object object, Calendar calendar, int scale) throws SQLException { if (object instanceof String) { try { value_ = BinaryConverter.stringToBytes((String) object); } catch (NumberFormatException nfe) { // the String contains non-hex characters JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH, nfe); } } else if (object instanceof byte[]) value_ = (byte[]) object; else if (object instanceof InputStream) { // value_ = JDUtilities.streamToBytes((InputStream)object, scale); int length = scale; // hack to get the length into the set method if (length >= 0) { InputStream stream = (InputStream) object; ByteArrayOutputStream baos = new ByteArrayOutputStream(); 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) { baos.write(byteBuffer, 0, bytesRead); totalBytesRead += bytesRead; int bytesRemaining = length - totalBytesRead; if (bytesRemaining < blockSize) { blockSize = bytesRemaining; } bytesRead = stream.read(byteBuffer, 0, blockSize); } } catch (IOException ie) { JDError.throwSQLException(this, JDError.EXC_INTERNAL, ie); } value_ = baos.toByteArray(); if (value_.length < length) { // a length longer than the stream was specified JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH); } } else if (length == -2) // @readerlen new else-if block (read all data) { InputStream stream = (InputStream) object; ByteArrayOutputStream baos = new ByteArrayOutputStream(); 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) { baos.write(byteBuffer, 0, bytesRead); totalBytesRead += bytesRead; bytesRead = stream.read(byteBuffer, 0, blockSize); } } catch (IOException ie) { JDError.throwSQLException(this, JDError.EXC_INTERNAL, ie); } value_ = baos.toByteArray(); } else { JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH); } } else if (object instanceof Reader) { // value_ = BinaryConverter.stringToBytes(JDUtilities.readerToString((Reader)object, scale)); int length = scale; // hack to get the length into the set method 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); } value_ = baos.toByteArray(); stream.close(); // @scan1 if (value_.length < length) { // a length longer than the stream was specified JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH); } } catch (ExtendedIOException eie) { // the Reader contains non-hex characters JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH, eie); } catch (IOException ie) { JDError.throwSQLException(this, 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); } value_ = baos.toByteArray(); stream.close(); // @scan1 } catch (ExtendedIOException eie) { // the Reader contains non-hex characters JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH, eie); } 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 Blob) value_ = ((Blob) object).getBytes(1, (int) ((Blob) object).length()); else if (JDUtilities.JDBCLevel_ >= 20 && object instanceof Clob) { try { value_ = BinaryConverter.stringToBytes( ((Clob) object).getSubString(1, (int) ((Clob) object).length())); } catch (NumberFormatException nfe) { // the Clob contains non-hex characters JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH, nfe); } } else JDError.throwSQLException(this, JDError.EXC_DATA_TYPE_MISMATCH); // Truncate if necessary. int valueLength = value_.length; if (valueLength > maxLength_) { byte[] newValue = new byte[maxLength_]; System.arraycopy(value_, 0, newValue, 0, maxLength_); value_ = newValue; truncated_ = valueLength - maxLength_; outOfBounds_ = false; } else truncated_ = 0; outOfBounds_ = false; length_ = value_.length; }
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 }