IO builtin class. Wraps a BCL Stream object. Implementation of Ruby methods is in IoOps.cs in IronRuby.Libraries assembly.
Inheritance: IDisposable
Example #1
0
        public void ResetCurrentStream()
        {
            string file   = CurrentFileName.ToString();
            Stream stream = RubyFile.OpenFileStream(_context, file, _defaultMode);

            SingletonStream = new RubyIO(_context, stream, _defaultMode);
        }
Example #2
0
 public static void ForEach(BlockParam block, RubyClass /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ path,
                            [DefaultProtocol] MutableString separator, [DefaultProtocol, DefaultParameterValue(-1)] int limit)
 {
     using (RubyIO io = new RubyIO(self.Context, File.OpenRead(path.ConvertToString()), IOMode.ReadOnly)) {
         Each(self.Context, block, io, separator, limit);
     }
 }
Example #3
0
 public static RubyArray /*!*/ ReadLines(RubyClass /*!*/ self, [DefaultProtocol, NotNull] MutableString path, [DefaultProtocol] MutableString separator,
                                         [DefaultProtocol, DefaultParameterValue(-1)] int limit)
 {
     using (RubyIO io = new RubyIO(self.Context, File.OpenRead(path.ConvertToString()), IOMode.ReadOnly)) {
         return(ReadLines(self.Context, io, separator, limit));
     }
 }
Example #4
0
        public static object Load(RubyScope /*!*/ scope, RubyModule /*!*/ self, [NotNull] RubyIO /*!*/ source, [Optional] Proc proc)
        {
            BinaryReader  reader = source.GetBinaryReader();
            MarshalReader loader = new MarshalReader(reader, scope.RubyContext, scope.GlobalScope, proc);

            return(loader.Load());
        }
Example #5
0
        public static object Each(RubyContext /*!*/ context, BlockParam block, RubyIO /*!*/ self, [DefaultProtocol] MutableString separator)
        {
            self.RequireReadable();

            MutableString line;

            while ((line = self.ReadLineOrParagraph(separator)) != null)
            {
                if (block == null)
                {
                    throw RubyExceptions.NoBlockGiven();
                }

                line.IsTainted = true;
                context.InputProvider.LastInputLineNumber = ++self.LineNumber;

                object result;
                if (block.Yield(line, out result))
                {
                    return(result);
                }
            }

            return(self);
        }
Example #6
0
        public static bool IsAtty(RubyIO /*!*/ self)
        {
            ConsoleStreamType?console = self.ConsoleStreamType;

            if (console == null)
            {
                return(self.GetStream().BaseStream == Stream.Null);
            }

            int fd = GetStdHandleFd(console.Value);

            switch (Environment.OSVersion.Platform)
            {
            case PlatformID.Win32NT:
            case PlatformID.Win32S:
            case PlatformID.Win32Windows:
            case PlatformID.WinCE:
                IntPtr handle = GetStdHandle(fd);
                if (handle == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }

                return(GetFileType(handle) == FILE_TYPE_CHAR);

            default:
                return(isatty(fd) == 1);
            }
        }
Example #7
0
        private static void Seek(RubyIO /*!*/ self, long pos, int seekOrigin)
        {
            if (seekOrigin < 0 || seekOrigin > 2)
            {
                throw RubyExceptions.CreateArgumentError("Invalid argument");
            }

            if (self.IsConsoleDescriptor())
            {
                throw new Errno.BadFileDescriptorError();
            }

            // TODO: make sure we assert stream is not actually closed
            if (self.Closed)
            {
                throw RubyExceptions.CreateArgumentError("trying to seek on a non-existent stream?");
            }

            SeekOrigin origin = SeekOrigin.Current;

            if (seekOrigin == SEEK_SET)
            {
                origin = SeekOrigin.Begin;
            }
            else if (seekOrigin == SEEK_END)
            {
                origin = SeekOrigin.End;
            }

            self.Seek(pos, origin);
        }
