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 PositionIOStream.cpp
11 #include "PositionIOStream.h"
16 PositionIOStream::PositionIOStream(BPositionIO
&stream
)
22 PositionIOStream::Read(void *buffer
, size_t size
)
24 return fStream
.Read(buffer
, size
);
28 PositionIOStream::ReadAt(off_t pos
, void *buffer
, size_t size
)
30 return fStream
.ReadAt(pos
, buffer
, size
);
34 PositionIOStream::Write(const void *buffer
, size_t size
)
36 return fStream
.Write(buffer
, size
);
40 PositionIOStream::WriteAt(off_t pos
, const void *buffer
, size_t size
)
42 return fStream
.WriteAt(pos
, buffer
, size
);
45 /*! \brief Writes \a size bytes worth of data from \a data at the current
46 position in the file, incrementing the file's position marker as it goes.
49 PositionIOStream::Write(BDataIO
&data
, size_t size
)
51 status_t error
= kBufferSize
> 0 ? B_OK
: B_BAD_VALUE
;
54 void *buffer
= malloc(kBufferSize
);
55 error
= buffer
? B_OK
: B_NO_MEMORY
;
57 // Fudge the buffer size from here out if the requested
58 // number of bytes to write is smaller than the buffer
59 size_t bufferSize
= (size
< kBufferSize
? size
: kBufferSize
);
61 memset(buffer
, 0, bufferSize
);
63 while (bytes
< size
) {
64 ssize_t bytesRead
= data
.Read(buffer
, bufferSize
);
66 ssize_t bytesWritten
= fStream
.Write(buffer
, bytesRead
);
67 if (bytesWritten
>= 0) {
68 bytes
+= bytesWritten
;
70 error
= status_t(bytesWritten
);
74 error
= status_t(bytesRead
);
81 return error
? ssize_t(error
) : ssize_t(bytes
);
84 /*! \brief Writes \a size bytes worth of data from \a data at position
85 \a pos in the file without incrementing the file's position marker.
88 PositionIOStream::WriteAt(off_t pos
, BDataIO
&data
, size_t size
)
90 status_t error
= kBufferSize
> 0 ? B_OK
: B_BAD_VALUE
;
93 void *buffer
= malloc(kBufferSize
);
94 error
= buffer
? B_OK
: B_NO_MEMORY
;
96 // Fudge the buffer size from here out if the requested
97 // number of bytes to write is smaller than the buffer
98 size_t bufferSize
= (size
< kBufferSize
? size
: kBufferSize
);
100 memset(buffer
, 0, bufferSize
);
102 while (bytes
< size
) {
103 ssize_t bytesRead
= data
.Read(buffer
, bufferSize
);
104 if (bytesRead
>= 0) {
105 ssize_t bytesWritten
= fStream
.WriteAt(pos
, buffer
, bytesRead
);
106 if (bytesWritten
>= 0) {
107 bytes
+= bytesWritten
;
110 error
= status_t(bytesWritten
);
114 error
= status_t(bytesRead
);
121 return error
? ssize_t(error
) : ssize_t(bytes
);
124 /*! \brief Writes \a size bytes worth of zeros at the current position
125 in the file, incrementing the file's position marker as it goes.
128 PositionIOStream::Zero(size_t size
)
130 status_t error
= kBufferSize
> 0 ? B_OK
: B_BAD_VALUE
;
133 void *buffer
= malloc(kBufferSize
);
134 error
= buffer
? B_OK
: B_NO_MEMORY
;
136 // Fudge the buffer size from here out if the requested
137 // number of bytes to write is smaller than the buffer
138 size_t bufferSize
= (size
< kBufferSize
? size
: kBufferSize
);
140 memset(buffer
, 0, bufferSize
);
142 while (bytes
< size
) {
143 ssize_t bytesWritten
= fStream
.Write(buffer
, bufferSize
);
144 if (bytesWritten
>= 0) {
145 bytes
+= bytesWritten
;
147 error
= status_t(bytesWritten
);
154 return error
? ssize_t(error
) : ssize_t(bytes
);
157 /*! \brief Writes \a size bytes worth of zeros at position \a pos
158 in the file without incrementing the file's position marker.
161 PositionIOStream::ZeroAt(off_t pos
, size_t size
)
163 status_t error
= kBufferSize
> 0 ? B_OK
: B_BAD_VALUE
;
166 void *buffer
= malloc(kBufferSize
);
167 error
= buffer
? B_OK
: B_NO_MEMORY
;
169 // Fudge the buffer size from here out if the requested
170 // number of bytes to write is smaller than the buffer
171 size_t bufferSize
= (size
< kBufferSize
? size
: kBufferSize
);
173 memset(buffer
, 0, bufferSize
);
175 while (bytes
< size
) {
176 ssize_t bytesWritten
= fStream
.WriteAt(pos
, buffer
, bufferSize
);
177 if (bytesWritten
>= 0) {
178 bytes
+= bytesWritten
;
181 error
= status_t(bytesWritten
);
188 return error
? ssize_t(error
) : ssize_t(bytes
);
192 PositionIOStream::Seek(off_t position
, uint32 seek_mode
)
194 return fStream
.Seek(position
, seek_mode
);
198 PositionIOStream::Position() const
200 return fStream
.Position();
204 PositionIOStream::SetSize(off_t size
)
206 return fStream
.SetSize(size
);