Reads the header data, derives keys if necessary and creates the input
streams.

@throws IOException
if an error occurs
@throws EOFException
if we run out of data before reading the header
496990
private void initializeStream() throws IOException {
    int headerDataSize;
    if (isPasswordEncrypted) {
      headerDataSize = AES256v3Ciphertext.HEADER_SIZE
          + AES256v3Ciphertext.ENCRYPTION_SALT_LENGTH
          + AES256v3Ciphertext.HMAC_SALT_LENGTH
          + AES256v3Ciphertext.AES_BLOCK_SIZE;
    } else {
      headerDataSize = AES256v3Ciphertext.HEADER_SIZE
          + AES256v3Ciphertext.AES_BLOCK_SIZE;
    }

    byte[] headerData = new byte[headerDataSize];
    StreamUtils.readAllBytesOrFail(in, headerData); // throws EOF if insufficient data

    int offset = 0;
    byte version = headerData[offset++];

    if (version != AES256v3Ciphertext.EXPECTED_VERSION) {
      throw new IOException(String.format("Expected version %d but found %d.",
          AES256v3Ciphertext.EXPECTED_VERSION, version));
    }

    byte options = headerData[offset++];

    if (isPasswordEncrypted) {
      if (options != AES256v3Ciphertext.FLAG_PASSWORD) {
        throw new IOException("Expected password flag missing.");
      }

      byte[] decryptionSalt = new byte[AES256v3Ciphertext.ENCRYPTION_SALT_LENGTH];
      System.arraycopy(headerData, offset, decryptionSalt, 0,
          decryptionSalt.length);
      offset += decryptionSalt.length;

      byte[] hmacSalt = new byte[AES256v3Ciphertext.HMAC_SALT_LENGTH];
      System.arraycopy(headerData, offset, hmacSalt, 0, hmacSalt.length);
      offset += hmacSalt.length;

      // Derive keys
      JNCryptor cryptor = new AES256JNCryptor();
      try {
        decryptionKey = cryptor.keyForPassword(password, decryptionSalt);
        hmacKey = cryptor.keyForPassword(password, hmacSalt);
      } catch (CryptorException e) {
        throw new IOException("Failed to derive keys from password.", e);
      }

    } else {
      if (options != 0) {
        throw new IOException("Expected options byte to be zero.");
      }
    }

    byte[] iv = new byte[AES256v3Ciphertext.AES_BLOCK_SIZE];
    System.arraycopy(headerData, offset, iv, 0, iv.length);

    trailerIn = new TrailerInputStream(in, AES256v3Ciphertext.HMAC_SIZE);

    try {
      Cipher decryptCipher = Cipher
          .getInstance(AES256JNCryptor.AES_CIPHER_ALGORITHM);
      decryptCipher.init(Cipher.DECRYPT_MODE, decryptionKey,
          new IvParameterSpec(iv));

      mac = Mac.getInstance(AES256JNCryptor.HMAC_ALGORITHM);
      mac.init(hmacKey);

      // MAC the header
      mac.update(headerData);

      // The decryption stream will write the non-decrypted bytes to the mac
      // stream
      pushbackInputStream = new PushbackInputStream(new CipherInputStream(
          new MacUpdateInputStream(trailerIn, mac), decryptCipher), 1);


    } catch (GeneralSecurityException e) {
      throw new IOException("Failed to initiate cipher.", e);
    }
  }
1244363
private void pumpInputData() throws IOException {
    for (Input input : inputs) {
      if (input.pump == null) {
        continue;
      }
      OutputStream os;
      if (input.pipeName.equals(STDIN_FILE_NAME)) {
        os = process.getOutputStream();
      } else {
        throw new IOException("Only stdin piping is supported so far.");
      }
      input.pump.pump(os);
    }
  }
1155460
public void init(ReadStreamOld is, String headerBoundary)
    throws IOException
  {
    _is = is;
    
    _boundary.clear();
    _boundary.add("--");
    _boundary.add(headerBoundary);

    _boundaryBuffer = _boundary.getBuffer();
    _boundaryLength = _boundary.getLength();

    _peekBuffer.setLength(_boundaryLength + 5);
    _peek = _peekBuffer.getBuffer();
    _peekOffset = 0;
    _peekLength = 0;
    _peek[_peekLength++] = (byte) '\n';

    _isPartDone = false;
    _isDone = false;
    _isComplete = false;
    
    while (read(_dummyBuffer, 0, _dummyBuffer.length) >= 0) {
    }
    
    _isPartDone = true;
  }
