/// <summary>Initialize bit reader.</summary> /// <remarks> /// Initialize bit reader. /// <para> Initialisation turns bit reader to a ready state. Also a number of bytes is prefetched to /// accumulator. Because of that this method may block until enough data could be read from input.</para> /// </remarks> /// <param name="br">BitReader POJO</param> /// <param name="input">data source</param> internal static void Init(BitReader br, Stream input) { if (br._input != null) { throw new InvalidOperationException("Bit reader already has associated input stream"); } IntReader.Init(br._intReader, br._byteBuffer, br._intBuffer); br._input = input; br._accumulator = 0; br._bitOffset = 64; br._intOffset = Capacity; br._endOfStreamReached = false; Prepare(br); }
/* Number of bytes in unfinished "int" item. */ /// <summary>Fills up the input buffer.</summary> /// <remarks> /// Fills up the input buffer. /// <para> No-op if there are at least 36 bytes present after current position.</para> /// <para> After encountering the end of the input stream, 64 additional zero bytes are copied to the buffer.</para> /// </remarks> internal static void ReadMoreInput(BitReader br) { // TODO: Split to check and read; move read outside of decoding loop. if (br._intOffset <= Capacity - 9) { return; } if (br._endOfStreamReached) { if (IntAvailable(br) >= -2) { return; } throw new BrotliRuntimeException("No more input"); } int readOffset = br._intOffset << 2; int bytesRead = ByteReadSize - readOffset; Array.Copy(br._byteBuffer, readOffset, br._byteBuffer, 0, bytesRead); br._intOffset = 0; try { while (bytesRead < ByteReadSize) { int len = br._input.Read(br._byteBuffer, bytesRead, ByteReadSize - bytesRead); // EOF is -1 in Java, but 0 in C#. if (len <= 0) { br._endOfStreamReached = true; br._tailBytes = bytesRead; bytesRead += 3; break; } bytesRead += len; } } catch (IOException e) { throw new BrotliRuntimeException("Failed to read input", e); } IntReader.Convert(br._intReader, bytesRead >> 2); }