Example #1
0
        } // getBufferedReader

        public static void copy(NInputStream from_stream, NOutputStream to_stream) // throws IllegalStateException
        {
            try
            {
                byte[] buffer = new byte[1024 * 32];
                int    offset = 0;
                for (int value = from_stream.Read(buffer, offset++, 1); value != -1; value = from_stream.Read(buffer, offset++, 1))
                {
                    to_stream.Write(buffer, 0, value);
                }
            }
            catch (IOException e)
            {
                throw new InvalidOperationException(e.Message);
            }
        }
Example #2
0
        } // getWriter()

        //public static StreamWriter getWriter(NOutputStream file, String encoding) // throws IllegalStateException
        //{
        //    return getWriter(file, encoding, false);
        //} // getWriter()

        public static StreamReader getReader(NInputStream input_stream, String encoding_arg) // throws IllegalStateException
        {
            Encoding encoding_obj = null;

            try
            {
                if (encoding_arg == null || encoding_arg.ToLower().IndexOf("utf") != -1)
                {
                    byte[] bom = new byte[4];
                    int    unread;

                    // auto-detect byte-order-mark
                    //@SuppressWarnings("resource")
                    NInputStream pushbackInputStream = new NInputStream(input_stream.SafeFileHandle, FileAccess.Read, bom.Length);
                    int          n = pushbackInputStream.Read(bom, 0, bom.Length);

                    // Read ahead four bytes and check for BOM marks.
                    if ((bom[0] == (byte)0xEF) && (bom[1] == (byte)0xBB) && (bom[2] == (byte)0xBF))
                    {
                        encoding_arg = "UTF-8";
                        unread       = n - 3;
                        encoding_obj = Encoding.UTF8;
                    }
                    else if ((bom[0] == (byte)0xFE) && (bom[1] == (byte)0xFF))
                    {
                        encoding_arg = "UTF-16BE";
                        unread       = n - 2;
                    }
                    else if ((bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE))
                    {
                        encoding_arg = "UTF-16LE";
                        unread       = n - 2;
                    }
                    else if ((bom[0] == (byte)0x00) && (bom[1] == (byte)0x00) && (bom[2] == (byte)0xFE) &&
                             (bom[3] == (byte)0xFF))
                    {
                        encoding_arg = "UTF-32BE";
                        encoding_obj = Encoding.UTF32;
                        unread       = n - 4;
                    }
                    else if ((bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) && (bom[2] == (byte)0x00) &&
                             (bom[3] == (byte)0x00))
                    {
                        encoding_obj = Encoding.UTF32;
                        encoding_arg = "UTF-32LE";
                        unread       = n - 4;
                    }
                    else
                    {
                        unread = n;
                    }

                    if (unread > 0)
                    {
                        //pushbackInputStream.unread(bom, (n - unread), unread);
                    }
                    else if (unread < -1)
                    {
                        //pushbackInputStream.unread(bom, 0, 0);
                    }

                    input_stream = pushbackInputStream;
                }

                StreamReader inputStreamReader;
                if (encoding_arg == null)
                {
                    inputStreamReader = new StreamReader(input_stream);
                }
                else
                {
                    if (encoding_obj != null)
                    {
                        inputStreamReader = new StreamReader(input_stream, encoding_obj);
                    }
                    else
                    {
                        throw new IOException("getReader(): Encoding not available");
                    }
                }
                return(inputStreamReader);
            }
            catch (IOException e)
            {
                throw new InvalidOperationException(e.Message);
            }
        } // getReader()