Example #8
0
        private void Test_Read1() {
            string s;
            string crlf = "\r\n";
            var stream = new TestStream(false, B(
                "ab\r\r\n" +
                "e" + (s = "fgh" + crlf + "ijkl" + crlf + "mnop" + crlf + crlf + crlf + crlf + "qrst") +
                crlf + "!"
            ));
            int s_crlf_count = 6;

            var io = new RubyIO(Context, stream, "r");
            Assert(io.PeekByte() == (byte)'a');

            var buffer = MutableString.CreateBinary(B("foo:"));
            Assert(io.AppendBytes(buffer, 4) == 4);
            Assert(buffer.ToString() == "foo:ab\r\n");

            buffer = MutableString.CreateBinary();
            Assert(io.AppendBytes(buffer, 1) == 1);
            Assert(buffer.ToString() == "e");

            buffer = MutableString.CreateMutable("x:");
            int c = s.Length - s_crlf_count - 2;
            Assert(io.AppendBytes(buffer, c) == c);
            Assert(buffer.ToString() == "x:" + s.Replace(crlf, "\n").Substring(0, c));

            buffer = MutableString.CreateBinary();
            Assert(io.AppendBytes(buffer, 10) == 4);
            Assert(buffer.ToString() == "st\n!");

            buffer = MutableString.CreateBinary();
            Assert(io.AppendBytes(buffer, 10) == 0);
            Assert(buffer.ToString() == "");

        }
Example #9
0
 public static MutableString /*!*/ ReadFile(RubyClass /*!*/ self,
                                            [DefaultProtocol, NotNull] MutableString /*!*/ path)
 {
     using (RubyIO io = OpenFileForRead(self.Context, path)) {
         return(Read(io));
     }
 }
Example #10
0
        public static object Getc(RubyIO /*!*/ self)
        {
            self.AssertOpenedForReading();
            int c = self.ReadByteNormalizeEoln();

            return((c != -1) ? ScriptingRuntimeHelpers.Int32ToObject(c) : null);
        }
Example #11
0
 public static RubyArray /*!*/ ReadLines(RubyClass /*!*/ self,
                                         [DefaultProtocol, NotNull] MutableString path, [DefaultProtocol] MutableString separator)
 {
     using (RubyIO io = new RubyIO(self.Context, File.OpenRead(path.ConvertToString()), "r")) {
         return(ReadLines(self.Context, io, separator));
     }
 }
Example #12
0
        public static MutableString /*!*/ Read(RubyIO /*!*/ self)
        {
            var buffer = MutableString.CreateBinary();

            self.AppendBytes(buffer, Int32.MaxValue);
            return(buffer);
        }
Example #13
0
 public static RubyIO /*!*/ Reinitialize(RubyIO /*!*/ self, [DefaultProtocol] int descriptor, int mode)
 {
     self.Mode = (IOMode)mode;
     self.SetStream(GetDescriptorStream(self.Context, descriptor));
     self.SetFileDescriptor(descriptor);
     return(self);
 }
Example #14
0
        //write_nonblock

        #endregion

        #region read, read_nonblock

        private static byte[] ReadAllBytes(RubyIO /*!*/ io)
        {
            var fixedBuffer = new byte[io.Length];

            io.ReadBytes(fixedBuffer, 0, (int)io.Length);
            return(fixedBuffer);
        }
Example #15
0
 public static RubyIO /*!*/ Reopen(RubyIO /*!*/ self, [NotNull] RubyIO /*!*/ source)
 {
     self.Context.RedirectFileDescriptor(self.GetFileDescriptor(), source.GetFileDescriptor());
     self.SetStream(source.GetStream());
     self.Mode = source.Mode;
     return(self);
 }
Example #16
0
 public static RubyIO /*!*/ Binmode(RubyIO /*!*/ self)
 {
     if (!self.Closed && self.Position == 0)
     {
         self.PreserveEndOfLines = true;
     }
     return(self);
 }
Example #17
0
        public static object Dump(RubyModule /*!*/ self, object obj, [NotNull] RubyIO /*!*/ io, [Optional] int?limit)
        {
            BinaryWriter  writer = io.GetBinaryWriter();
            MarshalWriter dumper = new MarshalWriter(writer, self.Context, limit);

            dumper.Dump(obj);
            return(io);
        }
