Stream in; long timestamp = System.currentTimeInMillis(); int counter = 0; int INTERVAL = 1000; // one second int LIMIT = 1000; // bytes per INTERVAL ... /** * Read one byte with rate limiting */ @Override public int read() { if (counter > LIMIT) { long now = System.currentTimeInMillis(); if (timestamp + INTERVAL >= now) { Thread.sleep(timestamp + INTERVAL - now); } timestamp = now; counter = 0; } int res = in.read(); if (res >= 0) { counter++; } return res; }
public void clearerr() { stream.clearerr(); }
public void fclose() throws IOException, BadDescriptorException { stream.fclose(); }
ChannelDescriptor origDescriptor = origStream.getDescriptor(); boolean origIsSeekable = origDescriptor.isSeekable(); pos = origStream.fgetpos(); origFile.getPipeStream().fflush(); } else if (origFile.isWritable()) { origStream.fflush(); selfFile.getWriteStreamSafe().fflush(); ChannelDescriptor selfDescriptor = selfStream.getDescriptor(); boolean selfIsSeekable = selfDescriptor.isSeekable(); selfStream.clearerr(); selfStream.setModes(origStream.getModes()); } else { Stream pipeFile = selfFile.getPipeStream(); selfStream.fclose(); selfFile.setPipeStream(null); selfFile.getMainStreamSafe().setSync(selfFile.getMainStreamSafe().isSync()); if (selfFile.getPipeStream() != null && selfDescriptor.getFileno() != selfFile.getPipeStream().getDescriptor().getFileno()) { int fd = selfFile.getPipeStream().getDescriptor().getFileno(); selfFile.getPipeStream().fclose(); selfFile.setPipeStream(null);
originalFile.getPipeStream().fflush(); originalFile.getMainStreamSafe().lseek(0, Stream.SEEK_CUR); } else if (originalFile.isWritable()) { originalFile.getMainStreamSafe().fflush(); } else { originalFile.getMainStreamSafe().lseek(0, Stream.SEEK_CUR); modes = newIOOptions(runtime, ModeFlags.WRONLY); } else { modes = newIOOptions(runtime, originalFile.getMainStreamSafe().getModes()); ChannelDescriptor descriptor = originalFile.getMainStreamSafe().getDescriptor().dup(); newFile.getMainStream().setSync(originalFile.getMainStreamSafe().isSync()); if (originalFile.getMainStreamSafe().isBinmode()) newFile.getMainStream().setBinmode();
myOpenFile.setReadBuffered(); if (myOpenFile.getMainStreamSafe().feof()) { return runtime.getTrue(); if (myOpenFile.getMainStreamSafe().readDataBuffered()) { return runtime.getFalse(); waitReadable(myOpenFile.getMainStreamSafe()); myOpenFile.getMainStreamSafe().clearerr(); int c = myOpenFile.getMainStreamSafe().fgetc(); myOpenFile.getMainStreamSafe().ungetc(c); return runtime.getFalse(); myOpenFile.getMainStreamSafe().clearerr();
Stream ps = pipeStream; if (ps != null) { pipe = ps.getDescriptor(); if (isProcess && ps.getChannel().isOpen() || !isProcess) { ps.fflush(); ps.fclose(); if (ms != null) { ChannelDescriptor main = ms.getDescriptor(); runtime.removeFilenoIntMap(main.getFileno()); try { if (ms.getChannel().isOpen()) { if (pipe == null && isWriteBuffered()) { ms.fflush(); ms.fclose(); } catch (IOException ioe) { ms.fflush(); ms.fclose();
@JRubyMethod(name = "rewind") public RubyFixnum rewind(ThreadContext context) { OpenFile myOpenfile = getOpenFileChecked(); try { myOpenfile.getMainStreamSafe().lseek(0L, Stream.SEEK_SET); myOpenfile.getMainStreamSafe().clearerr(); // TODO: This is some goofy global file value from MRI..what to do? // if (io == current_file) { // gets_lineno -= fptr->lineno; // } } catch (BadDescriptorException e) { throw context.runtime.newErrnoEBADFError(); } catch (InvalidValueException e) { throw context.runtime.newErrnoEINVALError(); } catch (PipeException e) { throw context.runtime.newErrnoESPIPEError(); } catch (IOException e) { throw context.runtime.newIOErrorFromException(e); } // Must be back on first line on rewind. myOpenfile.setLineNumber(0); return RubyFixnum.zero(context.runtime); }
public boolean feof() throws IOException, BadDescriptorException { return stream.feof(); }
private static RubyIO getRubyIO(Ruby runtime, Writer writer) throws IOException, BadDescriptorException { PrintStream pstream = new PrintStream(new WriterOutputStream(writer), true); RubyIO io = new RubyIO(runtime, pstream, false); io.getOpenFile().getMainStreamSafe().setSync(true); io.getOpenFile().getMainStreamSafe().fflush(); return io; }
public void fflush(Stream stream) throws IOException, BadDescriptorException { while (true) { int n = stream.fflush(); if (n != -1) { break; } } mode &= ~WBUF; }
@JRubyMethod(name = "fsync") public RubyFixnum fsync(ThreadContext context) { Ruby runtime = context.runtime; try { OpenFile myOpenFile = getOpenFileChecked(); myOpenFile.checkWritable(runtime); Stream writeStream = myOpenFile.getWriteStream(); writeStream.fflush(); writeStream.sync(); } catch (InvalidValueException ex) { throw runtime.newErrnoEINVALError(); } catch (IOException e) { throw runtime.newIOErrorFromException(e); } catch (BadDescriptorException e) { throw runtime.newErrnoEBADFError(); } return RubyFixnum.zero(runtime); }
public int getcCommon() { try { OpenFile myOpenFile = getOpenFileChecked(); myOpenFile.checkReadable(getRuntime()); myOpenFile.setReadBuffered(); Stream stream = myOpenFile.getMainStreamSafe(); readCheck(stream); waitReadable(stream); stream.clearerr(); return myOpenFile.getMainStreamSafe().fgetc(); } catch (InvalidValueException ex) { throw getRuntime().newErrnoEINVALError(); } catch (BadDescriptorException e) { throw getRuntime().newErrnoEBADFError(); } catch (EOFException e) { throw getRuntime().newEOFError(); } catch (IOException e) { throw getRuntime().newIOErrorFromException(e); } }
/** * waits until input available or timed out and returns self, or nil when EOF reached. */ @JRubyMethod public static IRubyObject io_wait(ThreadContext context, IRubyObject obj) { RubyIO io = (RubyIO)obj; try { OpenFile openFile = io.getOpenFile(); if (openFile.getMainStreamSafe().feof()) { return context.runtime.getNil(); } openFile.getMainStreamSafe().waitUntilReady(); } catch (BadDescriptorException e) { throw context.runtime.newErrnoEBADFError(); } catch (Exception anyEx) { return context.runtime.getNil(); } return obj; } }
pos = myOpenFile.getMainStreamSafe().getDescriptor().lseek(offset, whence); myOpenFile.getMainStreamSafe().clearerr(); } catch (BadDescriptorException ex) { throw context.runtime.newErrnoEBADFError();
public long fgetpos() throws IOException, PipeException, BadDescriptorException, InvalidValueException { return stream.fgetpos(); }
public void freopen(Ruby runtime, String path, ModeFlags modes) throws DirectoryAsFileException, IOException, InvalidValueException, PipeException, BadDescriptorException { stream.freopen(runtime, path, modes); }