~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-05-22 12:53:10 UTC
  • mfrom: (3408.6.3 eric)
  • Revision ID: pqm@pqm.ubuntu.com-20080522125310-lneifpa40hzg4lu2
Fix MemoryError during large fetches over HTTP. (Eric Holmberg)

Show diffs side-by-side

added added

removed removed

Lines of Context:
529
529
    return False
530
530
 
531
531
 
532
 
def pumpfile(fromfile, tofile):
 
532
def pumpfile(from_file, to_file, read_length=-1, buff_size=32768):
533
533
    """Copy contents of one file to another.
534
 
    
 
534
 
 
535
    The read_length can either be -1 to read to end-of-file (EOF) or
 
536
    it can specify the maximum number of bytes to read.
 
537
 
 
538
    The buff_size represents the maximum size for each read operation
 
539
    performed on from_file.
 
540
 
535
541
    :return: The number of bytes copied.
536
542
    """
537
 
    BUFSIZE = 32768
538
543
    length = 0
539
 
    while True:
540
 
        b = fromfile.read(BUFSIZE)
541
 
        if not b:
542
 
            break
543
 
        tofile.write(b)
544
 
        length += len(b)
 
544
    if read_length >= 0:
 
545
        # read specified number of bytes
 
546
 
 
547
        while read_length > 0:
 
548
            num_bytes_to_read = min(read_length, buff_size)
 
549
 
 
550
            block = from_file.read(num_bytes_to_read)
 
551
            if not block:
 
552
                # EOF reached
 
553
                break
 
554
            to_file.write(block)
 
555
 
 
556
            actual_bytes_read = len(block)
 
557
            read_length -= actual_bytes_read
 
558
            length += actual_bytes_read
 
559
    else:
 
560
        # read to EOF
 
561
        while True:
 
562
            block = from_file.read(buff_size)
 
563
            if not block:
 
564
                # EOF reached
 
565
                break
 
566
            to_file.write(block)
 
567
            length += len(block)
545
568
    return length
546
569
 
547
570