Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question on binary files

6 views
Skip to first unread message

Tom A.

unread,
Aug 26, 2008, 4:24:44 PM8/26/08
to
I'm an old Pascal programmer who is trying to revitalize my
programming skills. My first project is an old mainframe game. In
the program there was a rather large data structure which we packed
into (iirc) 10 60 bit words (with only 2 bits that I have found to
buffer it out. We cared about memory space back then :-)

In Pascal, it was easy to write this thing out to a file :
write(datastructure) and all 10 words (100 or so fields), were
written out.

In Java, I created a class to hold the record. I will not be packing
it -- if a field only goes from 0 to 7 I'm not going to be packaging
it with fields to fill up the whole byte.

But I was wondering how Java programs do this sort of thing. I'm sure
I _could_ pack up the structure, and I'm sure there are many ways to
do it, but I was wondering if there is a preferred way, or an easier
way.

(In the original, there were perhaps a hundred of these structures in
the file; but that was on a time sharing system. The world of PCs is
different, and each one, representing a player, could live in its own
file. But to learn random file access, I'll probably put them into a
single file at least to start).

Thanks for any suggestions & pointers. I've downloaded the Java
Turorials (http://java.sun.com/docs/books/tutorial/essential/
index.html) on files, but haven't read them. Its not so much the
programming I'm asking about, but the design possibilities.

Thanks again!

Tom A.
Who realizes that very few people would even be interested in a text
based dungeon game.

Message has been deleted

Eric Sosman

unread,
Aug 26, 2008, 4:55:50 PM8/26/08
to
Tom A. wrote:
> I'm an old Pascal programmer who is trying to revitalize my
> programming skills. My first project is an old mainframe game. In
> the program there was a rather large data structure which we packed
> into (iirc) 10 60 bit words (with only 2 bits that I have found to
> buffer it out. We cared about memory space back then :-)
>
> In Pascal, it was easy to write this thing out to a file :
> write(datastructure) and all 10 words (100 or so fields), were
> written out.
>
> In Java, I created a class to hold the record. I will not be packing
> it -- if a field only goes from 0 to 7 I'm not going to be packaging
> it with fields to fill up the whole byte.
>
> But I was wondering how Java programs do this sort of thing. I'm sure
> I _could_ pack up the structure, and I'm sure there are many ways to
> do it, but I was wondering if there is a preferred way, or an easier
> way.

There are several possibilities. Perhaps the most basic,
data-focused as opposed to object-focused approach would be to
use java.io.DataOutputStream and java.io.DataInputStream.

--
Eric....@sun.com

Daniele Futtorovic

unread,
Aug 26, 2008, 5:54:11 PM8/26/08
to

If you want to care about memory in this your new app, and consequently
write packed byte data, you could still go bit-shifting and create byte
arrays (or int arrays) out of your data and write them out. write(int)
and write(byte[], int, int) are still the most basic operations of the
java.io package (well, at least the output part).
If you want to go more elaborate, yet easier to write and to maintain,
go for Serialization and Data(Input|Output)Streams, as Stefan and Eric
suggested. What's a few kB on a desktop these days?


> Who realizes that very few people would even be interested in a text
> based dungeon game.

If it's good, I'll give it a try! Mass Effect sucks.


--
DF.

Tom A.

unread,
Aug 26, 2008, 6:24:11 PM8/26/08
to
On Aug 26, 4:54 pm, Daniele Futtorovic <da.futt.n...@laposte.invalid>
wrote:

Well, I had originally thought about writing some routines that would
use a byte variable to hold and array of 8 booleans, and routines to
set and query it, but I came to the same conclusion as you, whats a
few kB on a machine with 256 or 512 Mb main memory. (The CDC machine
I used only had an address space of under 200,000 60 bit words, so it
was important back then).

I'll read up on serialization and the data streams and see what I want
to do.

There's so much to learn....

Thanks! I'll probably be back with more questions and once I get it
working, I'll let you know.

Tom A.

Roedy Green

unread,
Aug 26, 2008, 10:11:46 PM8/26/08
to
On Tue, 26 Aug 2008 13:24:44 -0700 (PDT), "Tom A."
<meteoric...@yahoo.com> wrote, quoted or indirectly quoted
someone who said :

>In Java, I created a class to hold the record. I will not be packing
>it -- if a field only goes from 0 to 7 I'm not going to be packaging
>it with fields to fill up the whole byte.

You might use a DataOutputStream GZIPped, or a SerialOutputStream
GZIPped.

See http://mindprod.com/applet/fileio.html to generate you some sample
code.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Mark Space

unread,
Aug 27, 2008, 2:03:14 PM8/27/08
to
Tom A. wrote:

> In Pascal, it was easy to write this thing out to a file :
> write(datastructure) and all 10 words (100 or so fields), were
> written out.

Besides serialization and the obvious (write to an OutputStream), don't
overlook the use of java.nio.ByteBuffer. This class has methods for
writing all types of primitives as well as control over endianness.
Very handy when you're trying to read those old mainframe data files.

Serialization is tricky. You're implicitly making parts of a class
public that you might not realize. It's best to use classes that are
explicitly designed for serialization (like your struct was explicitly
designed to match the required file fields). You can just pop
"Serialization" onto any class but it's not a great idea to do so.

Highly recommended: check out the sections on serialization in
_Effective Java_ by Joshua Bloch.

> But I was wondering how Java programs do this sort of thing. I'm sure
> I _could_ pack up the structure, and I'm sure there are many ways to
> do it, but I was wondering if there is a preferred way, or an easier
> way.

However, bit fields are passe in Java. Use enumerations. If you have a
bit field like this:

int OPTION1 = 1;
int OPTION2 = 2;
int OPTION3 = 4;
int OPTION4 = 8;

where the options are designed to be | together, use an EnumSet instead.
It's nearly as efficient as OR'ing the ints together yourself, and
it's much better encapsulated.

And rather than use enumerations as indexes into arrays, use EnumMap
instead. Again, nearly as efficient as using arrays directly and much,
much better encapsulated.

Also in general, enumerations are well designed for serialization, and I
think the same applies to EnumSet and EnumMap. Just write 'em out to disk.

Again, see _Effective Java_ for more detail.


> Who realizes that very few people would even be interested in a text
> based dungeon game.

You'll need to add some art, but actually rogue-like games are still
popular. Final Fantasy Tactics kind of revived the genre, and I've
played imitators like Luminous Arc recently. Etrian Odyssey is on my
list (another rogue-like) as well.

0 new messages