1 //----------------------------------------------------------------------
2 // This software is part of the OpenBeOS distribution and is covered
5 // Copyright (c) 2003 Tyler Dauwalder, tyler@dauwalder.net
6 //----------------------------------------------------------------------
8 /*! \file SimulatedStream.cpp
11 #include "SimulatedStream.h"
18 SimulatedStream::SimulatedStream(DataStream
&stream
)
25 SimulatedStream::InitCheck() const
27 return fStream
.InitCheck();
31 SimulatedStream::Read(void *_buffer
, size_t size
)
33 uint8
*buffer
= reinterpret_cast<uint8
*>(_buffer
);
34 status_t error
= buffer
? B_OK
: B_BAD_VALUE
;
35 ssize_t bytesTotal
= 0;
36 while (error
== B_OK
&& size
> 0) {
38 error
= _GetExtent(fPosition
, size
, extent
);
40 if (extent
.size
> 0) {
41 ssize_t bytes
= fStream
.ReadAt(extent
.offset
, buffer
, extent
.size
);
48 error
= status_t(bytes
);
51 // end of simulated stream
56 return !error
? bytesTotal
: ssize_t(error
);
60 SimulatedStream::ReadAt(off_t pos
, void *_buffer
, size_t size
)
62 uint8
*buffer
= reinterpret_cast<uint8
*>(_buffer
);
63 status_t error
= buffer
? B_OK
: B_BAD_VALUE
;
64 ssize_t bytesTotal
= 0;
65 while (error
== B_OK
&& size
> 0) {
67 error
= _GetExtent(pos
, size
, extent
);
69 if (extent
.size
> 0) {
70 ssize_t bytes
= fStream
.ReadAt(extent
.offset
, buffer
, extent
.size
);
77 error
= status_t(bytes
);
80 // end of simulated stream
85 return !error
? bytesTotal
: ssize_t(error
);
88 /*! \brief Writes \a size bytes worth of data from \a buffer at the current
89 position in the stream, incrementing the stream's position marker as it goes.
92 SimulatedStream::Write(const void *_buffer
, size_t size
)
94 const uint8
*buffer
= reinterpret_cast<const uint8
*>(_buffer
);
95 status_t error
= buffer
? B_OK
: B_BAD_VALUE
;
96 ssize_t bytesTotal
= 0;
97 while (error
== B_OK
&& size
> 0) {
99 error
= _GetExtent(fPosition
, size
, extent
);
101 if (extent
.size
> 0) {
102 ssize_t bytes
= fStream
.WriteAt(extent
.offset
, buffer
, extent
.size
);
109 error
= status_t(bytes
);
112 // end of simulated stream
117 return !error
? bytesTotal
: ssize_t(error
);
120 /*! \brief Writes \a size bytes worth of data from \a buffer at position
121 \a pos in the stream without incrementing the stream's position marker.
124 SimulatedStream::WriteAt(off_t pos
, const void *_buffer
, size_t size
)
126 const uint8
*buffer
= reinterpret_cast<const uint8
*>(_buffer
);
127 status_t error
= buffer
? B_OK
: B_BAD_VALUE
;
128 ssize_t bytesTotal
= 0;
129 while (error
== B_OK
&& size
> 0) {
131 error
= _GetExtent(pos
, size
, extent
);
133 if (extent
.size
> 0) {
134 ssize_t bytes
= fStream
.WriteAt(extent
.offset
, buffer
, extent
.size
);
141 error
= status_t(bytes
);
144 // end of simulated stream
149 return !error
? bytesTotal
: ssize_t(error
);
152 /*! \brief Writes \a size bytes worth of data from \a data at the current
153 position in the stream, incrementing the stream's position marker as it goes.
156 SimulatedStream::Write(BDataIO
&data
, size_t size
)
158 DEBUG_INIT_ETC("SimulatedStream", ("size: %ld", size
));
159 status_t error
= B_OK
;
160 ssize_t bytesTotal
= 0;
161 while (error
== B_OK
&& size
> 0) {
163 error
= _GetExtent(fPosition
, size
, extent
);
165 if (extent
.size
> 0) {
166 PRINT(("writing to underlying stream (offset: %llu, size: %ld)\n", extent
.offset
, extent
.size
));
167 ssize_t bytes
= fStream
.WriteAt(extent
.offset
, data
, extent
.size
);
173 error
= status_t(bytes
);
176 // end of simulated stream
181 RETURN(!error
? bytesTotal
: ssize_t(error
));
184 /*! \brief Writes \a size bytes worth of data from \a data at position
185 \a pos in the stream without incrementing the stream's position marker.
188 SimulatedStream::WriteAt(off_t pos
, BDataIO
&data
, size_t size
)
190 status_t error
= B_OK
;
191 ssize_t bytesTotal
= 0;
192 while (error
== B_OK
&& size
> 0) {
194 error
= _GetExtent(pos
, size
, extent
);
196 if (extent
.size
> 0) {
197 ssize_t bytes
= fStream
.WriteAt(extent
.offset
, data
, extent
.size
);
203 error
= status_t(bytes
);
206 // end of simulated stream
211 return !error
? bytesTotal
: ssize_t(error
);
214 /*! \brief Writes \a size bytes worth of zeros at the current position
215 in the stream, incrementing the stream's position marker as it goes.
218 SimulatedStream::Zero(size_t size
)
220 status_t error
= B_OK
;
221 ssize_t bytesTotal
= 0;
222 while (error
== B_OK
&& size
> 0) {
224 error
= _GetExtent(fPosition
, size
, extent
);
226 if (extent
.size
> 0) {
227 ssize_t bytes
= fStream
.ZeroAt(extent
.offset
, extent
.size
);
233 error
= status_t(bytes
);
236 // end of simulated stream
241 return !error
? bytesTotal
: ssize_t(error
);
244 /*! \brief Writes \a size bytes worth of zeros at position \a pos
245 in the stream without incrementing the stream's position marker.
248 SimulatedStream::ZeroAt(off_t pos
, size_t size
)
250 status_t error
= B_OK
;
251 ssize_t bytesTotal
= 0;
252 while (error
== B_OK
&& size
> 0) {
254 error
= _GetExtent(pos
, size
, extent
);
256 if (extent
.size
> 0) {
257 ssize_t bytes
= fStream
.ZeroAt(extent
.offset
, extent
.size
);
263 error
= status_t(bytes
);
266 // end of simulated stream
271 return !error
? bytesTotal
: ssize_t(error
);
275 SimulatedStream::Seek(off_t pos
, uint32 seek_mode
)
277 off_t size
= _Size();
286 fPosition
= size
+ pos
;
294 else if (fPosition
> size
&& SetSize(fPosition
) != B_OK
)