public void testInt()
        {
            for (int i = 0; i < buffer.Length; i++)
              buffer[i] = 0;
            bf.Align=true;
            bf.TruncationThrow=false;

            int[] tint = new int[100];

            tint[0] = int.MinValue;
            tint[1] = int.MaxValue;
            tint[2] = 0;
            Random random = new Random();

            for (int i=0; i<tint.Length; i += 1) {
            tint[i] = (int)(int.MaxValue * (2 * (random.NextDouble() - 0.5)));
            }

            cnt = 0;
            offset = 0;
            // Write 100 numbers

               int colSize = 12;
            while (cnt < tint.Length) {

            offset = bf.format(tint[cnt], buffer, offset, colSize);
             cnt += 1;
            if (cnt % 8 == 0) {
            offset = bf.format("\n", buffer, offset, 1);
            }
            }

            // Now see if we can get them back
            bp.Offset=0;
             bp = new ByteParser(buffer);
            for (int i=0; i<tint.Length; i += 1) {
            Console.WriteLine("i= " + i);
            int chk = bp.GetInt(colSize);

            Assertion.AssertEquals("IntegersRA", chk, tint[i]);
            if ((i+1) % 8 == 0) {
            bp.Skip(1);
            }
            }

            // Now do it with left-aligned numbers.
            bf.Align=false;
            bp.FillFields=true;
            offset = 0;
            colSize = 12;
            cnt = 0;
            offset = 0;
            while (cnt < tint.Length) {
            int oldOffset = offset;
            offset = bf.format(tint[cnt], buffer, offset, colSize);
            int nb = colSize - (offset- oldOffset);
            if (nb > 0) {
            offset = bf.alignFill(buffer, offset, nb);
            }
            cnt += 1;
            if (cnt % 8 == 0) {
            offset = bf.format("\n", buffer, offset, 1);
            }
            }

            // Now see if we can get them back
            bp.Offset=0;
            for (int i=0; i<tint.Length; i += 1) {

            int chk = bp.GetInt(colSize);

            Assertion.AssertEquals("IntegersLA", chk, tint[i]);
            if ((i+1) % 8 == 0) {
            bp.Skip(1);
            }
            }

            offset = 0;
            colSize = 12;
            cnt = 0;
            offset = 0;
            while (cnt < tint.Length) {
            offset = bf.format(tint[cnt], buffer, offset, colSize);
            cnt += 1;
            if (cnt % 8 == 0) {
            offset = bf.format("\n", buffer, offset, 1);
            }
            }
            String myStr = null;
            sbyte[] sbytes = new sbyte[100000];
            Buffer.BlockCopy(buffer, 0, sbytes, 0, buffer.Length);
            unsafe
            {
            // Instruct the Garbage Collector not to move the memory
            fixed (sbyte* buffBytes = sbytes)
            {
            myStr = new String(buffBytes, 0, offset);

            }

            }
            Assertion.AssertEquals("No spaces", -1, myStr.IndexOf(" "));
            bf.Align=false;

            offset = 0;
            colSize = 12;
            cnt = 0;
            offset = 0;
            while (cnt < tint.Length) {
            offset = bf.format(tint[cnt], buffer, offset, colSize);
            offset = bf.format(" ", buffer, offset, 1);
            cnt   += 1;
            }
            String myStr2 = null;
            sbyte[] sbytes2 = new sbyte[100000];
            Buffer.BlockCopy(buffer, 0, sbytes2, 0, buffer.Length);
            unsafe
            {
            // Instruct the Garbage Collector not to move the memory
            fixed (sbyte* buffBytes2 = sbytes2)
            {
            myStr2 = new String(buffBytes2, 0, offset);

            }

            }
            String[] array = myStr2.Split(' ');

            Assertion.AssertEquals("Split size", 100, array.Length-1);

            for (int i=0; i<array.Length-1; i += 1) {
            Assertion.AssertEquals("Parse token",tint[i], int.Parse(array[i]));
            }

            bf.TruncationThrow=false;

            int val = 1;
            //Arrays.fill(buffer, (byte)' ');

            // array is used, values set, etc.
            //Array.Clear(buffer, ' ', buffer.Length-1);
            for(int i = 0; i < buffer.Length; i++)
             buffer[i] = (byte)' ';
            for (int i=0; i<10; i += 1) {
            offset = bf.format(val, buffer, 0, 6);
            String test = (val+"      ").Substring(0,6);
            sbyte[] sbytes1 = new sbyte[100000];
            Buffer.BlockCopy(buffer, 0, sbytes1, 0, buffer.Length);
            unsafe
            {
            // Instruct the Garbage Collector not to move the memory
            fixed (sbyte* buffBytes1 = sbytes1)
            {

                if (i < 6)
                {
                    Assertion.AssertEquals("TestTrunc" + i, test, new String(buffBytes1, 0, 6));
                }
                else
                {
                    Assertion.AssertEquals("TestTrunc" + i, "******", new String(buffBytes1, 0, 6));
                }
                val *= 10;
            }
            }
            }

            bf.TruncationThrow=true;
            val = 1;
            for (int i=0; i<10; i += 1) {
            bool thrown = false;
            try {
            offset = bf.format(val, buffer, 0, 6);
            } catch (TruncationException e) {
            thrown = true;
            }
            if (i < 6) {
            Assertion.AssertEquals("TestTruncThrow"+i, false, thrown);
            } else {
            Assertion.AssertEquals("TestTruncThrow"+i, true, thrown);
            }
            val *= 10;
            }
        }