1999
public static void readAndWriteStreams(InputStream inputStream,OutputStream outputStream) throws IOException
    {
        byte[] buffer=new byte[5000];
        int read=-1;
        try
        {
            do
            {
                //read next buffer
                read=inputStream.read(buffer);
                
                if(read!=-1)
                {
                    //write to in memory stream
                    outputStream.write(buffer,0,read);
                }
            }while(read!=-1);
        }
        finally
        {
            //close streams
            IOHelper.closeResource(inputStream);
            IOHelper.closeResource(outputStream);
        }
    }
927944
public static void read(InputStream stream, byte[] buffer) throws IOException {
        int read = 0;
        while(read < buffer.length) {
            int newlyRead = stream.read(buffer, read, buffer.length - read);
            if(newlyRead == -1)
                throw new EOFException("Attempt to read " + buffer.length
                                       + " bytes failed due to EOF.");
            read += newlyRead;
        }
    }
227358
public void read(InputStream is) throws IOException
   {
      byte[] headerBlock = new byte[20];
      is.read(headerBlock);

      int headerLength = PEPUtility.getShort(headerBlock, 8);
      int recordCount = PEPUtility.getInt(headerBlock, 10);
      int recordLength = PEPUtility.getInt(headerBlock, 16);
      StreamHelper.skip(is, headerLength - headerBlock.length);

      byte[] record = new byte[recordLength];
      for (int recordIndex = 1; recordIndex <= recordCount; recordIndex++)
      {
         is.read(record);
         readRow(recordIndex, record);
      }
   }
109788
static void readAllBytesOrFail(InputStream in, byte[] buffer)
      throws IOException {
    int read = readAllBytes(in, buffer);
    if (read != buffer.length) {
      throw new EOFException(String.format(
          "Expected %d bytes but read %d bytes.", buffer.length, read));
    }
  }
1050798
private boolean before() throws IOException {
        if (closed) {
            throw new IOException("Attempted read from closed stream.");
        }
        switch (length) {
            case EOF:
                return false;
            case UNKNOWN:
                length = readLength();
                pos = 0;
                if (length == EOF) {
                    HeaderList.parse(src); // result is ignored
                    return false;
                }
                return true;
            default:
                return true;
        }
    }
970371
private void processInput(boolean endOfInput) throws IOException {
        // Prepare decoderIn for reading
        decoderIn.flip();
        CoderResult coderResult;
        while (true) {
            coderResult = decoder.decode(decoderIn, decoderOut, endOfInput);
            if (coderResult.isOverflow()) {
                flushOutput();
            } else if (coderResult.isUnderflow()) {
                break;
            } else {
                // The decoder is configured to replace malformed input and unmappable characters,
                // so we should not get here.
                throw new IOException("Unexpected coder result");
            }
        }
        // Discard the bytes that have been read
        decoderIn.compact();
    }
626945
public void setHeader (File header)
    {
        try {
            _header = StreamUtil.toString(new FileReader(header));
        } catch (IOException ioe) {
            System.err.println("Unabled to load header '" + header + ": " +
                               ioe.getMessage());
        }
    }
1079623
private void readRoot() throws IOException {
        if (root != null) {
            return;
        }
        final ByteArrayOutputStream content = new ByteArrayOutputStream();
        final byte[] buffer = new byte[READ_BUFFER_SIZE];
        try {
            while (trans_.read(buffer, 0, READ_BUFFER_SIZE) > 0) {
                content.write(buffer);
            }
        } catch (TTransportException e) {
            if (TTransportException.END_OF_FILE != e.getType()) {
                throw new IOException(e);
            }
        }
        root = OBJECT_MAPPER.readTree(content.toByteArray());
    }
