2 Copyright (c) 2002-2009 Tampere University.
4 This file is part of TTA-Based Codesign Environment (TCE).
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
25 * @file BinaryStream.icc
27 * Implementation of BinaryStream class inline functions.
29 * @author Ari Metsähalme 2003 (ari.metsahalme-no.spam-tut.fi)
30 * @note reviewed 7 August 2003 by pj, am, jn, ao, rl
32 * @note rating: yellow
35 #include "Exception.hh"
36 #include "BinaryStream.hh"
41 * Reads a single byte from the stream.
43 * Opens the stream if it's not opened yet.
45 * @return One byte from the stream.
46 * @exception UnreachableStream If stream is bad.
47 * @exception EndOfFile If end of file was reached unexpectedly.
50 BinaryStream::getByte() {
51 if (!iStream_.is_open()) {
56 throw UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
59 throw EndOfFile(__FILE__, __LINE__, __func__, fileName_);
62 Byte result = iStream_.get();
65 throw UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
72 * Writes a single byte to the stream.
74 * Opens the stream if it's not opened yet.
76 * @note If write position is set past the end of file, putByte will throw
77 * UnreachableStream exception.
79 * @param byte The byte to write to the stream.
80 * @exception UnreachableStream If stream is bad.
81 * @exception WritePastEOF If write position is set past the end of
82 * file and writing is attempted.
85 BinaryStream::putByte(Byte byte) {
86 if (extOStream_ != NULL) {
87 extOStream_->put(byte);
91 if (!oStream_.is_open()) {
92 openOutput(fileName_);
96 throw UnreachableStream(__FILE__, __LINE__, __func__, fileName_);
99 unsigned int writePos = oStream_.tellp();
100 oStream_.seekp(0, std::ios::end);
101 unsigned int eofPos = oStream_.tellp();
103 if (writePos > eofPos) {
104 oStream_.seekp(writePos);
105 throw WritePastEOF(__FILE__, __LINE__, __func__, fileName_);
108 oStream_.seekp(writePos);
111 if (oStream_.bad()) {
112 throw UnreachableStream(__FILE__, __LINE__, __func__, fileName_);