1/*
2Copyright (C) 2002 Andrea Mazzoleni ( http://advancemame.sf.net )
3Copyright (C) 2001-4 Igor Pavlov ( http://www.7-zip.org )
4
5This library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Lesser General Public
7License version 2.1 as published by the Free Software Foundation.
8
9This library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Lesser General Public License for more details.
13
14You should have received a copy of the GNU Lesser General Public
15License along with this library; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19#ifndef __STREAM_INBYTE_H
20#define __STREAM_INBYTE_H
21
22#include "iiostrm.h"
23
24namespace NStream {
25
26class CInByte
27{
28 UINT64 m_ProcessedSize;
29 BYTE *m_BufferBase;
30 UINT32 m_BufferSize;
31 BYTE *m_Buffer;
32 BYTE *m_BufferLimit;
33 ISequentialInStream* m_Stream;
34 bool m_StreamWasExhausted;
35
36 bool ReadBlock();
37
38public:
39 CInByte(UINT32 aBufferSize = 0x100000);
40 ~CInByte();
41
42 void Init(ISequentialInStream *aStream);
43
44 bool ReadByte(BYTE &aByte)
45 {
46 if(m_Buffer >= m_BufferLimit)
47 if(!ReadBlock())
48 return false;
49 aByte = *m_Buffer++;
50 return true;
51 }
52 BYTE ReadByte()
53 {
54 if(m_Buffer >= m_BufferLimit)
55 if(!ReadBlock())
56 return 0x0;
57 return *m_Buffer++;
58 }
59 void ReadBytes(void *aData, UINT32 aSize, UINT32 &aProcessedSize)
60 {
61 for(aProcessedSize = 0; aProcessedSize < aSize; aProcessedSize++)
62 if (!ReadByte(((BYTE *)aData)[aProcessedSize]))
63 return;
64 }
65 bool ReadBytes(void *aData, UINT32 aSize)
66 {
67 UINT32 aProcessedSize;
68 ReadBytes(aData, aSize, aProcessedSize);
69 return (aProcessedSize == aSize);
70 }
71 UINT64 GetProcessedSize() const { return m_ProcessedSize + (m_Buffer - m_BufferBase); }
72};
73
74}
75
76#endif
77