Example #18
0
        public static void CreateIO(RubyIO/*!*/ self,
            [DefaultProtocol]int fileDescriptor, [DefaultProtocol, NotNull, Optional]MutableString modeString) {

            // TODO:
            if (modeString != null) {
                self.ResetIOMode(modeString.ConvertToString());
            }
        }
Example #19
0
 public static void CloseWriter(RubyIO /*!*/ self)
 {
     if (self.Closed)
     {
         throw RubyExceptions.CreateIOError("closed stream");
     }
     self.CloseWriter();
 }
Example #20
0
        public static object /*!*/ Pos(RubyIO /*!*/ self)
        {
            if (self.Position <= Int32.MaxValue)
            {
                return((int)self.Position);
            }

            return((BigInteger)self.Position);
        }
Example #21
0
        public static RubyIO /*!*/ Reopen(RubyIO /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ path, int mode)
        {
            Stream newStream = RubyFile.OpenFileStream(self.Context, path.ConvertToString(), (IOMode)mode);

            self.Context.SetStream(self.GetFileDescriptor(), newStream);
            self.SetStream(newStream);
            self.Mode = (IOMode)mode;
            return(self);
        }
Example #22
0
        public static RubyIO/*!*/ InitializeCopy(RubyIO/*!*/ self, [NotNull]RubyIO/*!*/ source) {
            Stream stream = source.GetStream();
            int descriptor = self.Context.DuplicateFileDescriptor(source.GetFileDescriptor());

            self.SetStream(stream);
            self.SetFileDescriptor(descriptor);
            self.Mode = source.Mode;
            return self;
        }
Example #23
0
        public static void Pos(RubyIO /*!*/ self, [DefaultProtocol] int value)
        {
            if (self.IsConsoleDescriptor())
            {
                throw new Errno.BadFileDescriptorError();
            }

            self.Seek(value, SeekOrigin.Begin);
        }
Example #24
0
        private static RubyIO /*!*/ ToIo(RubyContext /*!*/ context, object obj)
        {
            RubyIO io = obj as RubyIO;

            if (io == null)
            {
                throw RubyExceptions.CreateTypeConversionError(RubyUtils.GetClassName(context, obj), "IO");
            }
            return(io);
        }
Example #25
0
        public static int Write(RubyIO /*!*/ self, [NotNull] MutableString /*!*/ val)
        {
            int bytesWritten = val.IsEmpty ? 0 : self.WriteBytes(val, 0, val.GetByteCount());

            if (self.AutoFlush)
            {
                self.Flush();
            }
            return(bytesWritten);
        }
Example #26
0
        public static RubyIO /*!*/ InitializeCopy(RubyIO /*!*/ self, [NotNull] RubyIO /*!*/ source)
        {
            Stream stream     = source.GetStream();
            int    descriptor = self.Context.DuplicateFileDescriptor(source.GetFileDescriptor());

            self.SetStream(stream);
            self.SetFileDescriptor(descriptor);
            self.Mode = source.Mode;
            return(self);
        }
Example #27
0
        public static int Write(RubyIO /*!*/ self, [NotNull] MutableString /*!*/ val)
        {
            self.AssertOpenedForWriting();
            int bytesWritten = self.Write(val);

            if (self.AutoFlush)
            {
                self.Flush();
            }
            return(bytesWritten);
        }
Example #28
0
        public static int Seek(RubyIO /*!*/ self, [NotNull] BigInteger /*!*/ pos, [DefaultProtocol, DefaultParameterValue(SEEK_SET)] int seekOrigin)
        {
            long longPos;

            if (!pos.AsInt64(out longPos))
            {
                throw RubyExceptions.CreateRangeError("bignum too big to convert into `long'");
            }
            Seek(self, longPos, seekOrigin);
            return(0);
        }
Example #29
0
        public static int WriteNoBlock(RubyIO /*!*/ self, [NotNull] MutableString /*!*/ val)
        {
            var stream = self.GetWritableStream();

            try {
                stream.WriteTimeout = 0;
            } catch (InvalidOperationException) {
                throw RubyExceptions.CreateEBADF();
            }
            return(Write(self, val));
        }
Example #30
0
        public static MutableString Gets(RubyScope /*!*/ scope, RubyIO /*!*/ self, [DefaultProtocol] MutableString separator)
        {
            MutableString result = self.ReadLineOrParagraph(separator);

            KernelOps.Taint(scope.RubyContext, result);

            scope.GetInnerMostClosureScope().LastInputLine = result;
            scope.RubyContext.InputProvider.IncrementLastInputLineNumber();

            return(result);
        }
Example #31
0
        public static RubyIO /*!*/ CreateIO(RubyClass /*!*/ self,
                                            [DefaultProtocol] int fileDescriptor, [DefaultProtocol, NotNull, Optional] MutableString modeString)
        {
            // TODO: a new RubyIO should be created here
            RubyIO result = self.Context.GetDescriptor(fileDescriptor);

            if (modeString != null)
            {
                result.ResetIOMode(modeString.ConvertToString());
            }
            return(result);
        }
Example #32
0
        public static int ReadChar(RubyIO /*!*/ self)
        {
            self.AssertOpenedForReading();
            int c = self.ReadByteNormalizeEoln();

            if (c == -1)
            {
                throw new EOFError("end of file reached");
            }

            return(c);
        }
Example #33
0
        public static MutableString /*!*/ ReadLine(RubyScope /*!*/ scope, RubyIO /*!*/ self, [DefaultProtocol] MutableString separator)
        {
            // no dynamic call, modifies $_ scope variable:
            MutableString result = Gets(scope, self);

            if (result == null)
            {
                throw new EOFError("end of file reached");
            }

            return(result);
        }
Example #34
0
        public Request(HttpRequestBase request) {
            ContractUtils.RequiresNotNull(request, "request");

            // http or https
            this.scheme = request.Url.Scheme;

            // move headers to a Ruby Hash
            this.headers = new Hash(RubyEngine.Context);
            foreach (string key in request.Headers.AllKeys) {
                string value = request.Headers.Get(key);
                if (string.IsNullOrEmpty(value)) continue;
                headers.Add(key, value);
            }

            this.queryString = request.QueryString.ToString();

            this.body = new RubyIO(RubyEngine.Context, request.InputStream, IOMode.ReadOnly);

            // Save the origional request incase it's needed.
            OrigionalRequest = request;
        }
Example #35
0
 public static int WriteNoBlock(ConversionStorage<MutableString>/*!*/ tosConversion, RubyIO/*!*/ self, object obj) {
     return Write(self, Protocols.ConvertToString(tosConversion, obj));
 }
Example #36
0
 public static int WriteNoBlock(RubyIO/*!*/ self, [NotNull]MutableString/*!*/ val) {
     var stream = self.GetWritableStream();
     int result = -1;
     self.NonBlockingOperation(() => result = Write(self, val), false);
     return result;
 }
Example #37
0
        public static RubyArray/*!*/ ReadLines(RubyContext/*!*/ context, RubyIO/*!*/ self, [DefaultProtocol]MutableString separator, [DefaultProtocol]int limit) {
            RubyArray result = new RubyArray();

            // no dynamic call, doesn't modify $_ scope variable:
            MutableString line;
            while ((line = self.ReadLineOrParagraph(separator, limit)) != null) {
                result.Add(line);
            }

            self.LineNumber += result.Count;
            context.InputProvider.LastInputLineNumber = self.LineNumber;
            return result;
        }
Example #38
0
 public static RubyArray/*!*/ ReadLines(RubyContext/*!*/ context, RubyIO/*!*/ self, DynamicNull separator) {
     return ReadLines(context, self, null, -1);
 }
Example #39
0
        public static MutableString/*!*/ ReadLine(RubyScope/*!*/ scope, RubyIO/*!*/ self, [DefaultProtocol]MutableString separator, [DefaultProtocol]int limit) {

            // no dynamic call, modifies $_ scope variable:
            MutableString result = Gets(scope, self, separator, limit);
            if (result == null) {
                throw new EOFError("end of file reached");
            }

            return result;
        }
Example #40
0
 public static MutableString/*!*/ ReadLine(RubyScope/*!*/ scope, RubyIO/*!*/ self, DynamicNull separator) {
     return ReadLine(scope, self, null, -1);
 }
Example #41
0
        public static int ReadChar(RubyIO/*!*/ self) {
            self.RequireReadable();
            int c = self.ReadByteNormalizeEoln();
            
            if (c == -1) {
                throw new EOFError("end of file reached");
            }

            return c;
        }
Example #42
0
        public static MutableString/*!*/ SystemRead(RubyIO/*!*/ self, [DefaultProtocol]int bytes, [DefaultProtocol, Optional]MutableString buffer) {
            var stream = self.GetReadableStream();
            if (stream.DataBuffered) {
                throw RubyExceptions.CreateIOError("sysread for buffered IO");
            }

            // We use Flush to simulate non-buffered IO. 
            // A better approach would be to create a parallel FileStream with 
            // System.IO.FileOptions.WriteThrough (which corresponds to FILE_FLAG_NO_BUFFERING), and also maybe 
            // System.IO.FileOptions.SequentialScan (FILE_FLAG_SEQUENTIAL_SCAN).
            // TODO: sysopen does that?
            stream.Flush();

            var result = Read(self, bytes, buffer);
            if (result == null) {
                throw new EOFError("end of file reached");
            }
            return result;
        }
Example #43
0
 public static MutableString/*!*/ Read(RubyIO/*!*/ self, DynamicNull bytes, [DefaultProtocol, Optional]MutableString buffer) {
     buffer = PrepareReadBuffer(self, buffer);
     self.AppendBytes(buffer, Int32.MaxValue);
     return buffer;
 }
Example #44
0
        private static MutableString PrepareReadBuffer(RubyIO/*!*/ io, MutableString buffer) {
            if (buffer == null) {
                buffer = MutableString.CreateBinary();
            } else {
                buffer.Clear();
            } 
#if TODO
            var internalEncoding = io.InternalEncoding ?? io.ExternalEncoding;

            if (buffer != null) {
                buffer.Clear();
                buffer.ForceEncoding(internalEncoding);
            } else if (io.ExternalEncoding == RubyEncoding.Binary && internalEncoding == RubyEncoding.Binary) {
                buffer = MutableString.CreateBinary();
            } else {
                buffer = MutableString.CreateMutable(internalEncoding);
            }
#endif            
            return buffer;
        }
Example #45
0
 public static MutableString/*!*/ Read(RubyIO/*!*/ self) {
     return Read(self, null, null);
 }
Example #46
0
 public static object SysSeek(RubyIO/*!*/ self, [DefaultProtocol]IntegerValue pos, [DefaultProtocol, DefaultParameterValue(SEEK_SET)]int seekOrigin) {
     self.Flush();
     self.Seek(pos.ToInt64(), RubyIO.ToSeekOrigin(seekOrigin));
     return pos.ToObject();
 }
Example #47
0
        public static MutableString Read(RubyIO/*!*/ self, [DefaultProtocol]int bytes, [DefaultProtocol, Optional]MutableString buffer) {
            self.RequireReadable();
            if (bytes < 0) {
                throw RubyExceptions.CreateArgumentError("negative length -1 given");
            }

            buffer = PrepareReadBuffer(self, buffer);
            int bytesRead = self.AppendBytes(buffer, bytes);
            return (bytesRead == 0 && bytes != 0) ? null : buffer;
        }
Example #48
0
        public static object/*!*/ Pos(RubyIO/*!*/ self) {
            if (self.Position <= Int32.MaxValue) {
                return (int)self.Position;
            }

            return (BigInteger)self.Position;
        }
