selectable_binary_pipe.py :  » Development » SnapLogic » snaplogic » common » snapstream » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » SnapLogic 
SnapLogic » snaplogic » common » snapstream » selectable_binary_pipe.py
# $SnapHashLicense:
# 
# SnapLogic - Open source data services
# 
# Copyright (C) 2008, SnapLogic, Inc.  All rights reserved.
# 
# See http://www.snaplogic.org for more information about
# the SnapLogic project. 
# 
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2. See the LEGAL file
# at the top of the source tree.
# 
# "SnapLogic" is a trademark of SnapLogic, Inc.
# 
# 
# $

# $Id: selectable_binary_pipe.py 4402 2008-09-22 17:04:01Z kurt $

"""
Contains the SelectableBinaryPipe object.

Provides support for a SelectablePipe that handles binary data.

"""

from snaplogic.common.snapstream.selectable_pipe import *
from snaplogic.common.snapstream.string_buffer import StringBuffer
from snaplogic.common.snapstream import memory

class SelectableBinaryPipe(SelectablePipe):
    """
    A SelectablePipe for binary streams.

    A binary stream of data is buffered as a string. This string is then made available via the SelectablePipe
    interface. To provide flexibility for working efficiently with binary pipes, SelectablePipe operations
    utilize a block size to determine availability. The block size defines the minimum amount of data that
    must be buffered to consider the pipe available for a read. The block size is determined by the block_size
    attribute on objects of this class. The default value is L{DEFAULT_BLOCK_SIZE}.

    In addition to the block-level functionality, the buffer property provides read access to the internal buffer
    of data. Also, the read() method allows data of arbitrary sizes to be read from the beginning of the buffer.
    """
    
    def __init__(self):
        super(SelectableBinaryPipe, self).__init__()
        self._buffer = StringBuffer()
        self.block_size = memory.DEFAULT_BLOCK_SIZE

    def _available_write(self):
        return self._buffer.length() < max(memory.MAX_BUFFER_SIZE, self.block_size)
    
    def _available_read(self):
        if self._buffer.length() < self.block_size:
            # If the stream is still open, more datga may come later and we should not become available
            # yet.
            return not self._open and (self._buffer.length() > 0)
        else:
            return True
    
    def _put(self, data):
        self._buffer.write(data)

    def _get(self):
        return self._buffer.read(self.block_size)

    def read(self, size):
        """
        Blocking read of arbitrary sized blocks of data.

        Reads a data block of up to size bytes out of the pipe.  If not enough data is available, the call
        will block until enough data is available. If EOF occurs before enough data is available, it will
        result in a short read of the remaining bytes. Futher attempts to read will follow the same semantics
        as get().

        This call is equivalent to changing the block_size to size and issuing a blocking get. The call is provided
        for the convenience of RP.

        @param size: Number of bytes to read.
        @type size: int

        @return: A buffer of bytes read. If EOF, None is returned.
        @rtype: string
        
        """
        self.block_size = size
        return self.get()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.