Check an IO stream
348929
def check_stream(io_stream)
      yaml_data = io_stream.read
      error_array = []

      valid = check_data(yaml_data, error_array)
      errors[''] = error_array unless error_array.empty?

      valid
    end
511687
def output(stream):
    """"""
    while True:
        content = stream.read(1024)
        if len(content) == 0: break
        sys.stdout.write(content)
877558
public static void checkGetInputStreamOK(Resource resource) throws IOException {
	if (!resource.exists()) throw new IOException("file [" + resource.getPath() + "] does not exist");

	if (resource.isDirectory()) throw new IOException("can't read directory [" + resource.getPath() + "] as a file");

    }
942459
def stream_stdin_lines():
    """"""
    stdin = os.fdopen(sys.stdin.fileno(), 'r', 0)
    while True:
        line = stdin.readline()
        if line:
            yield line
        else:
            break
37269
def complete_io(self, iocb, msg):
        """"""
        if _debug: IOController._debug("complete_io %r %r", iocb, msg)

        # if it completed, leave it alone
        if iocb.ioState == COMPLETED:
            pass

        # if it already aborted, leave it alone
        elif iocb.ioState == ABORTED:
            pass

        else:
            # change the state
            iocb.ioState = COMPLETED
            iocb.ioResponse = msg

            # notify the client
            iocb.trigger()
869742
def _wrap_stream(stream):
        
        if hasattr(stream, "buffer"):
            return io.TextIOWrapper(stream.buffer, encoding="utf-8")
        elif hasattr(stream, "readable"):
            return io.TextIOWrapper(stream, encoding="utf-8")
        # Python 2.x stdin and stdout are just files
        else:
            return io.open(stream.fileno(), mode=stream.mode, encoding="utf-8")
915312
def get_io(io, offset=0)
      io = ::File.open(io, "rb") unless io.is_a?(IO) || io.is_a?(StringIO)
      io.seek(offset, ::IO::SEEK_SET)
      io
    end
728195
def exhaust_stream(f):
    """"""

    def wrapper(self, stream, *args, **kwargs):
        try:
            return f(self, stream, *args, **kwargs)
        finally:
            exhaust = getattr(stream, "exhaust", None)
            if exhaust is not None:
                exhaust()
            else:
                while 1:
                    chunk = stream.read(1024 * 64)
                    if not chunk:
                        break

    return update_wrapper(wrapper, f)
95793
async function readLogStream(file, instance) {
    return new Promise(resolve => {
        var stream = fs.createReadStream(file, { encoding: 'utf8', highWaterMark: instance.chunkSize });
        var hasStarted = false;
        // Split data into chunks so we dont stall the client
        stream.on('data', chunk => {
            if (!hasStarted) instance.emit("parsingStarted");
            hasStarted = true;
            var lines = chunk.toString().split("\n");
            // Pause stream until this chunk is completed to avoid spamming the thread
            stream.pause();
            async.each(lines, function (line, callback) {
                instance.registerMatch(line);
                callback();
            }, function (err) {
                setTimeout(() => {
                    stream.resume();
                }, instance.chunkInterval);
            });
        });
        stream.on('end', () => {
            instance.emit("parsingComplete");
            resolve();
            stream.close();
        });
    });
}
607781
def find_input(self, stream):
        

        for i, input_x in enumerate(self.inputs):
            if input_x[0].matches(stream):
                return i