Example #49
0
 public static MutableString ReadNoBlock(RubyIO/*!*/ self, [DefaultProtocol]int bytes, [DefaultProtocol, Optional]MutableString buffer) {
     var stream = self.GetReadableStream();
     MutableString result = null;
     self.NonBlockingOperation(() => result = Read(self, bytes, buffer), true);
     return result;
 }
Example #50
0
 public static void Pos(RubyIO/*!*/ self, [DefaultProtocol]IntegerValue pos) {
     self.Seek(pos.ToInt64(), SeekOrigin.Begin);
 }
Example #51
0
 public static MutableString/*!*/ ReadLine(RubyScope/*!*/ scope, RubyIO/*!*/ self) {
     return ReadLine(scope, self, scope.RubyContext.InputSeparator, -1);
 }
Example #52
0
 public static int GetLineNumber(RubyIO/*!*/ self) {
     self.RequireOpen();
     return self.LineNumber;
 }
Example #53
0
 public static MutableString/*!*/ ReadLine(RubyScope/*!*/ scope, RubyIO/*!*/ self, [DefaultProtocol, NotNull]Union<MutableString, int> separatorOrLimit) {
     if (separatorOrLimit.IsFixnum()) {
         return ReadLine(scope, self, scope.RubyContext.InputSeparator, separatorOrLimit.Fixnum());
     } else {
         return ReadLine(scope, self, separatorOrLimit.String(), -1);
     }
 }
Example #54
0
 public static void SetLineNumber(RubyContext/*!*/ context, RubyIO/*!*/ self, [DefaultProtocol]int value) {
     self.RequireOpen();
     self.LineNumber = value;
 }
Example #55
0
 public static RubyArray/*!*/ ReadLines(RubyContext/*!*/ context, RubyIO/*!*/ self) {
     return ReadLines(context, self, context.InputSeparator, -1);
 }
Example #56
0
 public static int Write(RubyIO/*!*/ self, [NotNull]MutableString/*!*/ val) {
     int bytesWritten = val.IsEmpty ? 0 : self.WriteBytes(val, 0, val.GetByteCount());
     if (self.AutoFlush) {
         self.Flush();
     }
     return bytesWritten;
 }
Example #57
0
 public static RubyArray/*!*/ ReadLines(RubyContext/*!*/ context, RubyIO/*!*/ self, [DefaultProtocol, NotNull]Union<MutableString, int> separatorOrLimit) {
     if (separatorOrLimit.IsFixnum()) {
         return ReadLines(context, self, context.InputSeparator, separatorOrLimit.Fixnum());
     } else {
         return ReadLines(context, self, separatorOrLimit.String(), -1);
     }
 }
Example #58
0
        public static int SysWrite(BinaryOpStorage/*!*/ writeStorage, ConversionStorage<MutableString>/*!*/ tosConversion,
            RubyContext/*!*/ context, RubyIO/*!*/ self, [NotNull]MutableString/*!*/ val) {

            RubyBufferedStream stream = self.GetWritableStream();
            if (stream.DataBuffered) {
                PrintOps.ReportWarning(writeStorage, tosConversion, MutableString.CreateAscii("syswrite for buffered IO"));
            }
            int bytes = Write(self, val);
            self.Flush();
            return bytes;
        }
Example #59
0
        public static RubyArray/*!*/ ReadLines(RubyClass/*!*/ self, [DefaultProtocol, NotNull]MutableString path, [DefaultProtocol]MutableString separator, 
            [DefaultProtocol, DefaultParameterValue(-1)]int limit) {

            using (RubyIO io = new RubyIO(self.Context, File.OpenRead(path.ConvertToString()), IOMode.ReadOnly)) {
                return ReadLines(self.Context, io, separator, limit);
            }
        }
Example #60
0
 public static int SysWrite(BinaryOpStorage/*!*/ writeStorage, ConversionStorage<MutableString>/*!*/ tosConversion,
     RubyContext/*!*/ context, RubyIO/*!*/ self, object obj) {
     return SysWrite(writeStorage, tosConversion, context, self, Protocols.ConvertToString(tosConversion, obj));
 }