1 // IOChannel.h - a virtual IO channel, for Gnash
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 Free Software Foundation, Inc
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef GNASH_IOCHANNEL_H
22 #define GNASH_IOCHANNEL_H
25 #include <boost/cstdint.hpp> // for boost int types
28 #include "dsodefs.h" // DSOEXPORT
29 #include "GnashException.h" // for IOException inheritance
33 /// Exception signalling an IO error
34 class DSOEXPORT IOException
: public GnashException
37 IOException(const std::string
& s
) : GnashException(s
) {}
38 IOException() : GnashException("IO error") {}
41 /// A virtual IO channel
42 class DSOEXPORT IOChannel
46 virtual ~IOChannel() {}
48 /// \brief Read a 32-bit word from a little-endian stream.
49 /// returning it as a native-endian word.
51 /// Throw IOException on error
53 boost::uint32_t read_le32();
55 /// Read a 16-bit word from a little-endian stream.
57 /// Throw IOException on error
59 boost::uint16_t read_le16();
61 /// Read a single byte from the stream
63 /// Throw IOException on error
65 boost::uint8_t read_byte();
67 /// Read the given number of bytes from the stream
69 /// Return the number of bytes actually read.
70 /// EOF might cause it to be < num.
72 /// Throw IOException on error
74 virtual std::streamsize
read(void* dst
, std::streamsize num
)=0;
76 /// Read at most the given number of bytes w/out blocking
78 /// Throw IOException on error
80 /// @return The number of bytes actually read.
81 /// A short count may mean EOF was hit or
82 /// data didn't arrive yet.
84 /// Default implementation proxies the call to the
87 virtual std::streamsize
readNonBlocking(void* dst
, std::streamsize num
)
89 return read(dst
, num
);
92 /// Write the given number of bytes to the stream
94 /// Throw IOException on error/unsupported op.
96 virtual std::streamsize
write(const void* src
, std::streamsize num
);
99 /// Read up to max_length characters, returns the number of characters
100 /// read, or -1 if the string length is longer than max_length.
102 /// Stops at the first \0 character if it comes before max_length.
104 /// Guarantees termination of the string.
106 /// @return the number of characters read, or -1 no null-termination
107 /// was found within max_length
109 /// Throw IOException on error
111 int read_string(char* dst
, int max_length
);
113 /// Read a 32-bit float from a little-endian stream.
115 /// NOTE: this currently relies on host FP format being the
116 /// same as the Flash one (presumably IEEE 754).
118 /// Throw IOException on error
120 float read_float32();
122 /// Return current stream position
124 /// Throw IOException on error
126 virtual std::streampos
tell() const = 0;
128 /// Seek to the specified position
131 /// Throw IOException on error
133 /// @return true on success, or false on failure.
135 virtual bool seek(std::streampos p
) = 0;
137 /// Seek to the end of the stream
139 /// Throw IOException on error
141 virtual void go_to_end() = 0;
143 /// Return true if the end of the stream has been reached.
145 /// Throw IOException on error
147 virtual bool eof() const = 0;
149 /// Return true if the stream is in an error state
151 /// When the stream is in an error state there's nothing
152 /// you can do about it, just delete it and log the error.
153 virtual bool bad() const = 0;
155 /// Get the size of the stream (unreliably).
157 /// Size of stream is unreliable as not all input
158 /// channels have a mechanism to advertise size,
159 /// and some have one but isn't necessarely truthful
160 /// (a few HTTP severs are bogus in this reguard).
162 /// @return unreliable input size, (size_t)-1 if not known.
164 virtual size_t size() const { return static_cast<size_t>(-1); }
170 #endif // GNASH_IOCHANNEL_H
175 // indent-tabs-mode: t