1369850
def _handle_stdout_event(self, fd, events):
        """"""

        assert fd == self.fd_stdout

        if events & self.ioloop.READ:
            # got data ready to read
            data = ''

            # Now basically we have two cases: either the client supports
            # HTTP/1.1 in which case we can stream the answer in chunked mode
            # in HTTP/1.0 we need to send a content-length and thus buffer the complete output
            if self.request.supports_http_1_1():
                if not self.headers_sent:
                    self.sent_chunks = True
                    self.headers.update({'Date': get_date_header(), 'Transfer-Encoding': 'chunked'})
                    data = 'HTTP/1.1 200 OK\r\n' + '\r\n'.join([ k + ': ' + v for k, v in self.headers.items()]) + '\r\n\r\n'

                    if self.output_prelude:
                        data += hex(len(self.output_prelude))[2:] + "\r\n" # cut off 0x
                        data += self.output_prelude + "\r\n"

                    self.headers_sent = True

                payload = os.read(fd, 8192)
                if events & self.ioloop.ERROR: # there might be data remaining in the buffer if we got HUP, get it all
                    remainder = True
                    while remainder != '': # until EOF
                        remainder = os.read(fd, 8192)
                        payload += remainder

                data += hex(len(payload))[2:] + "\r\n" # cut off 0x
                data += payload + "\r\n"

            else:
                if not self.headers_sent:
                    # Use the over-eager blocking read that will get everything until we hit EOF
                    # this might actually be somewhat dangerous as noted in the subprocess documentation
                    # and lead to a deadlock. This is only a legacy mode for HTTP/1.0 clients anyway,
                    # so we might want to remove it entirely anyways
                    payload = self.process.stdout.read()
                    self.headers.update({'Date': get_date_header(), 'Content-Length': str(len(payload))})
                    data = 'HTTP/1.0 200 OK\r\n' + '\r\n'.join([ k + ': ' + v for k, v in self.headers.items()]) + '\r\n\r\n'
                    self.headers_sent = True
                    data += self.output_prelude + payload
                else:
                    # this is actually somewhat illegal as it messes with content-length but 
                    # it shouldn't happen anyways, as the read above should have read anything
                    # python docs say this can happen on ttys...
                    logger.error("This should not happen")
                    data = self.process.stdout.read()

            if len(data) == 8200:
                self.number_of_8k_chunks_sent += 1
            else:
                if self.number_of_8k_chunks_sent > 0:
                    logger.debug('Sent %d * 8192 bytes', self.number_of_8k_chunks_sent)
                    self.number_of_8k_chunks_sent = 0

                logger.debug('Sending stdout to client %d bytes: %r', len(data), data[:20])
            self.request.write(data)

        # now we can also have an error. This is because tornado maps HUP onto error
        # therefore, no elif here!
        if events & self.ioloop.ERROR:
            logger.debug('Error on stdout')
            # ensure file is closed
            if not self.process.stdout.closed:
                self.process.stdout.close()
            # remove handler
            self.ioloop.remove_handler(self.fd_stdout)
            # if all fds are closed, we can finish
            return self._graceful_finish()
491118
def check_args(args):
    """"""
    # Checking that only VCF can have a - (stdout) as output
    if args.output_format not in _streamable_format and args.output == "-":
        logger.error("{} format cannot be streamed to standard output"
                     "".format(args.output_format))
        sys.exit(1)

    # Checking the file extensions
    if args.output_format == "vcf" and args.output != "-":
        if not args.output.endswith(".vcf"):
            args.output += ".vcf"

    elif args.output_format == "plink":
        if args.output.endswith(".bed"):
            args.output = args.output[:-4]
894220
def listen(self, io_in, io_out, io_err):
        
        for line in io_in:
            if line.strip().lower() == 'exit':
                break

            try:
                y_pred = self.process_line(line)
            except Exception as e:
                io_out.write('[]\n')
                io_err.write(
                    "Error while processing input row: {}"
                    "{}: {}\n".format(line, type(e), e))
                io_err.flush()
            else:
                io_out.write(ujson.dumps(y_pred.tolist()))
                io_out.write('\n')
                io_out.flush()
791361
def check_output(args):
    """"""
    log.debug('run: %s', args)
    out = subprocess.check_output(args=args).decode('utf-8')
    log.debug('out: %r', out)
    return out
1086990
def _stream_out(self, outfile, append=False):
        '''
        
        '''
        if type(outfile) in (str, unicode):
            outfile = os.path.expanduser(os.path.expandvars(outfile))
            outfile = open(outfile, 'a' if append else 'w')
        self._run(outfile)
        self._pop.wait()
971677
def detect_stream_mode(stream):
    '''
        
    '''
    # If "Mode" is present, pull from that
    if hasattr(stream, 'mode'):
        if 'b' in stream.mode:
            return bytes
        elif 't' in stream.mode:
            return str

    # Read a zero-length string off the device 
    if hasattr(stream, 'read'):
        zeroStr = stream.read(0)
        if type(zeroStr) is str:
            return str
        return bytes
    elif hasattr(stream, 'recv'):
        zeroStr = stream.recv(0)
        if type(zeroStr) is str:
            return str
        return bytes

    # Cannot figure it out, assume bytes.
    return bytes
840422
def ainput(prompt='', *, streams=None, use_stderr=False, loop=None):
    """"""
    # Get standard streams
    if streams is None:
        streams = yield from get_standard_streams(
            use_stderr=use_stderr, loop=loop)
    reader, writer = streams
    # Write prompt
    writer.write(prompt.encode())
    yield from writer.drain()
    # Get data
    data = yield from reader.readline()
    # Decode data
    data = data.decode()
    # Return or raise EOF
    if not data.endswith('\n'):
        raise EOFError
    return data.rstrip('\n')