0

I'm trying to write a Java program to decode and encode Ogg streams. I've got a decoder working but I didn't like the fact that I had duplicate code so I started writing something like that:

Decoder oggDecoder = new Decoder(
    new StringDecoder( "Ogg" ),
    new IntDecoder( "something" )//, ...
);

I wrote encoders and decoders for some "types" and then use them to build the whole thing. But then I don't know how to store the result. I have 3 options I know: - keep the data in an array of bytes and provide a get( String name ) and set( String name, Object value ) methods that will work directly on the bytes. - use a dictionary. - use a class and use reflection to set the properties.

I'm not that much into performance and if it's slow I don't really care as long as it's fast enough to read music. Meaning that I know writing the functions myself would make it faster but I want to write just one function working for all the properties.

So what do you think would be the fastest way of doing this?

Another way to ask this question would be: Given a set of field names as an array of String, what is the most appropriate data structure to store the corresponding values that got decoded from a byte stream: - keep them as byte - store them in a dictionary - store them in a class using reflexion

Thanks in advance for your answer.

xavierm02
  • 8,457
  • 1
  • 20
  • 24

1 Answers1

2

KISS - just use a HashMap<String, byte[]>. No reflection needed.

Update I don't think I understood at first what you want, but now I think what you are looking for is a hetergeneous map structure.

Here's a question that might be of more use to you.

Community
  • 1
  • 1
wolfcastle
  • 5,850
  • 3
  • 33
  • 46
  • Well in that case, I might as well keep them in the original byte array... Because the whole point of using a hashmap / class was to have the data in the correct type, e.g. 4 bytes as an int, because after decoding it, I need a "simple" way to read the data from another part of the program (e.g. the one taking the data from the Ogg stream and reading Vorbis packets in it). – xavierm02 Aug 14 '12 at 15:23
  • Is there really no way to tell it "you're going to need exactly *** bytes: an int, a ... etc. ? Because the name are dynamic only because I don't want to write the same thing 20 times but in the end they won't change. – xavierm02 Aug 14 '12 at 15:48
  • After trying for a while, it seems like you're right. It gets overly complicated unless you use a HashMap. I'm going to go with the HashMap though. – xavierm02 Aug 14 '12 at 16:23