1322090
private void readHeader() throws IOException {
    headerBuffer.rewind();

    final int flgRead = in.read();
    if (flgRead < 0) {
      throw new IOException(PREMATURE_EOS);
    }
    final int bdRead = in.read();
    if (bdRead < 0) {
      throw new IOException(PREMATURE_EOS);
    }

    final byte flgByte = (byte)(flgRead & 0xFF);
    final LZ4FrameOutputStream.FLG flg = LZ4FrameOutputStream.FLG.fromByte(flgByte);
    headerBuffer.put(flgByte);
    final byte bdByte = (byte)(bdRead & 0xFF);
    final LZ4FrameOutputStream.BD bd = LZ4FrameOutputStream.BD.fromByte(bdByte);
    headerBuffer.put(bdByte);

    this.frameInfo = new LZ4FrameOutputStream.FrameInfo(flg, bd);

    if (flg.isEnabled(LZ4FrameOutputStream.FLG.Bits.CONTENT_SIZE)) {
      expectedContentSize = readLong(in);
      headerBuffer.putLong(expectedContentSize);
    }
    totalContentSize = 0L;

    // check stream descriptor hash
    final byte hash = (byte) ((checksum.hash(headerArray, 0, headerBuffer.position(), 0) >> 8) & 0xFF);
    final int expectedHash = in.read();
    if (expectedHash < 0) {
      throw new IOException(PREMATURE_EOS);
    }

    if (hash != (byte)(expectedHash & 0xFF)) {
      throw new IOException(DESCRIPTOR_HASH_MISMATCH);
    }

    maxBlockSize = frameInfo.getBD().getBlockMaximumSize();
    compressedBuffer = new byte[maxBlockSize]; // Reused during different compressions
    rawBuffer = new byte[maxBlockSize];
    buffer = ByteBuffer.wrap(rawBuffer);
    buffer.limit(0);
  }
255081
@Override
  public void readFully(byte[] b) throws IOException {
    if (inner.read(b) < b.length) {
      throw new EOFException();
    }
  }
10264
public void readHeaderTo(OutputStream os) throws IOException {
        position = 0;
        byte[] buf = new byte[(int)responseBodyStart];
        int c = read(buf,0,buf.length);
        if(c != -1) {
            os.write(buf,0,c);
        }
    }
386877
@Override
    public void reset() throws IOException {
        abortIfNeeded();
        // Clear up any encoded data
        currentChunkIterator = null;
        priorChunkSignature = headerSignature;
        // Reset the wrapped stream if it is mark-supported,
        // otherwise use our buffered data.
        if (is.markSupported()) {
            if (log.isDebugEnabled())
                log.debug("AwsChunkedEncodingInputStream reset "
                        + "(will reset the wrapped stream because it is mark-supported).");
            is.reset();
        }
        else {
            if (log.isDebugEnabled())
                log.debug("AwsChunkedEncodingInputStream reset "
                        + "(will use the buffer of the decoded stream).");
            if (null == decodedStreamBuffer) {
                throw new IOException("Cannot reset the stream because the mark is not set.");
            }
            decodedStreamBuffer.startReadBuffer();
        }

        currentChunkIterator = null;
        isAtStart = true;
        isTerminating = false;
    }
422585
public void readData(DataInput input) throws IOException {
        readReference = input.readUnsignedShort();
        readCount = input.readUnsignedShort();
        writeReference = input.readUnsignedShort();
        writeCount = input.readUnsignedShort();
        int byteCount = input.readUnsignedByte();

        if (nonWordDataHandler == null) {
            byte buffer[] = new byte[byteCount];
            input.readFully(buffer, 0, byteCount);

            int offset = 0;
            registers = new Register[writeCount];

            for (int register = 0; register < writeCount; register++) {
                registers[register] = new SimpleRegister(buffer[offset], buffer[offset + 1]);
                offset += 2;
            }
        }
        else {
            nonWordDataHandler.readData(input, writeReference, writeCount);
        }
    }
195738
private void initializeContentBuffer() throws IOException {
		if (this.seekEnabled) {
			this.buffer = ByteBuffer.allocate(this.fileSize - HEADER_BYTES);
			final int read = this.stream.read(this.buffer);
			if (read < 0) {
				throw new EOFException();
			}
			this.buffer.rewind();
			this.buffer.limit(read);
			this.bufferPosition = HEADER_BYTES;
		} else {
			this.buffer = ByteBuffer.allocate(BLOCK_SIZE);
			final int read = this.stream.read(this.buffer);
			if (read < 0) {
				throw new EOFException();
			}
			this.buffer.rewind();
			this.buffer.limit(read);
			this.bufferPosition = HEADER_BYTES;
		}
	}