|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.sciss.net.OSCPacketCodec
public class OSCPacketCodec
A packet codec defines how the translation between Java objects
and OSC atoms is accomplished. For example, by default, when
an OSC message is assembled for transmission, the encoder will
translate ajava.lang.Integer
argument into
a four byte integer with typetag 'i'
. Or when
a received message is being decoded, finding an atom typetagged
'f'
, the decoder will create a java.lang.Float
out of it.
This example sounds trivial, but the codec is also able to handle
type conversions. For instance, in the strict OSC 1.0 specification,
only 32bit numeric atoms are defined ('i'
and 'f'
).
A codec with mode MODE_STRICT_V1
will reject a
java.lang.Double
in the encoding process and not be
able to decode a typetag 'd'
. A codec with mode
MODE_MODEST
automatically breaks down everything the 32bit,
so a java.lang.Double
gets encoded as 32bit 'f'
and a received atom tagged 'd'
becomes a
java.lang.Float
. Other configurations exist.
Another important function of the codec is to specify the charset encoding
of strings, something that was overseen in the OSC 1.0 spec. By default,
UTF-8
is used so all special characters can be safely encoded.
Last but not least, using the putDecoder
and putEncoder
methods, the codec can be extended to support additional Java classes or
OSC typetags, without the need to subclass OSCPacketCodec
.
Nested Class Summary | |
---|---|
static class |
OSCPacketCodec.Atom
The Atom class represents a combination of
an encoder and decoder of a Java respectively OSC atom. |
Field Summary | |
---|---|
protected String |
charsetName
|
static int |
MODE_FAT_V1
Support mode: like MODE_STRICT_V1 , but with additional
64bit support, that is a mutual mapping between
'h' <--> java.lang.Long , and
'd' <--> java.lang.Double . |
static int |
MODE_GRACEFUL
Support mode: like MODE_MODEST , that is, it will
downgrade to 32bit in the encoding process, but decoding leaves
64bit values intact, so 'h' becomes java.lang.Long ,
and 'd' into java.lang.Double . |
static int |
MODE_MODEST
Support mode: like MODE_STRICT_V1 , but coder additionally
encodes java.lang.Long as a 'i' ,
java.lang.Double as a 'f' , and
de.sciss.net.OSCPacket as a blob 'b' . |
static int |
MODE_READ_DOUBLE
|
static int |
MODE_READ_DOUBLE_AS_FLOAT
|
static int |
MODE_READ_LONG
|
static int |
MODE_READ_LONG_AS_INTEGER
|
static int |
MODE_READ_SYMBOL_AS_STRING
|
static int |
MODE_STRICT_V1
Support mode: coder only accepts java.lang.Integer ,
java.lang.Float , java.lang.String ,
and byte[] . |
static int |
MODE_WRITE_DOUBLE
|
static int |
MODE_WRITE_DOUBLE_AS_FLOAT
|
static int |
MODE_WRITE_LONG
|
static int |
MODE_WRITE_LONG_AS_INTEGER
|
static int |
MODE_WRITE_PACKET_AS_BLOB
|
Constructor Summary | |
---|---|
OSCPacketCodec()
Creates a new codec with MODE_GRACEFUL and
UTF-8 encoding. |
|
OSCPacketCodec(int mode)
Creates a new codec with a given support mode and UTF-8 encoding. |
|
OSCPacketCodec(int mode,
String charset)
Creates a new codec with a given support mode and a given charset for string encoding. |
Method Summary | |
---|---|
OSCPacket |
decode(ByteBuffer b)
Creates a new packet decoded from the ByteBuffer. |
protected OSCBundle |
decodeBundle(ByteBuffer b)
|
protected OSCMessage |
decodeMessage(String command,
ByteBuffer b)
Creates a new message with arguments decoded from the ByteBuffer. |
void |
encode(OSCPacket p,
ByteBuffer b)
Encodes the contents of this packet into the provided ByteBuffer ,
beginning at the buffer's current position. |
protected void |
encodeBundle(OSCBundle bndl,
ByteBuffer b)
|
protected void |
encodeMessage(OSCMessage msg,
ByteBuffer b)
Encodes the message onto the given ByteBuffer ,
beginning at the buffer's current position. |
protected int |
getBundleSize(OSCBundle bndl)
|
static OSCPacketCodec |
getDefaultCodec()
Queries the standard codec which is used in all implicit client and server creations. |
protected int |
getMessageSize(OSCMessage msg)
Calculates the byte size of the encoded message |
int |
getSize(OSCPacket p)
Calculates and returns the packet's size in bytes |
static void |
padToAlign(ByteBuffer b)
Adds as many zero padding bytes as necessary to stop on a 4 byte alignment. |
void |
putDecoder(byte typeTag,
OSCPacketCodec.Atom a)
Registers an atomic decoder with the packet codec. |
void |
putEncoder(Class javaClass,
OSCPacketCodec.Atom a)
Registers an atomic encoder with the packet codec. |
static String |
readString(ByteBuffer b)
Reads a null terminated string from the current buffer position |
void |
setStringCharsetCodec(String charsetName)
Specifies the charset to use in string coding and decoding. |
void |
setSupportMode(int mode)
Adjusts the support mode for type tag handling. |
static void |
skipToAlign(ByteBuffer b)
Advances the current buffer position to an integer of four bytes. |
static void |
skipToValues(ByteBuffer b)
Advances in the buffer as long there are non-zero bytes, then advance to a four byte alignment. |
static void |
terminateAndPadToAlign(ByteBuffer b)
Adds as many zero padding bytes as necessary to stop on a 4 byte alignment. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int MODE_READ_DOUBLE
public static final int MODE_READ_DOUBLE_AS_FLOAT
public static final int MODE_READ_LONG
public static final int MODE_READ_LONG_AS_INTEGER
public static final int MODE_WRITE_DOUBLE
public static final int MODE_WRITE_DOUBLE_AS_FLOAT
public static final int MODE_WRITE_LONG
public static final int MODE_WRITE_LONG_AS_INTEGER
public static final int MODE_READ_SYMBOL_AS_STRING
public static final int MODE_WRITE_PACKET_AS_BLOB
public static final int MODE_STRICT_V1
java.lang.Integer
,
java.lang.Float
, java.lang.String
,
and byte[]
.
Decoder only accepts 'i'
, 'f'
,
's'
, and 'b'
. Note that byte[]
is used to represents blobs ('b'
).
public static final int MODE_MODEST
MODE_STRICT_V1
, but coder additionally
encodes java.lang.Long
as a 'i'
,
java.lang.Double
as a 'f'
, and
de.sciss.net.OSCPacket
as a blob 'b'
.
The decoder decodes 'h'
into java.lang.Integer
,
'd'
into java.lang.Float
, and
'S'
(Symbol) into java.lang.String
.
public static final int MODE_GRACEFUL
MODE_MODEST
, that is, it will
downgrade to 32bit in the encoding process, but decoding leaves
64bit values intact, so 'h'
becomes java.lang.Long
,
and 'd'
into java.lang.Double
.
public static final int MODE_FAT_V1
MODE_STRICT_V1
, but with additional
64bit support, that is a mutual mapping between
'h'
<--> java.lang.Long
, and
'd'
<--> java.lang.Double
.
Also, 'S'
(Symbol) is decoded into java.lang.String
,
and de.sciss.net.OSCPacket
is encoded as a blob 'b'
.
protected String charsetName
Constructor Detail |
---|
public OSCPacketCodec()
MODE_GRACEFUL
and
UTF-8
encoding. Note that since a codec
and be shared between OSCServer
or
OSCClient
instances, usually you will just
want to call getDefaultCodec
!
MODE_GRACEFUL
,
getDefaultCodec()
public OSCPacketCodec(int mode)
UTF-8
encoding.
mode
- the support mode flag field to usepublic OSCPacketCodec(int mode, String charset)
mode
- the support mode flag mask to usecharset
- the name of the charset to use for
string coding and decoding, like
"UTF-8"
,
"ISO-8859-1"
etc.Charset
Method Detail |
---|
public static OSCPacketCodec getDefaultCodec()
MODE_GRACEFUL
scheme and uses
UTF-8
string encoding.
Note that although it is not recommended, it is possible to modify the returned codec. That means that upon your application launch, you could query the default codec and switch its behaviour, e.g. change the string charset, so all successive operations with the default codec will be subject to those customizations.
MODE_GRACEFUL
public void setStringCharsetCodec(String charsetName)
charsetName
- the name of the charset, e.g.
"UTF-8"
,
"ISO-8859-1"
etc.Charset
public void putDecoder(byte typeTag, OSCPacketCodec.Atom a)
typeTag
- the typetag which is to be decoded with the
new Atom
. typeTag
must be in the ASCII value range 0 to 127.a
- the decoder to useOSCPacketCodec.Atom
public void putEncoder(Class javaClass, OSCPacketCodec.Atom a)
javaClass
- the class for which the encoder is responsiblea
- the encoder to useOSCPacketCodec.Atom
public void setSupportMode(int mode)
OSCPacketCodec
, but you can change it later
using this method.
mode
- the new mode to use. A flag field combination
of MODE_READ_DOUBLE
or
MODE_READ_DOUBLE_AS_FLOAT
etc.,
or a ready made combination such as
MODE_FAT_V1
.OSCPacketCodec( int )
public OSCPacket decode(ByteBuffer b) throws IOException
decode
of OSCBundle
is called (which may recursively decode
nested bundles), otherwise the one from
OSCMessage
.
b
- ByteBuffer
pointing right at
the beginning of the packet. the buffer's
limited should be set appropriately to
allow the complete packet to be read. when
the method returns, the buffer's position
is right after the end of the packet.
IOException
- in case some of the
reading or decoding procedures failed.
BufferUnderflowException
- in case of a parsing
error that causes the
method to read past the buffer limit
IllegalArgumentException
- occurs in some cases of buffer underflowpublic void encode(OSCPacket p, ByteBuffer b) throws IOException
ByteBuffer
,
beginning at the buffer's current position. To write the
encoded packet, you will typically call flip()
on the buffer, then write()
on the channel.
b
- ByteBuffer
pointing right at
the beginning of the osc packet.
buffer position will be right after the end
of the packet when the method returns.
IOException
- in case some of the
writing procedures failed.public int getSize(OSCPacket p) throws IOException
encode
method.
IOException
- if an error occurs during the calculationprotected int getBundleSize(OSCBundle bndl) throws IOException
IOException
protected int getMessageSize(OSCMessage msg) throws IOException
IOException
- if the message contains invalid argumentsprotected OSCBundle decodeBundle(ByteBuffer b) throws IOException
IOException
protected OSCMessage decodeMessage(String command, ByteBuffer b) throws IOException
decode
from the OSCPacket
superclass which will invoke this method of
it finds an OSC message.
b
- ByteBuffer pointing right at
the beginning of the type
declaration section of the
OSC message, i.e. the name
was skipped before.
IOException
- in case some of the
reading or decoding procedures failed.
BufferUnderflowException
- in case of a parsing
error that causes the
method to read past the buffer limit
IllegalArgumentException
- occurs in some cases of buffer underflowprotected void encodeBundle(OSCBundle bndl, ByteBuffer b) throws IOException
IOException
protected void encodeMessage(OSCMessage msg, ByteBuffer b) throws BufferOverflowException, IOException
ByteBuffer
,
beginning at the buffer's current position. To write the
encoded message, you will typically call flip()
on the buffer, then write()
on the channel.
b
- ByteBuffer
pointing right at
the beginning of the osc packet.
buffer position will be right after the end
of the message when the method returns.
IOException
- in case some of the
writing procedures failed
(buffer overflow, illegal arguments).
BufferOverflowException
public static String readString(ByteBuffer b)
b
- buffer to read from. position and limit must be
set appropriately. new position will be right after
the terminating zero byte when the method returns
BufferUnderflowException
- in case the string exceeds
the provided buffer limitpublic static void terminateAndPadToAlign(ByteBuffer b)
b
- the buffer to pad
BufferOverflowException
- in case the padding exceeds
the provided buffer limitpublic static void padToAlign(ByteBuffer b)
b
- the buffer to align
BufferOverflowException
- in case the padding exceeds
the provided buffer limitpublic static void skipToValues(ByteBuffer b) throws BufferUnderflowException
b
- the buffer to advance
BufferUnderflowException
- in case the reads exceed
the provided buffer limit
IllegalArgumentException
- in case the skipping exceeds
the provided buffer limitpublic static void skipToAlign(ByteBuffer b)
b
- the buffer to advance
IllegalArgumentException
- in case the skipping exceeds
the provided buffer limit
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |