 |
Changeset 3687
- Timestamp:
- 06/29/08 18:03:13
(2 months ago)
- Author:
- kris
- Message:
adjusted load() and transfer() a bit
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3471 |
r3687 |
|
| 16 | 16 | |
|---|
| 17 | 17 | public import tango.io.model.IConduit; |
|---|
| | 18 | |
|---|
| | 19 | private import tango.stdc.stdlib : alloca; |
|---|
| 18 | 20 | |
|---|
| 19 | 21 | /******************************************************************************* |
|---|
| … | … | |
| 194 | 196 | final OutputStream copy (InputStream src) |
|---|
| 195 | 197 | { |
|---|
| 196 | | return transfer (src, this); |
|---|
| | 198 | transfer (src, this); |
|---|
| | 199 | return this; |
|---|
| 197 | 200 | } |
|---|
| 198 | 201 | |
|---|
| … | … | |
| 216 | 219 | /*********************************************************************** |
|---|
| 217 | 220 | |
|---|
| 218 | | Transfer the content of one stream to another. Returns |
|---|
| 219 | | the dst OutputStream, and throws IOException on failure. |
|---|
| 220 | | Queries dst to identify what size of buffer to utilize. |
|---|
| 221 | | |
|---|
| 222 | | ***********************************************************************/ |
|---|
| 223 | | |
|---|
| 224 | | static OutputStream transfer (InputStream src, OutputStream dst) |
|---|
| 225 | | { |
|---|
| 226 | | uint len = 0; |
|---|
| 227 | | auto con = dst.conduit; |
|---|
| 228 | | auto tmp = new byte [con.bufferSize]; |
|---|
| 229 | | while ((len = src.read(tmp)) != IConduit.Eof) |
|---|
| 230 | | { |
|---|
| 231 | | auto p = tmp.ptr; |
|---|
| 232 | | for (uint j; len > 0; len -= j, p += j) |
|---|
| 233 | | if ((j = dst.write (p[0..len])) is IConduit.Eof) |
|---|
| 234 | | con.error ("Conduit.copy :: Eof while writing to: "~con.toString); |
|---|
| 235 | | } |
|---|
| 236 | | delete tmp; |
|---|
| 237 | | return dst; |
|---|
| 238 | | } |
|---|
| 239 | | |
|---|
| 240 | | /*********************************************************************** |
|---|
| 241 | | |
|---|
| 242 | 221 | |
|---|
| 243 | 222 | Load the bits from a stream, and return them all in an |
|---|
| … | … | |
| 252 | 231 | static void[] load (InputStream src, void[] dst = null) |
|---|
| 253 | 232 | { |
|---|
| 254 | | auto chunk = 0; |
|---|
| 255 | 233 | auto index = 0; |
|---|
| | 234 | auto chunk = 256; |
|---|
| 256 | 235 | |
|---|
| 257 | | while (chunk != Eof) |
|---|
| | 236 | do { |
|---|
| | 237 | if (dst.length - index < chunk) |
|---|
| | 238 | dst.length = dst.length + (chunk * 2); |
|---|
| | 239 | |
|---|
| | 240 | chunk = src.read (dst[index .. $]); |
|---|
| | 241 | index += chunk; |
|---|
| | 242 | } while (chunk != Eof) |
|---|
| | 243 | |
|---|
| | 244 | return dst [0 .. index - chunk]; |
|---|
| | 245 | } |
|---|
| | 246 | |
|---|
| | 247 | /*********************************************************************** |
|---|
| | 248 | |
|---|
| | 249 | Low-level data transfer, where max represents the maximum |
|---|
| | 250 | number of bytes to transfer, and tmp represents space for |
|---|
| | 251 | buffering the transfer. Throws IOException on failure. |
|---|
| | 252 | |
|---|
| | 253 | ***********************************************************************/ |
|---|
| | 254 | |
|---|
| | 255 | static size_t transfer (InputStream src, OutputStream dst, size_t max=size_t.max) |
|---|
| | 256 | { |
|---|
| | 257 | byte[8192] tmp; |
|---|
| | 258 | size_t done; |
|---|
| | 259 | |
|---|
| | 260 | while (max) |
|---|
| 258 | 261 | { |
|---|
| 259 | | if (dst.length - index < 1024) |
|---|
| 260 | | dst.length = dst.length + 16 * 1024; |
|---|
| 261 | | |
|---|
| 262 | | chunk = src.read (dst[index .. $]); |
|---|
| 263 | | index += chunk; |
|---|
| 264 | | } |
|---|
| 265 | | |
|---|
| 266 | | return dst [0 .. index - chunk]; |
|---|
| | 262 | auto len = max; |
|---|
| | 263 | if (len > tmp.length) |
|---|
| | 264 | len = tmp.length; |
|---|
| | 265 | |
|---|
| | 266 | if ((len = src.read(tmp[0 .. len])) is IConduit.Eof) |
|---|
| | 267 | max = 0; |
|---|
| | 268 | else |
|---|
| | 269 | { |
|---|
| | 270 | max -= len; |
|---|
| | 271 | done += len; |
|---|
| | 272 | auto p = tmp.ptr; |
|---|
| | 273 | for (uint j; len > 0; len -= j, p += j) |
|---|
| | 274 | if ((j = dst.write (p[0 .. len])) is IConduit.Eof) |
|---|
| | 275 | dst.conduit.error ("Conduit.copy :: Eof while writing to: "~dst.conduit.toString); |
|---|
| | 276 | } |
|---|
| | 277 | } |
|---|
| | 278 | |
|---|
| | 279 | return done; |
|---|
| 267 | 280 | } |
|---|
| 268 | 281 | } |
|---|
| … | … | |
| 429 | 442 | OutputStream copy (InputStream src) |
|---|
| 430 | 443 | { |
|---|
| 431 | | return Conduit.transfer (src, this); |
|---|
| | 444 | Conduit.transfer (src, this); |
|---|
| | 445 | return this; |
|---|
| 432 | 446 | } |
|---|
| 433 | 447 | |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic