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; }
private static Stream maybeWrapWithLineEndingWrapper(Stream stream, ModeFlags modes) { if (modes.isText() || // FIXME: Remove this one textmode is part of transcoding. (Platform.IS_WINDOWS && stream.getDescriptor().getChannel() instanceof SeekableByteChannel && !modes.isBinary())) { return new CRLFStreamWrapper(stream); } return stream; }
public static Stream fdopen(Ruby runtime, ChannelDescriptor descriptor, ModeFlags modes) throws InvalidValueException { // check these modes before constructing, so we don't finalize the partially-initialized stream descriptor.checkNewModes(modes); return maybeWrapWithLineEndingWrapper(new ChannelStream(runtime, descriptor, modes, true), modes); }
private void finish(boolean close) throws BadDescriptorException, IOException { try { flushWrite(); if (DEBUG) LOG.info("Descriptor for fileno {} closed by stream", descriptor.getFileno()); } finally { buffer = EMPTY_BUFFER; // clear runtime so it doesn't get stuck in memory (JRUBY-2933) runtime = null; // finish descriptor descriptor.finish(close); } }
/** * Check whether a specified set of mode flags is a superset of this * descriptor's original set of mode flags. * * @param newModes The modes to confirm as superset * @throws org.jruby.util.io.InvalidValueException if the modes are not a superset */ public void checkNewModes(ModeFlags newModes) throws InvalidValueException { if (!newModes.isSubsetOf(originalModes)) { throw new InvalidValueException(); } }
public long seek(ThreadContext context, long offset, int whence) { boolean locked = lock(); try { flushBeforeSeek(context); return posix.lseek(fd, offset, whence); } finally { if (locked) unlock(); } }
/** * Check whether the isOpen returns true, raising a BadDescriptorException if * it returns false. * * @throws org.jruby.util.io.BadDescriptorException if isOpen returns false */ public void checkOpen() throws BadDescriptorException { if (!isOpen()) { throw new BadDescriptorException(); } }
public static void rb_maygvl_fd_fix_cloexec(Ruby runtime, int fd) { PosixShim shim = new PosixShim(runtime); OpenFile.fdFixCloexec(shim, fd); } }
/** * Construct a new ChannelDescriptor with the given channel, file number, * and file descriptor object. The channel will be kept open until all ChannelDescriptor * references to it have been closed. The channel's capabilities will be used * to determine the "original" set of mode flags. This version generates a * new fileno. * * @param channel The channel for the new descriptor */ public ChannelDescriptor(Channel channel) throws InvalidValueException { this(channel, ModeFlags.getModesFromChannel(channel), FilenoUtil.getDescriptorFromChannel(channel)); }
public static ExecArg execargNew(ThreadContext context, IRubyObject[] argv, boolean accept_shell) { ExecArg eargp = new ExecArg(); execargInit(context, argv, accept_shell, eargp); return eargp; }
private static Stream maybeWrapWithLineEndingWrapper(Stream stream, ModeFlags modes) { if (modes.isText() || // FIXME: Remove this one textmode is part of transcoding. (Platform.IS_WINDOWS && stream.getDescriptor().getChannel() instanceof SeekableByteChannel && !modes.isBinary())) { return new CRLFStreamWrapper(stream); } return stream; }
public static Stream fdopen(Ruby runtime, ChannelDescriptor descriptor, ModeFlags modes, boolean autoclose) throws InvalidValueException { // check these modes before constructing, so we don't finalize the partially-initialized stream descriptor.checkNewModes(modes); return maybeWrapWithLineEndingWrapper(new ChannelStream(runtime, descriptor, modes, autoclose), modes); }
private void finish(boolean close) throws BadDescriptorException, IOException { try { flushWrite(); if (DEBUG) LOG.info("Descriptor for fileno {} closed by stream", descriptor.getFileno()); } finally { buffer = EMPTY_BUFFER; // clear runtime so it doesn't get stuck in memory (JRUBY-2933) runtime = null; // finish descriptor descriptor.finish(close); } }
/** * Check whether a specified set of mode flags is a superset of this * descriptor's original set of mode flags. * * @param newModes The modes to confirm as superset * @throws org.jruby.util.io.InvalidValueException if the modes are not a superset */ public void checkNewModes(ModeFlags newModes) throws InvalidValueException { if (!newModes.isSubsetOf(originalModes)) { throw new InvalidValueException(); } }
public static void rb_maygvl_fd_fix_cloexec(Ruby runtime, int fd) { PosixShim shim = new PosixShim(runtime); OpenFile.fdFixCloexec(shim, fd); } }
private static Stream maybeWrapWithLineEndingWrapper(Stream stream, ModeFlags modes) { if (modes.isText() || // FIXME: Remove this one textmode is part of transcoding. (Platform.IS_WINDOWS && stream.getDescriptor().getChannel() instanceof FileChannel && !modes.isBinary())) { return new CRLFStreamWrapper(stream); } return stream; }
public static Stream fdopen(Ruby runtime, ChannelDescriptor descriptor, ModeFlags modes) throws InvalidValueException { // check these modes before constructing, so we don't finalize the partially-initialized stream descriptor.checkNewModes(modes); return maybeWrapWithLineEndingWrapper(new ChannelStream(runtime, descriptor, modes, true), modes); }
private static Stream maybeWrapWithLineEndingWrapper(Stream stream, ModeFlags modes) { if (modes.isText() || // FIXME: Remove this one textmode is part of transcoding. (Platform.IS_WINDOWS && stream.getDescriptor().getChannel() instanceof FileChannel && !modes.isBinary())) { return new CRLFStreamWrapper(stream); } return stream; }
public static Stream fdopen(Ruby runtime, ChannelDescriptor descriptor, ModeFlags modes, boolean autoclose) throws InvalidValueException { // check these modes before constructing, so we don't finalize the partially-initialized stream descriptor.checkNewModes(modes); return maybeWrapWithLineEndingWrapper(new ChannelStream(runtime, descriptor, modes, autoclose), modes); }