Exemplo n.º 2
0
        /// <summary>Write the data to an output stream.</summary>
        public override void Write(ArrayDataIO str)
        {
            // If buffer is still around we can just reuse it,
            // since nothing we've done has invalidated it.
            if (buffer == null)
            {
                if (data == null)
                {
                    throw new FitsException("Attempt to write undefined ASCII Table");
                }

                buffer = new byte[nRows * rowLen];
                bp = new ByteParser(buffer);
                for(int i = 0; i < buffer.Length; i += 1)
                {
                    buffer[i] = (byte)' ';//SupportClass.Identity(' ');
                }

                ByteFormatter bf = new ByteFormatter();
                bf.TruncationThrow = false;
                bf.TruncateOnOverflow = true;

                for (int i = 0; i < nRows; i += 1)
                {
                    for (int j = 0; j < nFields; j += 1)
                    {
                        int offset = i * rowLen + offsets[j];
                        int len = lengths[j];

                        try
                        {
                            if (isNull_Renamed_Field != null && isNull_Renamed_Field[i * nFields + j])
                            {
                                if (nulls[j] == null)
                                {
                                    throw new FitsException("No null value set when needed");
                                }
                                bf.format(nulls[j], buffer, offset, len);
                            }
                            else
                            {
                                if (types[j] == typeof(String))
                                {
                                    String[] s = (String[]) data[j];
                                    bf.format(s[i], buffer, offset, len);
                                }
                                else if (types[j] == typeof(int))
                                {
                                    int[] ia = (int[]) data[j];
                                    bf.format(ia[i], buffer, offset, len);
                                }
                                else if (types[j] == typeof(float))
                                {
                                    float[] fa = (float[]) data[j];
                                    bf.format(fa[i], buffer, offset, len);
                                }
                                else if (types[j] == typeof(double))
                                {
                                    double[] da = (double[]) data[j];
                                    bf.format(da[i], buffer, offset, len);
                                }
                                else if (types[j] == typeof(long))
                                {
                                    long[] la = (long[]) data[j];
                                    bf.format(la[i], buffer, offset, len);
                                }
                            }
                        }
                        catch(TruncationException)
                        {
                            Console.Error.WriteLine("Ignoring truncation error:" + i + "," + j);
                        }
                    }
                }
            }

            // Now write the buffer.
            try
            {
                str.Write(buffer);
                byte[] padding = new byte[FitsUtil.Padding(buffer.Length)];
                for (int i = 0; i < padding.Length; i += 1)
                {
                    padding[i] = (byte) SupportClass.Identity(' ');
                }
                if (buffer.Length > 0)
                {
                    str.Write(padding);
                }
                str.Flush();
            }
            catch(IOException)
            {
                throw new FitsException("Error writing ASCII Table data");
            }
        }
Exemplo n.º 3
0
        /// <summary>Read some data into the buffer.</summary>
        private void GetBuffer(int size, long offset)
        {
            if(currInput == null)
            {
                throw new IOException("No stream open to read");
            }

            buffer = new byte[size];
            if(offset != 0)
            {
                //FitsUtil.Reposition(currInput, offset);
                currInput.Seek(offset, SeekOrigin.Begin);
            }
            currInput.Read(buffer);//SupportClass.ReadInput(currInput.BaseStream, ref buffer, 0, buffer.Length);

            // Suggested in .97 version
            bp = new ByteParser(buffer);
        }
