Belle II Software development
BufferedReader Class Reference
Inheritance diagram for BufferedReader:
Reader

Public Member Functions

 BufferedReader (size_t size, unsigned char *memory=NULL)
 
 BufferedReader (const BufferedReader &r)
 
void seekTo (size_t pos)
 
unsigned char * ptr ()
 
size_t count () const
 
size_t size () const
 
void copy (const void *, size_t)
 
size_t read (void *, size_t) override
 
bool available () override
 
const BufferedReaderoperator= (const BufferedReader &)
 
virtual bool readBool ()
 
virtual char readChar ()
 
virtual short readShort ()
 
virtual int readInt ()
 
virtual long long readLong ()
 
virtual unsigned char readUChar ()
 
virtual unsigned short readUShort ()
 
virtual unsigned int readUInt ()
 
virtual unsigned long long readULong ()
 
virtual float readFloat ()
 
virtual double readDouble ()
 
virtual const std::string readString ()
 
virtual void readObject (Serializable &)
 

Protected Member Functions

void reverse (void *buf, size_t len)
 

Protected Attributes

unsigned char * m_memory
 
size_t m_size
 
size_t m_pos
 
bool m_allocated
 

Detailed Description

Definition at line 19 of file BufferedReader.h.

Constructor & Destructor Documentation

◆ BufferedReader() [1/3]

Definition at line 16 of file BufferedReader.cc.

17 : m_memory(NULL), m_size(0), m_pos(0), m_allocated(false) {}

◆ BufferedReader() [2/3]

BufferedReader ( size_t  size,
unsigned char *  memory = NULL 
)

Definition at line 33 of file BufferedReader.cc.

34 : m_memory(memory), m_size(size), m_pos(0), m_allocated(false)
35{
36 if (memory == NULL) {
37 m_memory = new unsigned char[size];
38 m_allocated = true;
39 }
40}

◆ BufferedReader() [3/3]

Definition at line 19 of file BufferedReader.cc.

20 : m_memory(NULL), m_size(reader.m_size),
21 m_pos(reader.m_pos), m_allocated(reader.m_allocated)
22{
23 if (m_allocated) {
24 m_memory = new unsigned char [reader.m_size];
25 ::memcpy(m_memory, reader.m_memory, reader.m_size);
26 } else {
27 m_memory = reader.m_memory;
28 }
29 m_size = reader.m_size;
30 m_pos = reader.m_pos;
31}

◆ ~BufferedReader()

~BufferedReader ( )
virtual

Definition at line 42 of file BufferedReader.cc.

43{
44 if (m_allocated && m_memory != NULL) {
45 delete [] m_memory;
46 }
47}

Member Function Documentation

◆ available()

bool available ( )
inlineoverridevirtual

Implements Reader.

Definition at line 36 of file BufferedReader.h.

36{ return true; }

◆ copy()

void copy ( const void *  buffer,
size_t  count 
)

Definition at line 66 of file BufferedReader.cc.

67{
68 if (count < m_size) {
69 memcpy(m_memory, buffer, count);
70 m_pos = 0;
71 } else {
72 throw (IOException("Out of buffer range: %d > %d",
73 (int)count, (int)m_size));
74 }
75}

◆ count()

size_t count ( ) const
inline

Definition at line 32 of file BufferedReader.h.

32{ return m_pos; }

◆ operator=()

const BufferedReader & operator= ( const BufferedReader reader)

Definition at line 49 of file BufferedReader.cc.

50{
51 if (m_allocated) {
52 delete [] m_memory;
53 }
54 m_allocated = reader.m_allocated;
55 if (m_allocated) {
56 m_memory = new unsigned char [reader.m_size];
57 ::memcpy(m_memory, reader.m_memory, reader.m_size);
58 } else {
59 m_memory = reader.m_memory;
60 }
61 m_size = reader.m_size;
62 m_pos = reader.m_pos;
63 return *this;
64}

◆ ptr()

unsigned char * ptr ( )
inline

Definition at line 31 of file BufferedReader.h.

31{ return m_memory; }

◆ read()

size_t read ( void *  buf,
size_t  count 
)
overridevirtual

Implements Reader.

Definition at line 77 of file BufferedReader.cc.

78{
79 if (m_pos + count > size()) {
80 throw (IOException("Out of buffer range: %d + %d > %d",
81 (int)m_pos, (int)count, (int)m_size));
82 }
83 memcpy(buf, (m_memory + m_pos), count);
84 m_pos += count;
85 return count;
86}

◆ readBool()

bool readBool ( )
virtualinherited

Definition at line 27 of file Reader.cc.

