21 using std::streamsize;
32 _filename = _file->get_filename();
39 if (_vfile ==
nullptr) {
43 _out = _vfile->open_write_file(
true,
true);
44 _owns_out = (_out !=
nullptr);
45 return _owns_out && !_out->fail();
61 if (!filename.empty()) {
85 _wrote_first_datagram =
false;
97 nassertr(_out !=
nullptr,
false);
98 nassertr(!_wrote_first_datagram,
false);
100 _out->write(header.data(), header.size());
101 thread_consider_yield();
102 return !_out->fail();
111 nassertr(_out !=
nullptr,
false);
112 _wrote_first_datagram =
true;
116 size_t num_bytes = data.get_length();
117 if (num_bytes == (uint32_t)-1 || num_bytes != (uint32_t)num_bytes) {
127 _out->write((
const char *)data.get_data(), data.get_length());
128 thread_consider_yield();
130 return !_out->fail();
144 nassertr(_out !=
nullptr,
false);
145 _wrote_first_datagram =
true;
149 if (vfile ==
nullptr) {
152 std::istream *in = vfile->open_read_file(
true);
157 streamsize size = vfile->get_file_size(in);
158 streamsize num_remaining = size;
161 if (num_remaining == (uint32_t)-1 || num_remaining != (uint32_t)num_remaining) {
170 static const size_t buffer_size = 4096;
171 char buffer[buffer_size];
173 streampos start = _out->tellp();
174 in->read(buffer, min((streamsize)buffer_size, num_remaining));
175 streamsize count = in->gcount();
177 _out->write(buffer, count);
179 vfile->close_read_file(in);
182 num_remaining -= count;
183 if (num_remaining == 0) {
186 in->read(buffer, min((streamsize)buffer_size, num_remaining));
187 count = in->gcount();
190 vfile->close_read_file(in);
192 if (num_remaining != 0) {
194 <<
"Truncated input stream.\n";
213 nassertr(_out !=
nullptr,
false);
214 _wrote_first_datagram =
true;
221 streamsize num_remaining = source.
get_size();
224 if (num_remaining == (uint32_t)-1 || num_remaining != (uint32_t)num_remaining) {
233 static const size_t buffer_size = 4096;
234 char buffer[buffer_size];
236 streampos start = _out->tellp();
238 in.read(buffer, min((streamsize)buffer_size, num_remaining));
239 streamsize count = in.gcount();
241 _out->write(buffer, count);
245 num_remaining -= count;
246 if (num_remaining == 0) {
249 in.read(buffer, min((streamsize)buffer_size, num_remaining));
253 if (num_remaining != 0) {
255 <<
"Truncated input stream.\n";
268 if (_out ==
nullptr) {
284 if (_out !=
nullptr) {
318 if (_out ==
nullptr) {
321 return _out->tellp();
A StreamWriter object is used to write sequential binary data directly to an ostream.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual std::streampos get_file_pos()
Returns the current file position within the data stream, if any, or 0 if the file position is not me...
std::streamsize get_size() const
Returns the number of consecutive bytes, beginning at get_start(), that correspond to this file data.
Keeps a reference-counted pointer to a file on disk.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A hierarchy of directories and files that appears to be one continuous file system,...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_binary()
Indicates that the filename represents a binary file.
bool write_header(const std::string &header)
Writes a sequence of bytes to the beginning of the datagram file.
bool open_read(std::ifstream &stream) const
Opens the indicated ifstream for reading the file, if possible.
virtual const Filename & get_filename()
Returns the filename that provides the target for these datagrams, if any, or empty string if the dat...
virtual bool is_error()
Returns true if the file has reached an error condition.
The abstract base class for a file or directory within the VirtualFileSystem.
PointerTo< VirtualFile > create_file(const Filename &filename)
Attempts to create a file by the indicated name in the filesystem, if possible, and returns it.
virtual bool copy_datagram(SubfileInfo &result, const Filename &filename)
Copies the file data from the entire indicated file (via the vfs) as the next datagram.
bool open(const FileReference *file)
Opens the indicated filename for writing.
virtual bool put_datagram(const Datagram &data)
Writes the given datagram to the file.
The name of a file, such as a texture file or an Egg file.
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
PointerTo< VirtualFile > get_file(const Filename &filename, bool status_only=false) const
Looks up the file by the indicated name in the file system.
virtual void flush()
Ensures that all datagrams previously written will be visible in the output file.
const Filename & get_filename() const
A shortcut to the filename.
static void close_write_file(std::ostream *stream)
Closes a file opened by a previous call to open_write_file().
virtual const FileReference * get_file()
Returns the FileReference that provides the target for these datagrams, if any, or NULL if the datagr...
void add_uint64(uint64_t value)
Adds an unsigned 64-bit integer to the stream.
This class records a particular byte sub-range within an existing file on disk.
void add_uint32(uint32_t value)
Adds an unsigned 32-bit integer to the stream.
void close()
Closes the file.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
std::streampos get_start() const
Returns the offset within the file at which this file data begins.