3
from zope.interface import implements
4
from twisted.web2.stream import SimpleStream, IByteStream
5
from twisted.vfs.ivfs import IFileSystemLeaf, VFSError
6
from twisted.python import components
8
class FileSystemLeafStream(SimpleStream):
9
implements(IByteStream)
10
"""A stream that reads data from a FileSystemLeaf."""
11
# 65K, minus some slack
12
CHUNK_SIZE = 2 ** 2 ** 2 ** 2 - 32
14
def __init__(self, leaf, start=0, length=None):
16
Create the stream from leaf. If you specify start and length,
17
use only that portion of the leaf.
20
self.leaf.open(os.O_RDONLY)
23
self.length = leaf.getMetadata()['size']
36
readSize = min(length, self.CHUNK_SIZE)
37
b = self.leaf.readChunk(self.start, readSize)
40
if bytesRead != readSize:
42
("Ran out of data reading vfs leaf %r, expected %d more bytes,"
43
" got %d") % (self.leaf, readSize, bytesRead))
45
self.length -= bytesRead
46
self.start += bytesRead
52
SimpleStream.close(self)
54
components.registerAdapter(FileSystemLeafStream, IFileSystemLeaf, IByteStream)