<s> from concurrence . timer import Timeout <EOL> from concurrence . io import IOStream , Buffer , BufferOverflowError , BufferUnderflowError , BufferInvalidArgumentError <EOL> class BufferedReader ( object ) : <EOL> def __init__ ( self , stream , buffer ) : <EOL> assert isinstance ( stream , IOStream ) <EOL> self . stream = stream <EOL> self . buffer = buffer <EOL> self . buffer . position = <NUM_LIT:0> <EOL> self . buffer . limit = <NUM_LIT:0> <EOL> def file ( self ) : <EOL> return CompatibleFile ( self , None ) <EOL> def clear ( self ) : <EOL> self . buffer . clear ( ) <EOL> def _read_more ( self ) : <EOL> self . buffer . compact ( ) <EOL> if not self . stream . read ( self . buffer , Timeout . current ( ) ) : <EOL> raise EOFError ( "<STR_LIT>" ) <EOL> self . buffer . flip ( ) <EOL> def read_lines ( self ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> try : <EOL> yield self . buffer . read_line ( ) <EOL> except BufferUnderflowError : <EOL> self . _read_more ( ) <EOL> def read_line ( self ) : <EOL> return self . read_lines ( ) . next ( ) <EOL> def read_bytes ( self , n ) : <EOL> """<STR_LIT>""" <EOL> buffer = self . buffer <EOL> s = [ ] <EOL> while n > <NUM_LIT:0> : <EOL> r = buffer . remaining <EOL> if r > <NUM_LIT:0> : <EOL> s . append ( buffer . read_bytes ( min ( n , r ) ) ) <EOL> n -= r <EOL> else : <EOL> self . _read_more ( ) <EOL> return '<STR_LIT>' . join ( s ) <EOL> def read_short ( self ) : <EOL> while True : <EOL> try : <EOL> return self . buffer . read_short ( ) <EOL> except BufferUnderflowError : <EOL> self . _read_more ( ) <EOL> class BufferedWriter ( object ) : <EOL> def __init__ ( self , stream , buffer ) : <EOL> assert isinstance ( stream , IOStream ) <EOL> self . stream = stream <EOL> self . buffer = buffer <EOL> def file ( self ) : <EOL> return CompatibleFile ( None , self ) <EOL> def clear ( self ) : <EOL> self . buffer . clear ( ) <EOL> def write_bytes ( self , s ) : <EOL> assert type ( s ) == str , "<STR_LIT>" <EOL> try : <EOL> self . buffer . write_bytes ( s ) <EOL> except BufferOverflowError : <EOL> while s : <EOL> r = self . buffer . remaining <EOL> part , s = s [ : r ] , s [ r : ] <EOL> self . buffer . write_bytes ( part ) <EOL> self . flush ( ) <EOL> def write_byte ( self , ch ) : <EOL> assert type ( ch ) == int , "<STR_LIT>" <EOL> while True : <EOL> try : <EOL> self . buffer . write_byte ( ch ) <EOL> return <EOL> except BufferOverflowError : <EOL> self . flush ( ) <EOL> def write_short ( self , i ) : <EOL> while True : <EOL> try : <EOL> self . buffer . write_short ( i ) <EOL> return <EOL> except BufferOverflowError : <EOL> self . flush ( ) <EOL> def flush ( self ) : <EOL> self . buffer . flip ( ) <EOL> while self . buffer . remaining : <EOL> if not self . stream . write ( self . buffer , Timeout . current ( ) ) : <EOL> raise EOFError ( "<STR_LIT>" ) <EOL> self . buffer . clear ( ) <EOL> class BufferedStream ( object ) : <EOL> def __init__ ( self , stream , buffer_size = <NUM_LIT> * <NUM_LIT:8> , read_buffer_size = <NUM_LIT:0> , write_buffer_size = <NUM_LIT:0> ) : <EOL> self . stream = stream <EOL> self . reader = BufferedReader ( stream , Buffer ( read_buffer_size or buffer_size ) ) <EOL> self . writer = BufferedWriter ( stream , Buffer ( write_buffer_size or buffer_size ) ) <EOL> def file ( self ) : <EOL> return CompatibleFile ( self . reader , self . writer ) <EOL> def close ( self ) : <EOL> self . stream . close ( ) <EOL> del self . stream <EOL> del self . reader <EOL> del self . writer <EOL> class CompatibleFile ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , reader = None , writer = None ) : <EOL> self . _reader = reader <EOL> self . _writer = writer <EOL> def readlines ( self ) : <EOL> reader = self . _reader <EOL> buffer = reader . buffer <EOL> while True : <EOL> try : <EOL> yield buffer . read_line ( True ) <EOL> except BufferUnderflowError : <EOL> try : <EOL> reader . _read_more ( ) <EOL> except EOFError : <EOL> buffer . flip ( ) <EOL> yield buffer . read_bytes ( - <NUM_LIT:1> ) <EOL> def readline ( self ) : <EOL> return self . readlines ( ) . next ( ) <EOL> def read ( self , n = - <NUM_LIT:1> ) : <EOL> reader = self . _reader <EOL> buffer = reader . buffer <EOL> s = [ ] <EOL> if n == - <NUM_LIT:1> : <EOL> while True : <EOL> s . append ( buffer . read_bytes ( - <NUM_LIT:1> ) ) <EOL> try : <EOL> reader . _read_more ( ) <EOL> except EOFError : <EOL> buffer . flip ( ) <EOL> break <EOL> else : <EOL> while n > <NUM_LIT:0> : <EOL> r = buffer . remaining <EOL> if r > <NUM_LIT:0> : <EOL> s . append ( buffer . read_bytes ( min ( n , r ) ) ) <EOL> n -= r <EOL> else : <EOL> try : <EOL> reader . _read_more ( ) <EOL> except EOFError : <EOL> buffer . flip ( ) <EOL> break <EOL> return '<STR_LIT>' . join ( s ) <EOL> def write ( self , s ) : <EOL> self . _writer . write_bytes ( s ) <EOL> def flush ( self ) : <EOL> self . _writer . flush ( ) </s>
