     bytes
 
 
+class ArrayCodec
+
+  # An array codec is a special case. It wraps a format codec, but prefixes
+  # it with a uint32 length value. It will first read the length, then read
+  # than many of the values from the byte array.
+
+  lengthCodec: new IntegerCodec(4, signed: false)
+
+  constructor: (@valueType) ->
+    @valueCodec = codecs[@valueType]
+    unless @valueCodec?
+      throw new Exception("Invalid array value type #{valueType}")
+
+  fromByteArray: (bytes, byteIndex, littleEndian) ->
+    # First read the number of elements in the array
+    length = @lengthCodec.fromByteArray(bytes, byteIndex, littleEndian)
+    byteIndex += @lengthCodec.length
+
+    # Then read all the elements
+    if length > 0
+      values = new Array(length)
+      for index in [0...length]
+        values[index] = @valueCodec.fromByteArray(bytes, byteIndex,
+                                                  littleEndian)
+        byteIndex += @valueCodec.length
+      @length = @lengthCodec.length + length * @valueCodec.length
+      values
+    else
+      @length = @lengthCodec.length
+      []
+
+  toByteArray: (values, littleEndian) ->
+    length = values?.length or 0
+    bytes = @lengthCodec.toByteArray(length, littleEndian)
+    if length > 0
+      for value in values
+        bytes = bytes.concat(@valueCodec.toByteArray(value, littleEndian))
+    bytes
+
+
 class StringCodec
 
   # The string codec is another special case. JavaScript strings are UTF-16,

  boolean: new BooleanCodec()
  float32: new FloatCodec(4, 23, Math.pow(2, -24) - Math.pow(2, -77))
  float64: new FloatCodec(8, 52, 0)
  int8: new IntegerCodec(1, true)
  int16: new IntegerCodec(2, true)
  int32: new IntegerCodec(4, true)
  string: new StringCodec()
  uint8: new IntegerCodec(1, false)
  uint16: new IntegerCodec(2, false)
  uint32: new IntegerCodec(4, false)


# Return true is the type is *not* one of the allowed types.
isInvalidType = (type) ->
  switch (type)
    when 'array', 'string', 'boolean', 'int8', 'int16', 'int32', 'uint8', \

Math.log2 ||= (value) -> Math.log(value) / Math.LN2


class BooleanCodec
