Botan  1.10.9
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)
 
size_t discard_next (size_t N)
 
bool end_of_data () const
 
std::string id () const
 
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 125 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 190 of file data_src.cpp.

191  :
192  identifier(name),
193  source_p(0),
194  source(in),
195  total_read(0)
196  {
197  }
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 171 of file data_src.cpp.

172  :
173  identifier(path),
174  source_p(new std::ifstream(
175  path.c_str(),
176  use_binary ? std::ios::binary : std::ios::in)),
177  source(*source_p),
178  total_read(0)
179  {
180  if(!source.good())
181  {
182  delete source_p;
183  throw Stream_IO_Error("DataSource: Failure opening file " + path);
184  }
185  }
Botan::DataSource_Stream::~DataSource_Stream ( )

Definition at line 202 of file data_src.cpp.

203  {
204  delete source_p;
205  }

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 n, and 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  }
BigInt n
Definition: numthry.cpp:26
unsigned char byte
Definition: types.h:22
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 155 of file data_src.cpp.

Referenced by peek().

156  {
157  return (!source.good());
158  }
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 163 of file data_src.cpp.

164  {
165  return identifier;
166  }
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 121 of file data_src.cpp.

References end_of_data(), and Botan::MemoryRegion< T >::size().

122  {
123  if(end_of_data())
124  throw Invalid_State("DataSource_Stream: Cannot peek when out of data");
125 
126  size_t got = 0;
127 
128  if(offset)
129  {
130  SecureVector<byte> buf(offset);
131  source.read(reinterpret_cast<char*>(&buf[0]), buf.size());
132  if(source.bad())
133  throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
134  got = source.gcount();
135  }
136 
137  if(got == offset)
138  {
139  source.read(reinterpret_cast<char*>(out), length);
140  if(source.bad())
141  throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
142  got = source.gcount();
143  }
144 
145  if(source.eof())
146  source.clear();
147  source.seekg(total_read, std::ios::beg);
148 
149  return got;
150  }
bool end_of_data() const
Definition: data_src.cpp:155
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 107 of file data_src.cpp.

108  {
109  source.read(reinterpret_cast<char*>(out), length);
110  if(source.bad())
111  throw Stream_IO_Error("DataSource_Stream::read: Source failure");
112 
113  size_t got = source.gcount();
114  total_read += got;
115  return got;
116  }
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: