private InputStream spoolInputStream ( ObjectReader in , long contentLen ) throws IOException { byte [ ] buffer = new byte [ 0 ] ; byte [ ] tmpBuff ; long readLen = 0 ; File sf = null ; OutputStream sfout = null ; try { while ( true ) { int needToRead = contentLen - readLen > 2048 ? 2048 : ( int ) ( contentLen - readLen ) ; tmpBuff = new byte [ needToRead ] ; if ( needToRead == 0 ) { break ; } in . readFully ( tmpBuff ) ; if ( sfout != null ) { sfout . write ( tmpBuff ) ; } else if ( readLen + needToRead > maxBufferSize && fileCleaner != null ) { sf = PrivilegedFileHelper . createTempFile ( "jcrvd" , null , tempDir ) ; sfout = PrivilegedFileHelper . fileOutputStream ( sf ) ; sfout . write ( buffer ) ; sfout . write ( tmpBuff ) ; buffer = null ; } else { // reallocate new buffer and spool old buffer contents byte [ ] newBuffer = new byte [ ( int ) ( readLen + needToRead ) ] ; System . arraycopy ( buffer , 0 , newBuffer , 0 , ( int ) readLen ) ; System . arraycopy ( tmpBuff , 0 , newBuffer , ( int ) readLen , needToRead ) ; buffer = newBuffer ; } readLen += needToRead ; } if ( buffer != null ) { return new ByteArrayInputStream ( buffer ) ; } else { return PrivilegedFileHelper . fileInputStream ( sf ) ; } } finally { if ( sfout != null ) { sfout . close ( ) ; } if ( sf != null ) { spoolFileList . add ( sf ) ; } } }
Spool input stream .