28{
29 bool v;
30 read(&v, sizeof(bool));
31 return v;
32}

◆ readChar()

char readChar ( )
virtualinherited

Definition at line 34 of file Reader.cc.

35{
36 char v;
37 read(&v, sizeof(char));
38 return v;
39}

◆ readDouble()

double readDouble ( )
virtualinherited

Definition at line 118 of file Reader.cc.

119{
120 double v;
121 read(&v, sizeof(double));
122#if defined(__ENDIAN_L__)
123 reverse(&v, sizeof(double));
124#endif
125 return v;
126}

◆ readFloat()

float readFloat ( )
virtualinherited

Definition at line 108 of file Reader.cc.

109{
110 float v;
111 read(&v, sizeof(float));
112#if defined(__ENDIAN_L__)
113 reverse(&v, sizeof(float));
114#endif
115 return v;
116}

◆ readInt()

int readInt ( )
virtualinherited

Definition at line 68 of file Reader.cc.

69{
70 int v;
71 read(&v, sizeof(int));
72#if defined(__ENDIAN_L__)
73 reverse(&v, sizeof(int));
74#endif
75 return v;
76}

◆ readLong()

long long readLong ( )
virtualinherited

Definition at line 88 of file Reader.cc.

89{
90 long long v;
91 read(&v, sizeof(long long));
92#if defined(__ENDIAN_L__)
93 reverse(&v, sizeof(long long));
94#endif
95 return v;
96}

◆ readObject()

void readObject ( Serializable v)
virtualinherited

Definition at line 142 of file Reader.cc.

143{
144 v.readObject(*this);
145}

◆ readShort()

short readShort ( )
virtualinherited

Definition at line 48 of file Reader.cc.

49{
50 short v;
51 read(&v, sizeof(short));
52#if defined(__ENDIAN_L__)
53 reverse(&v, sizeof(short));
54#endif
55 return v;
56}

◆ readString()

const std::string readString ( )
virtualinherited

Definition at line 128 of file Reader.cc.

129{
130 int count = readInt();
131 char str[1025];
132 std::string v;
133 while (count > 0) {
134 int c = (count > 1024) ? 1024 : count;
135 read(str, c);
136 v.append(str, 0, c);
137 count -= c;
138 }
139 return v;
140}

◆ readUChar()

unsigned char readUChar ( )
virtualinherited

Definition at line 41 of file Reader.cc.

42{
43 unsigned char v;
44 read(&v, sizeof(unsigned char));
45 return v;
46}

◆ readUInt()

unsigned int readUInt ( )
virtualinherited

Definition at line 78 of file Reader.cc.

79{
80 unsigned int v;
81 read(&v, sizeof(unsigned int));
82#if defined(__ENDIAN_L__)
83 reverse(&v, sizeof(unsigned int));
84#endif
85 return v;
86}

◆ readULong()

unsigned long long readULong ( )
virtualinherited

Definition at line 98 of file Reader.cc.

99{
100 unsigned long long v;
101 read(&v, sizeof(unsigned long long));
102#if defined(__ENDIAN_L__)
103 reverse(&v, sizeof(unsigned long long));
104#endif
105 return v;
106}

◆ readUShort()

unsigned short readUShort ( )
virtualinherited

Definition at line 58 of file Reader.cc.

59{
60 unsigned short v;
61 read(&v, sizeof(unsigned short));
62#if defined(__ENDIAN_L__)
63 reverse(&v, sizeof(unsigned short));
64#endif
65 return v;
66}

◆ reverse()

void reverse ( void *  buf,
size_t  len 
)
protectedinherited

Definition at line 18 of file Reader.cc.

19{
20 for (int c = 0 ; c < (int)len / 2 ; c ++) {
21 char tmp = *((char*)buf + c);
22 *((char*)buf + c) = *((char*)buf + len - 1 - c);
23 *((char*)buf + len - 1 - c) = tmp;
24 }
25}

◆ seekTo()

void seekTo ( size_t  pos)
inline

Definition at line 30 of file BufferedReader.h.

30{ m_pos = pos; }

◆ size()

size_t size ( ) const
inline

Definition at line 33 of file BufferedReader.h.

33{ return m_size; }

Member Data Documentation

◆ m_allocated

bool m_allocated
protected

Definition at line 46 of file BufferedReader.h.

◆ m_memory

unsigned char* m_memory
protected

Definition at line 43 of file BufferedReader.h.

◆ m_pos

size_t m_pos
protected

Definition at line 45 of file BufferedReader.h.

◆ m_size

size_t m_size
protected

Definition at line 44 of file BufferedReader.h.


The documentation for this class was generated from the following files: