Pack current struct into stream. For parser internal use.
        
        :param stream: a buffered stream (File or BytesIO)
        
        :return: packed bytes length
766095
def _packto(self, stream):
        '''
        
        '''
        #self._logger.log(logging.DEBUG, 'packing %r', self)
        total_size = 0
        current = self
        while current is not None:
            total_size += current._parser.packto(current, stream)
            last = current
            current = getattr(current, '_sub', None)
        if hasattr(last, '_extra'):
            _extra = last._extra
            total_size += stream.write(_extra)
        return total_size
221132
def decode_header(cls, stream):
        
        read = stream.read
        data = read(2)
        if len(data) != 2:
            raise WebSocketError("Unexpected EOF while decoding header")
        first_byte, second_byte = struct.unpack('!BB', data)
        header = cls(
            fin=first_byte & cls.FIN_MASK == cls.FIN_MASK,
            opcode=first_byte & cls.OPCODE_MASK,
            flags=first_byte & cls.HEADER_FLAG_MASK,
            length=second_byte & cls.LENGTH_MASK)
        has_mask = second_byte & cls.MASK_MASK == cls.MASK_MASK
        if header.opcode > 0x07:
            if not header.fin:
                raise WebSocketError('Received fragmented control frame: {0!r}'.format(data))
            # Control frames MUST have a payload length of 125 bytes or less
            if header.length > 125:
                raise FrameTooLargeException('Control frame cannot be larger than 125 bytes: {0!r}'.format(data))
        if header.length == 126:
            # 16 bit length
            data = read(2)
            if len(data) != 2:
                raise WebSocketError('Unexpected EOF while decoding header')
            header.length = struct.unpack('!H', data)[0]
        elif header.length == 127:
            # 64 bit length
            data = read(8)
            if len(data) != 8:
                raise WebSocketError('Unexpected EOF while decoding header')
            header.length = struct.unpack('!Q', data)[0]
        if has_mask:
            mask = read(4)
            if len(mask) != 4:
                raise WebSocketError('Unexpected EOF while decoding header')
            header.mask = mask
        return header
474507
def stream(self, chunk_size=64*1024):
        ''
        #return self.jfs.stream(url='%s?mode=bin' % self.path, chunk_size=chunk_size)
        return self.jfs.stream(url=self.path, params={'mode':'bin'}, chunk_size=chunk_size)
1353775
def readonce(self, size = None):
        
        if self.eof:
            raise EOFError
        if self.errored:
            raise IOError('Stream is broken before EOF')
        if size is not None and size < len(self.data) - self.pos:
            ret = self.data[self.pos: self.pos + size]
            self.pos += size
            return ret
        else:
            ret = self.data[self.pos:]
            self.pos = len(self.data)
            if self.dataeof:
                self.eof = True
            return ret
696715
def GetStream(data=None):
        
        if len(__mstreams_available__) == 0:
            if data:
                mstream = MemoryStream(data)
                mstream.seek(0)
            else:
                mstream = MemoryStream()
            __mstreams__.append(mstream)
            return mstream

        mstream = __mstreams_available__.pop()

        if data is not None and len(data):
            mstream.Cleanup()
            mstream.write(data)

        mstream.seek(0)

        return mstream
1291031
def decode_value(stream):
    
    length = decode_length(stream)
    (value,) = unpack_value(">{:d}s".format(length), stream)
    return value
1257484
def len(self):
        """"""
        # The len property is needed for requests.
        # requests checks __len__, then len
        # Since we cannot implement __len__ because python 32-bit uses 32-bit
        # sizes, we implement this instead.
        def stream_len(stream):
            """Stream length"""
            cur = stream.tell()
            try:
                stream.seek(0, 2)
                return stream.tell() - cur
            finally:
                stream.seek(cur)

        return sum(stream_len(s) for s in self.streams)
169755
def parse_packets(stream):
    
    reader = util.Reader(stream)
    while True:
        try:
            value = reader.readfmt('B')
        except EOFError:
            return

        log.debug('prefix byte: %s', bin(value))
        assert util.bit(value, 7) == 1

        tag = util.low_bits(value, 6)
        if util.bit(value, 6) == 0:
            length_type = util.low_bits(tag, 2)
            tag = tag >> 2
            fmt = {0: '>B', 1: '>H', 2: '>L'}[length_type]
            packet_size = reader.readfmt(fmt)
        else:
            first = reader.readfmt('B')
            if first < 192:
                packet_size = first
            elif first < 224:
                packet_size = ((first - 192) << 8) + reader.readfmt('B') + 192
            elif first == 255:
                packet_size = reader.readfmt('>L')
            else:
                log.error('Partial Body Lengths unsupported')

        log.debug('packet length: %d', packet_size)
        packet_data = reader.read(packet_size)
        packet_type = PACKET_TYPES.get(tag)

        p = {'type': 'unknown', 'tag': tag, 'raw': packet_data}
        if packet_type is not None:
            try:
                p = packet_type(util.Reader(io.BytesIO(packet_data)))
                p['tag'] = tag
            except ValueError:
                log.exception('Skipping packet: %s', util.hexlify(packet_data))

        log.debug('packet "%s": %s', p['type'], p)
        yield p
515344
def as_bin(self, *args, **kwargs):
        
        f = io.BytesIO()
        self.stream(f, *args, **kwargs)
        return f.getvalue()
278482
def into_buffer(self, buf, offset):
    
    self.payload = containerize(exclude_fields(self))
    self.parser = MsgEphemerisGPSDepF._parser
    self.stream_payload.reset(buf, offset)
    return self.pack_into(buf, offset, self._build_payload)
252184
def _read_stream_for_size(stream):
    """"""
    size = 0
    while True:
        buf = stream.read(_BUFFER_SIZE)
        size += len(buf)
        if not buf:
            break
    return size
1320098
def _read_stream_as_string(stream, encoding):
    
    output = io.BytesIO()
    try:
        for data in stream:
            output.write(data)
        if encoding is None:
            encoding = 'utf-8'
        return output.getvalue().decode(encoding)
    finally:
        output.close()
    raise RuntimeError('could not write data to stream or decode bytes')
644198
def read_full(stream):
    
    assert stream, "stream is required"

    chunks = []
    chunk = yield stream.read()

    while chunk:
        chunks.append(chunk)
        chunk = yield stream.read()

    raise tornado.gen.Return(b''.join(chunks))
459704
def readfmt(stream, fmt):
    """"""
    size = struct.calcsize(fmt)
    blob = stream.read(size)
    return struct.unpack(fmt, blob)
1338953
def build_stream(self, obj, stream: BytesIO, context=None) -> None:
        
        if context is None:
            context = Context()
        if not isinstance(context, Context):
            context = Context(context)
        try:
            self._build_stream(obj, stream, context)
        except Error:
            raise
        except Exception as exc:
            raise BuildingError(str(exc))
687959
def dump(self, stream):
        
        items = (
            ('time', self.time),
            ('inc', self.inc),
        )
        # use ordered dict to retain order
        ts = collections.OrderedDict(items)
        json.dump(dict(ts=ts), stream)
1343706
def from_stream(cls, stream):
        
        px_width, px_height = cls._dimensions_from_stream(stream)
        return cls(px_width, px_height, 72, 72)