copyIs2Os(InputStream inputStream, OutputStream outputStream) { if (bbuff == null) { lock (this) { { if (bbuff == null) { bbuff = new byte[8192]; } } } } try { lock (bbuff) { for (int len = inputStream.read(bbuff); len >= 0; len = inputStream.read(bbuff)) { outputStream.write(bbuff, 0, len); } } inputStream.close(); outputStream.flush(); } catch (Exception ex) { try { inputStream.close(); } catch (Exception /* ignore */) { } try { outputStream.flush(); } catch (Exception /* ignore */) { } throw SqlEx.get(ERR_GC4007_BLOB_IO, ex); } return; } // copyIs2Os
strm2array(InputStream inStream, int limit) { byte[] buff = new byte[8192]; byte[] ba = new byte[0]; int len; try { while ((limit < 0 || ba.Length < limit) && (len = inStream.read(buff, 0, buff.Length)) >= 0) { if (limit >= 0) { len = Math.Min(len, limit - ba.Length); } byte[] tmp = new byte[ba.Length + len]; if (ba.Length > 0) { Array.Copy(ba, 0, tmp, 0, ba.Length); } if (len > 0) { Array.Copy(buff, 0, tmp, ba.Length, len); } ba = tmp; } } catch (Exception) { throw SqlEx.get(ERR_GC4007_BLOB_IO); } finally { try { inStream.close(); } catch (Exception) {} } return(ba); } // strm2array
/* ** Name: setBinaryStream ** ** Description: ** Set parameter to a binary stream. ** ** Input: ** paramIndex Parameter index. ** stream Binary stream. ** length Length of stream in bytes. ** ** Output: ** None. ** ** Returns: ** void. ** ** History: ** 19-May-99 (gordy) ** Created. ** 2-Feb-00 (gordy) ** Send short streams as VARCHAR or VARBINARY. ** 13-Jun-00 (gordy) ** Map parameter index. ** 8-Jan-01 (gordy) ** Stream classes moved to ParamSet. ** 1-Jun-01 (gordy) ** No longer need wrapper class for binary InputStream. ** 1-Nov-03 (gordy) ** ParamSet functionality extended. ** 14-Sep-05 (gordy) ** If stream is short enough, save as VARBINARY. */ public void setBinaryStream( int paramIndex, InputStream stream, int length ) { if ( trace.enabled() ) trace.log( title + ".setBinaryStream( " + paramIndex + ", " + length + " )" ); /* ** Check length to see if can be sent as VARBINARY. ** Ingres won't coerce BLOB to VARBINARY, but will ** coerce VARBINARY to BLOB, so VARBINARY is preferred. */ if ( length >= 0 && length <= conn.max_vbyt_len ) { byte[] bytes = new byte[ length ]; if ( length > 0 ) try { int len = stream.read( bytes ); if ( len != length ) { /* ** Byte array limits read so any difference ** must be a truncation. */ if ( trace.enabled( 1 ) ) trace.write( tr_id + ".setBinaryStream: read only " + len + " of " + length + " bytes!" ); setWarning( DataTruncation( paramIndex, true, false, length, len ) ); } } catch( IOException ) { throw SqlEx.get( ERR_GC4007_BLOB_IO ); } paramIndex = paramMap( paramIndex ); paramSet.init(paramIndex, ProviderType.VarBinary); paramSet.setBytes( paramIndex, bytes ); } else { paramIndex = paramMap( paramIndex ); paramSet.init(paramIndex, ProviderType.LongVarBinary); paramSet.setBinaryStream( paramIndex, stream ); } return; }
/* ** Name: copyIs2Os ** ** Description: ** Writes the contents of an InputStream to an OutputStream. ** The InputStream is closed. The OutputStream is flushed ** but not closed. ** ** Input: ** is InputStream. ** os OutputStream. ** ** Output: ** None. ** ** Returns: ** void. ** ** History: ** 1-Dec-03 (gordy) ** Created. */ protected void copyIs2Os(InputStream inputStream, OutputStream outputStream) { if (bbuff == null) lock (this) { { if (bbuff == null) bbuff = new byte[8192]; } } try { lock (bbuff) { for (int len = inputStream.read(bbuff); len >= 0; len = inputStream.read(bbuff)) outputStream.write(bbuff, 0, len); } inputStream.close(); outputStream.flush(); } catch (Exception ex) { try { inputStream.close(); } catch (Exception /* ignore */) { } try { outputStream.flush(); } catch (Exception /* ignore */) { } throw SqlEx.get(ERR_GC4007_BLOB_IO, ex); } return; }
/* ** Name: strm2array ** ** Description: ** Read a binary input stream and convert to a byte array. ** The stream is closed. ** ** Input: ** in Binary input stream. ** limit Maximum length of result, negative for no limit. ** ** Output: ** None. ** ** Returns: ** byte[] The stream as a byte array. ** ** History: ** 12-Sep-03 (gordy) ** Created. */ private static byte[] strm2array( InputStream inStream, int limit ) { byte[] buff = new byte[ 8192 ]; byte[] ba = new byte[ 0 ]; int len; try { while( (limit < 0 || ba.Length < limit) && (len = inStream.read( buff, 0, buff.Length )) >= 0 ) { if ( limit >= 0 ) len = Math.Min( len, limit - ba.Length ); byte[] tmp = new byte[ ba.Length + len ]; if ( ba.Length > 0 ) Array.Copy( ba, 0, tmp, 0, ba.Length ); if ( len > 0 ) Array.Copy( buff, 0, tmp, ba.Length, len ); ba = tmp; } } catch( Exception ) { throw SqlEx.get( ERR_GC4007_BLOB_IO ); } finally { try { inStream.close(); } catch( Exception ) {} } return( ba ); }
/* ** Name: write (inputStream) ** ** Description: ** Append input stream to the output buffer. This ** routine does not split buffers, the input stream ** is read until the buffer is filled or the stream ** is exhausted. The number of bytes read from the ** input stream is returned. It is the callers ** responsibility to assure that there is space ** available in the output buffer. ** ** Input: ** inputStream InputStream ** ** Output: ** None. ** ** Returns: ** short Number of bytes written. ** ** History: ** 29-Sep-99 (gordy) ** Created. */ /// <summary> /// Append input stream to the output buffer. This /// routine does not split buffers, the input stream /// is read until the buffer is filled or the stream /// is exhausted. The number of bytes read from the /// input stream is returned. It is the callers /// responsibility to assure that there is space /// available in the output buffer. /// </summary> /// <param name="inputStream"></param> /// <returns></returns> public virtual short write(InputStream inputStream) { int length = avail(); try { length = inputStream.read(buffer, data_ptr, length); } catch (System.Exception ex) { throw SqlEx.get( ERR_GC4007_BLOB_IO, ex); } if (length < 0) length = 0; data_ptr += length; if (trace.enabled(5)) trace.write(title + ": appending " + length); return ((short) length); }