Why doesn't DataInputStream implement Seekable ?



Hi,

I'm wondering if there was any particular design rationale behind GLib not
implementing the Seekable interface on the class DataInputStream?

If there is no particular reason, then I would like to file a feature request
to get Seekable into DataInputStream or one of it's base classes.

I'm parsing a big file and I would like to seek to one specific location,
read some data and then seek to a second location and read some data from
there as well. As a workaround I tried to seek on the underlying FileInputStream
that I passed as a parameter to the DataInputStream constructor but this
hack didn't work reliably.

I'm doing this in Vala and coming from Java and C# I really don't want to
be forced to choose between being able to seek and reading binary data
(e.g. methods like read_uint64() is very useful to me).

The code below is my reduced test case that currently fails
because my ugly "seek on the parent stream" is buggy. I'm posting it
just to illustrate the issue, I would really prefer if it would be possible
to implement Seekable on DataInputStream.



using GLib;

int main (string[] args)
{
        try {

                FileUtils.set_contents("test.txt", "hello");

                File file = File.new_for_path ("test.txt");
                FileInputStream file_input_stream = file.read (null);
                DataInputStream data_input_stream = new DataInputStream (file_input_stream);
                bool res;
                int buffer_size = 1;
                char[] buffer = new char[1];
                ulong bytes_actually_read = 0;

                assert (file_input_stream.can_seek ());

                file_input_stream.seek (0, SeekType.SET, null);
                data_input_stream = new DataInputStream (file_input_stream);
                res = data_input_stream.read_all (buffer, buffer_size, out bytes_actually_read, null);
                if (!res || bytes_actually_read != buffer_size)
                        assert (false);
                assert (bytes_actually_read == 1);
                assert (buffer[0] == 'h');

                file_input_stream.seek (1, SeekType.SET, null);
                data_input_stream = new DataInputStream (file_input_stream);
                res = data_input_stream.read_all (buffer, buffer_size, out bytes_actually_read, null);
                if (!res || bytes_actually_read != buffer_size)
                        assert (false);
                assert (bytes_actually_read == 1);
                assert (buffer[0] == 'e');

                file_input_stream.seek (0, SeekType.SET, null);
                data_input_stream = new DataInputStream (file_input_stream);
                res = data_input_stream.read_all (buffer, buffer_size, out bytes_actually_read, null);
                if (!res || bytes_actually_read != buffer_size)
                        assert (false);
                assert (bytes_actually_read == 1);
                assert (buffer[0] == 'h');


                stdout.printf ("passed\n");

        } catch (Error e) {
                stdout.printf ("%d : %s", e.code, e.message);
                assert (false);
        }

        return 0;
}




Martin


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]