Botan  1.11.10
Public Member Functions | List of all members
Botan::DataSource_Stream Class Reference

#include <data_src.h>

Inheritance diagram for Botan::DataSource_Stream:
Botan::DataSource

Public Member Functions

 DataSource_Stream (std::istream &, const std::string &id="<std::istream>")
 
 DataSource_Stream (const std::string &file, bool use_binary=false)
 
 DataSource_Stream (const DataSource_Stream &)=delete
 
size_t discard_next (size_t N)
 
bool end_of_data () const
 
virtual size_t get_bytes_read () const
 
std::string id () const
 
DataSource_Streamoperator= (const DataSource_Stream &)=delete
 
size_t peek (byte[], size_t, size_t) const
 
size_t peek_byte (byte &out) const
 
size_t read (byte[], size_t)
 
size_t read_byte (byte &out)
 
 ~DataSource_Stream ()
 

Detailed Description

This class represents a Stream-Based DataSource.

Definition at line 141 of file data_src.h.

Constructor & Destructor Documentation

Botan::DataSource_Stream::DataSource_Stream ( std::istream &  in,
const std::string &  id = "<std::istream>" 
)

Definition at line 174 of file data_src.cpp.

175  :
176  identifier(name),
177  source_p(nullptr),
178  source(in),
179  total_read(0)
180  {
181  }
Botan::DataSource_Stream::DataSource_Stream ( const std::string &  file,
bool  use_binary = false 
)

Construct a Stream-Based DataSource from file

Parameters
filethe name of the file
use_binarywhether to treat the file as binary or not

Definition at line 155 of file data_src.cpp.

156  :
157  identifier(path),
158  source_p(new std::ifstream(
159  path.c_str(),
160  use_binary ? std::ios::binary : std::ios::in)),
161  source(*source_p),
162  total_read(0)
163  {
164  if(!source.good())
165  {
166  delete source_p;
167  throw Stream_IO_Error("DataSource: Failure opening file " + path);
168  }
169  }
Botan::DataSource_Stream::DataSource_Stream ( const DataSource_Stream )
delete
Botan::DataSource_Stream::~DataSource_Stream ( )

Definition at line 186 of file data_src.cpp.

187  {
188  delete source_p;
189  }

Member Function Documentation

size_t Botan::DataSource::discard_next ( size_t  N)
inherited

Discard the next N bytes of the data

Parameters
Nthe number of bytes to discard
Returns
number of bytes actually discarded

Definition at line 35 of file data_src.cpp.

References Botan::DataSource::read_byte().

36  {
37  size_t discarded = 0;
38  byte dummy;
39  for(size_t j = 0; j != n; ++j)
40  discarded += read_byte(dummy);
41  return discarded;
42  }
uint8_t byte
Definition: types.h:30
size_t read_byte(byte &out)
Definition: data_src.cpp:19
bool Botan::DataSource_Stream::end_of_data ( ) const
virtual

Test whether the source still has data that can be read.

Returns
true if there is still data to read, false otherwise

Implements Botan::DataSource.

Definition at line 139 of file data_src.cpp.

Referenced by peek().

140  {
141  return (!source.good());
142  }
virtual size_t Botan::DataSource_Stream::get_bytes_read ( ) const
inlinevirtual
Returns
number of bytes read so far.

Implements Botan::DataSource.

Definition at line 165 of file data_src.h.

165 { return total_read; }
std::string Botan::DataSource_Stream::id ( ) const
virtual

return the id of this data source

Returns
std::string representing the id of this data source

Reimplemented from Botan::DataSource.

Definition at line 147 of file data_src.cpp.

148  {
149  return identifier;
150  }
DataSource_Stream& Botan::DataSource_Stream::operator= ( const DataSource_Stream )
delete
size_t Botan::DataSource_Stream::peek ( byte  out[],
size_t  length,
size_t  peek_offset 
) const
virtual

Read from the source but do not modify the internal offset. Consecutive calls to peek() will return portions of the source starting at the same position.

Parameters
outthe byte array to write the output to
lengththe length of the byte array out
peek_offsetthe offset into the stream to read at
Returns
length in bytes that was actually read and put into out

Implements Botan::DataSource.

Definition at line 105 of file data_src.cpp.

References end_of_data().

106  {
107  if(end_of_data())
108  throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
109 
110  size_t got = 0;
111 
112  if(offset)
113  {
114  secure_vector<byte> buf(offset);
115  source.read(reinterpret_cast<char*>(&buf[0]), buf.size());
116  if(source.bad())
117  throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
118  got = source.gcount();
119  }
120 
121  if(got == offset)
122  {
123  source.read(reinterpret_cast<char*>(out), length);
124  if(source.bad())
125  throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
126  got = source.gcount();
127  }
128 
129  if(source.eof())
130  source.clear();
131  source.seekg(total_read, std::ios::beg);
132 
133  return got;
134  }
bool end_of_data() const
Definition: data_src.cpp:139
size_t Botan::DataSource::peek_byte ( byte out) const
inherited

Peek at one byte.

Parameters
outan output byte
Returns
length in bytes that was actually read and put into out

Definition at line 27 of file data_src.cpp.

References Botan::DataSource::peek().

Referenced by Botan::ASN1::maybe_BER().

28  {
29  return peek(&out, 1, 0);
30  }
virtual size_t peek(byte out[], size_t length, size_t peek_offset) const =0
size_t Botan::DataSource_Stream::read ( byte  out[],
size_t  length 
)
virtual

Read from the source. Moves the internal offset so that every call to read will return a new portion of the source.

Parameters
outthe byte array to write the result to
lengththe length of the byte array out
Returns
length in bytes that was actually read and put into out

Implements Botan::DataSource.

Definition at line 91 of file data_src.cpp.

92  {
93  source.read(reinterpret_cast<char*>(out), length);
94  if(source.bad())
95  throw Stream_IO_Error("DataSource_Stream::read: Source failure");
96 
97  size_t got = source.gcount();
98  total_read += got;
99  return got;
100  }
size_t Botan::DataSource::read_byte ( byte out)
inherited

Read one byte.

Parameters
outthe byte to read to
Returns
length in bytes that was actually read and put into out

Definition at line 19 of file data_src.cpp.

References Botan::DataSource::read().

Referenced by Botan::PEM_Code::decode(), Botan::DataSource::discard_next(), Botan::BER_Decoder::discard_remaining(), Botan::PGP_decode(), and Botan::BER_Decoder::raw_bytes().

20  {
21  return read(&out, 1);
22  }
virtual size_t read(byte out[], size_t length)=0

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