1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// InBuffer.cpp
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "StdAfx.h"
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../../C/Alloc.h"
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "InBuffer.h"
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
9baa3858d3f5d128a5c8466b700098109edcad5f2repo syncCInBuffer::CInBuffer():
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _buffer(0),
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _bufferLimit(0),
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _bufferBase(0),
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _stream(0),
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _bufferSize(0)
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{}
16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
17baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CInBuffer::Create(UInt32 bufferSize)
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  const UInt32 kMinBlockSize = 1;
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (bufferSize < kMinBlockSize)
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    bufferSize = kMinBlockSize;
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (_bufferBase != 0 && _bufferSize == bufferSize)
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return true;
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Free();
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _bufferSize = bufferSize;
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _bufferBase = (Byte *)::MidAlloc(bufferSize);
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return (_bufferBase != 0);
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
30baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CInBuffer::Free()
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ::MidFree(_bufferBase);
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _bufferBase = 0;
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
36baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CInBuffer::SetStream(ISequentialInStream *stream)
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _stream = stream;
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
41baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CInBuffer::Init()
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _processedSize = 0;
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _buffer = _bufferBase;
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _bufferLimit = _buffer;
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _wasFinished = false;
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef _NO_EXCEPTIONS
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ErrorCode = S_OK;
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
52baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CInBuffer::ReadBlock()
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef _NO_EXCEPTIONS
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (ErrorCode != S_OK)
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return false;
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (_wasFinished)
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return false;
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _processedSize += (_buffer - _bufferBase);
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 numProcessedBytes;
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  HRESULT result = _stream->Read(_bufferBase, _bufferSize, &numProcessedBytes);
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef _NO_EXCEPTIONS
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ErrorCode = result;
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #else
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (result != S_OK)
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    throw CInBufferException(result);
68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _buffer = _bufferBase;
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _bufferLimit = _buffer + numProcessedBytes;
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _wasFinished = (numProcessedBytes == 0);
72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return (!_wasFinished);
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
75baa3858d3f5d128a5c8466b700098109edcad5f2repo syncByte CInBuffer::ReadBlock2()
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (!ReadBlock())
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _processedSize++;
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return 0xFF;
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return *_buffer++;
83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
84