Exemplo n.º 4
0
        public void testInt()
        {
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = 0;
            }
            bf.Align           = true;
            bf.TruncationThrow = false;

            int[] tint = new int[100];

            tint[0] = int.MinValue;
            tint[1] = int.MaxValue;
            tint[2] = 0;
            Random random = new Random();

            for (int i = 0; i < tint.Length; i += 1)
            {
                tint[i] = (int)(int.MaxValue * (2 * (random.NextDouble() - 0.5)));
            }

            cnt    = 0;
            offset = 0;
            // Write 100 numbers

            int colSize = 12;

            while (cnt < tint.Length)
            {
                offset = bf.format(tint[cnt], buffer, offset, colSize);
                cnt   += 1;
                if (cnt % 8 == 0)
                {
                    offset = bf.format("\n", buffer, offset, 1);
                }
            }

            // Now see if we can get them back
            bp.Offset = 0;
            bp        = new ByteParser(buffer);
            for (int i = 0; i < tint.Length; i += 1)
            {
                Console.WriteLine("i= " + i);
                int chk = bp.GetInt(colSize);

                Assertion.AssertEquals("IntegersRA", chk, tint[i]);
                if ((i + 1) % 8 == 0)
                {
                    bp.Skip(1);
                }
            }

            // Now do it with left-aligned numbers.
            bf.Align      = false;
            bp.FillFields = true;
            offset        = 0;
            colSize       = 12;
            cnt           = 0;
            offset        = 0;
            while (cnt < tint.Length)
            {
                int oldOffset = offset;
                offset = bf.format(tint[cnt], buffer, offset, colSize);
                int nb = colSize - (offset - oldOffset);
                if (nb > 0)
                {
                    offset = bf.alignFill(buffer, offset, nb);
                }
                cnt += 1;
                if (cnt % 8 == 0)
                {
                    offset = bf.format("\n", buffer, offset, 1);
                }
            }

            // Now see if we can get them back
            bp.Offset = 0;
            for (int i = 0; i < tint.Length; i += 1)
            {
                int chk = bp.GetInt(colSize);

                Assertion.AssertEquals("IntegersLA", chk, tint[i]);
                if ((i + 1) % 8 == 0)
                {
                    bp.Skip(1);
                }
            }

            offset  = 0;
            colSize = 12;
            cnt     = 0;
            offset  = 0;
            while (cnt < tint.Length)
            {
                offset = bf.format(tint[cnt], buffer, offset, colSize);
                cnt   += 1;
                if (cnt % 8 == 0)
                {
                    offset = bf.format("\n", buffer, offset, 1);
                }
            }
            String myStr = null;

            sbyte[] sbytes = new sbyte[100000];
            Buffer.BlockCopy(buffer, 0, sbytes, 0, buffer.Length);
            unsafe
            {
                // Instruct the Garbage Collector not to move the memory
                fixed(sbyte *buffBytes = sbytes)
                {
                    myStr = new String(buffBytes, 0, offset);
                }
            }
            Assertion.AssertEquals("No spaces", -1, myStr.IndexOf(" "));
            bf.Align = false;

            offset  = 0;
            colSize = 12;
            cnt     = 0;
            offset  = 0;
            while (cnt < tint.Length)
            {
                offset = bf.format(tint[cnt], buffer, offset, colSize);
                offset = bf.format(" ", buffer, offset, 1);
                cnt   += 1;
            }
            String myStr2 = null;

            sbyte[] sbytes2 = new sbyte[100000];
            Buffer.BlockCopy(buffer, 0, sbytes2, 0, buffer.Length);
            unsafe
            {
                // Instruct the Garbage Collector not to move the memory
                fixed(sbyte *buffBytes2 = sbytes2)
                {
                    myStr2 = new String(buffBytes2, 0, offset);
                }
            }
            String[] array = myStr2.Split(' ');

            Assertion.AssertEquals("Split size", 100, array.Length - 1);

            for (int i = 0; i < array.Length - 1; i += 1)
            {
                Assertion.AssertEquals("Parse token", tint[i], int.Parse(array[i]));
            }


            bf.TruncationThrow = false;

            int val = 1;

            //Arrays.fill(buffer, (byte)' ');

// array is used, values set, etc.
//Array.Clear(buffer, ' ', buffer.Length-1);
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (byte)' ';
            }
            for (int i = 0; i < 10; i += 1)
            {
                offset = bf.format(val, buffer, 0, 6);
                String  test    = (val + "      ").Substring(0, 6);
                sbyte[] sbytes1 = new sbyte[100000];
                Buffer.BlockCopy(buffer, 0, sbytes1, 0, buffer.Length);
                unsafe
                {
                    // Instruct the Garbage Collector not to move the memory
                    fixed(sbyte *buffBytes1 = sbytes1)
                    {
                        if (i < 6)
                        {
                            Assertion.AssertEquals("TestTrunc" + i, test, new String(buffBytes1, 0, 6));
                        }
                        else
                        {
                            Assertion.AssertEquals("TestTrunc" + i, "******", new String(buffBytes1, 0, 6));
                        }
                        val *= 10;
                    }
                }
            }

            bf.TruncationThrow = true;
            val = 1;
            for (int i = 0; i < 10; i += 1)
            {
                bool thrown = false;
                try {
                    offset = bf.format(val, buffer, 0, 6);
                } catch (TruncationException) {
                    thrown = true;
                }
                if (i < 6)
                {
                    Assertion.AssertEquals("TestTruncThrow" + i, false, thrown);
                }
                else
                {
                    Assertion.AssertEquals("TestTruncThrow" + i, true, thrown);
                }
                val *= 10;
            }
        }