Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 3687

Show
Ignore:
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
  • trunk/tango/io/Conduit.d

    r3471 r3687  
    1616 
    1717public  import  tango.io.model.IConduit; 
     18 
     19private import tango.stdc.stdlib : alloca; 
    1820 
    1921/******************************************************************************* 
     
    194196        final OutputStream copy (InputStream src) 
    195197        { 
    196                 return transfer (src, this); 
     198                transfer (src, this); 
     199                return this; 
    197200        } 
    198201 
     
    216219        /*********************************************************************** 
    217220 
    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  
    242221 
    243222                Load the bits from a stream, and return them all in an 
     
    252231        static void[] load (InputStream src, void[] dst = null) 
    253232        { 
    254                 auto chunk = 0; 
    255233                auto index = 0; 
     234                auto chunk = 256; 
    256235                 
    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) 
    258261                      { 
    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; 
    267280        } 
    268281} 
     
    429442        OutputStream copy (InputStream src) 
    430443        { 
    431                 return Conduit.transfer (src, this); 
     444                Conduit.transfer (src, this); 
     445                return this; 
    432446        } 
    433447