The Artima Developer Community
Sponsored Link

Java Buzz Forum
Bit Syntax for Java (I)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Wilfred Springer

Posts: 176
Nickname: springerw
Registered: Sep, 2006

Wilfred Springer is a Software Architect at Xebia
Bit Syntax for Java (I) Posted: Feb 24, 2009 1:23 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Wilfred Springer.
Original Post: Bit Syntax for Java (I)
Feed Title: Distributed Reflections of the Third Kind
Feed URL: http://blog.flotsam.nl/feeds/posts/default/-/Java
Feed Description: Anything coming to my mind having to do with Java
Latest Java Buzz Posts
Latest Java Buzz Posts by Wilfred Springer
Latest Posts From Distributed Reflections of the Third Kind

Advertisement
Erlang's bit syntax is pretty cool. Now, Preon is aiming to provide something with similar ease of use. Which one is easier? You decide.

One of the examples in the excellent Erlang book shows how to decode an MPEG header, using Erlang's bit syntax. This is what it looks like:

decode_header(<<2#11111111111:11,B:2,C:2,_D:1,E:4,F:2,G:1,Bits:9>>) ->
Vsn = case B of
0 -> {2,5};
1 -> exit(badVsn);
2 -> 2;
3 -> 1
end,
Layer = case C of
0 -> exit(badLayer);
1 -> 3;
2 -> 2;
3 -> 1
end,
%% Protection = D,
BitRate = bitrate(Vsn, Layer, E) * 1000,
SampleRate = samplerate(Vsn, F),
Padding = G,
FrameLength = framelength(Layer, BitRate, SampleRate, Padding),
if
FrameLength < 21 ->
exit(frameSize);
true ->
{ok, FrameLength, {Layer,BitRate,SampleRate,Vsn,Bits}}
end;
decode_header(_) ->
exit(badHeader).

This is the same code, but then in Java, using Preon:

public class MpegHeader {

@BoundNumber(size="11", match="0b11111111111")
private int frameSync;

@BoundNumber(size="2")
private int mpegAudioVersionId;

@BoundNumber(size="2")
private int layerDescription;

@Bound
private boolean crcProtected;

@BoundNumber(size="4")
private int bitRateIndex;

@BoundNumber(size="2")
private int sampleRateFrequencyIndex;

@Bound
private boolean padded;

@Bound
private boolean privateBit;

@BoundNumber(size="2")
private int channelMode;

@BoundNumber(size="2")
private int modeExtension;

@Bound
private boolean copyright;

@Bound
private boolean original;

@BoundNumber(size="2")
private int emphasis;

}

Codec codec = new Codecs.create(MpegHeader.class);
Codecs.decode(codec, buffer);

Now, there are obviously a lot of differences. The Erlang example also includes calls to functions defined elsewhere to determine the bitrate and frame lenght. The Preon example wouldn't have that. If you would need that, you could imagine implementing it as methods of the MpegHeader class.

Also note that the example does match on the first 11 bits, expecting them to be ones only. The notation @BoundNumber(size="11", match="0b11111111111") basically says that this field will be decoded from 11 input bits, as long as it matches the bit pattern "11111111111". Or to be more precise, as long as it matches the numeric value of 11 "1" bits, interpreted as an integer. If that criterion isn't match, the Codec will throw a DecodingException.

I will try to work the other examples into Java with Preon examples as well, but it's interesting to see how the two approaches compare. What would you prefer? And why?

Read: Bit Syntax for Java (I)

Topic: SDK1.1r1 JavaDocs FIXED! Previous Topic   Next Topic Topic: Asynchronous processing support in Servlet 3.0

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use