Fixed the primitive map marhsalling problem that was breaking the tests.

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@386703 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Hiram R. Chirino 2006-03-17 20:11:02 +00:00
parent 257f16a0bb
commit 9224eeb0a2
8 changed files with 988 additions and 842 deletions

View File

@ -35,7 +35,6 @@
<Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets"/>
<ItemGroup>
<Compile Include="src\main\csharp\ActiveMQ\BrokerException.cs"/>
<Compile Include="src\main\csharp\ActiveMQ\Commands\AbstractCommand.cs"/>
<Compile Include="src\main\csharp\ActiveMQ\Commands\ActiveMQBytesMessage.cs"/>
<Compile Include="src\main\csharp\ActiveMQ\Commands\ActiveMQDestination.cs"/>
<Compile Include="src\main\csharp\ActiveMQ\Commands\ActiveMQMapMessage.cs"/>
@ -210,6 +209,7 @@
<Compile Include="src\test\csharp\ActiveMQ\Commands\CommandTest.cs"/>
<Compile Include="src\test\csharp\ActiveMQ\OpenWire\BooleanStreamTest.cs"/>
<Compile Include="src\test\csharp\ActiveMQ\OpenWire\EndianTest.cs"/>
<Compile Include="src\test\csharp\ActiveMQ\OpenWire\PrimitiveMapTest.cs"/>
<Compile Include="src\test\csharp\ActiveMQ\TestMain.cs"/>
<Compile Include="src\test\csharp\NMS\AsyncConsumeTest.cs"/>
<Compile Include="src\test\csharp\NMS\BadConsumeTest.cs"/>

View File

@ -29,17 +29,6 @@ namespace ActiveMQ.OpenWire
/// </summary>
public abstract class BaseDataStreamMarshaller
{
public const byte NULL = 0;
public const byte BOOLEAN_TYPE = 1;
public const byte BYTE_TYPE = 2;
public const byte CHAR_TYPE = 3;
public const byte SHORT_TYPE = 4;
public const byte INTEGER_TYPE = 5;
public const byte LONG_TYPE = 6;
public const byte DOUBLE_TYPE = 7;
public const byte FLOAT_TYPE = 8;
public const byte STRING_TYPE = 9;
public const byte BYTE_ARRAY_TYPE = 10;
private static String[] HEX_TABLE = new String[]{
"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f",
@ -713,183 +702,6 @@ namespace ActiveMQ.OpenWire
}
/// <summary>
/// Marshals the primitive type map to a byte array
/// </summary>
public static byte[] MarshalPrimitiveMap(IDictionary map)
{
if (map == null)
{
return null;
}
else
{
MemoryStream memoryStream = new MemoryStream();
MarshalPrimitiveMap(map, new OpenWireBinaryWriter(memoryStream));
return memoryStream.GetBuffer();
}
}
public static void MarshalPrimitiveMap(IDictionary map, BinaryWriter dataOut)
{
if (map == null)
{
dataOut.Write((int)-1);
}
else
{
dataOut.Write(map.Count);
foreach (DictionaryEntry entry in map)
{
String name = (String) entry.Key;
dataOut.Write(name);
Object value = entry.Value;
MarshalPrimitive(dataOut, value);
}
}}
/// <summary>
/// Unmarshals the primitive type map from the given byte array
/// </summary>
public static IDictionary UnmarshalPrimitiveMap(byte[] data)
{
if (data == null)
{
return new Hashtable();
}
else
{
return UnmarshalPrimitiveMap(new OpenWireBinaryReader(new MemoryStream(data)));
}
}
public static IDictionary UnmarshalPrimitiveMap(BinaryReader dataIn)
{
int size = dataIn.ReadInt32();
if (size < 0)
{
return null;
}
else
{
IDictionary answer = new Hashtable(size);
for (int i=0; i < size; i++)
{
String name = dataIn.ReadString();
answer[name] = UnmarshalPrimitive(dataIn);
}
return answer;
}
}
public static void MarshalPrimitive(BinaryWriter dataOut, Object value)
{
if (value == null)
{
dataOut.Write(NULL);
}
else if (value is bool)
{
dataOut.Write(BOOLEAN_TYPE);
dataOut.Write((bool) value);
}
else if (value is byte)
{
dataOut.Write(BYTE_TYPE);
dataOut.Write(((Byte)value));
}
else if (value is char)
{
dataOut.Write(CHAR_TYPE);
dataOut.Write((char) value);
}
else if (value is short)
{
dataOut.Write(SHORT_TYPE);
dataOut.Write((short) value);
}
else if (value is int)
{
dataOut.Write(INTEGER_TYPE);
dataOut.Write((int) value);
}
else if (value is long)
{
dataOut.Write(LONG_TYPE);
dataOut.Write((long) value);
}
else if (value is float)
{
dataOut.Write(FLOAT_TYPE);
dataOut.Write((float) value);
}
else if (value is double)
{
dataOut.Write(DOUBLE_TYPE);
dataOut.Write((double) value);
}
else if (value is byte[])
{
byte[] data = (byte[]) value;
dataOut.Write(BYTE_ARRAY_TYPE);
dataOut.Write(data.Length);
dataOut.Write(data);
}
else if (value is string)
{
dataOut.Write(STRING_TYPE);
dataOut.Write((string) value);
}
else
{
throw new IOException("Object is not a primitive: " + value);
}
}
public static Object UnmarshalPrimitive(BinaryReader dataIn)
{
Object value=null;
switch (dataIn.ReadByte())
{
case BYTE_TYPE:
value = dataIn.ReadByte();
break;
case BOOLEAN_TYPE:
value = dataIn.ReadBoolean();
break;
case CHAR_TYPE:
value = dataIn.ReadChar();
break;
case SHORT_TYPE:
value = dataIn.ReadInt16();
break;
case INTEGER_TYPE:
value = dataIn.ReadInt32();
break;
case LONG_TYPE:
value = dataIn.ReadInt64();
break;
case FLOAT_TYPE:
value = dataIn.ReadSingle();
break;
case DOUBLE_TYPE:
value = dataIn.ReadDouble();
break;
case BYTE_ARRAY_TYPE:
int size = dataIn.ReadInt32();
byte[] data = new byte[size];
dataIn.Read(data, 0, size);
value = data;
break;
case STRING_TYPE:
value = dataIn.ReadString();
break;
}
return value;
}
/// <summary>
/// Converts the object to a String

View File

@ -1,99 +1,99 @@
/*
* Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.IO;
using System;
namespace ActiveMQ.OpenWire
{
/// <summary>
/// Support class that switches from one endian to the other.
/// </summary>
[CLSCompliant(false)]
public class EndianSupport
{
public static char SwitchEndian(char x)
{
return (char) (
(((char)( (byte)(x) )) << 8 ) |
(((char)( (byte)(x >> 8) )) )
);
}
public static short SwitchEndian(short x)
{
return (short) (
(((ushort)( (byte)(x) )) << 8 ) |
(((ushort)( (byte)(x >> 8) )) )
);
}
public static int SwitchEndian(int x)
{
return
(((int)( (byte)(x) )) << 24 ) |
(((int)( (byte)(x >> 8) )) << 16 ) |
(((int)( (byte)(x >> 16) )) << 8 ) |
(((int)( (byte)(x >> 24) )) );
}
public static long SwitchEndian(long x)
{
return
(((long)( (byte)(x ) )) << 56 ) |
(((long)( (byte)(x >> 8) )) << 48 ) |
(((long)( (byte)(x >> 16) )) << 40 ) |
(((long)( (byte)(x >> 24) )) << 32 ) |
(((long)( (byte)(x >> 32) )) << 24 ) |
(((long)( (byte)(x >> 40) )) << 16 ) |
(((long)( (byte)(x >> 48) )) << 8 ) |
(((long)( (byte)(x >> 56) )) );
}
public static ushort SwitchEndian(ushort x)
{
return (ushort) (
(((ushort)( (byte)(x) )) << 8 ) |
(((ushort)( (byte)(x >> 8) )) )
);
}
public static uint SwitchEndian(uint x)
{
return
(((uint)( (byte)(x ) )) << 24 ) |
(((uint)( (byte)(x >> 8) )) << 16 ) |
(((uint)( (byte)(x >> 16) )) << 8 ) |
(((uint)( (byte)(x >> 24) )) );
}
public static ulong SwitchEndian(ulong x)
{
return
(((ulong)( (byte)(x ) )) << 56 ) |
(((ulong)( (byte)(x >> 8) )) << 48 ) |
(((ulong)( (byte)(x >> 16) )) << 40 ) |
(((ulong)( (byte)(x >> 24) )) << 32 ) |
(((ulong)( (byte)(x >> 32) )) << 24 ) |
(((ulong)( (byte)(x >> 40) )) << 16 ) |
(((ulong)( (byte)(x >> 48) )) << 8 ) |
(((ulong)( (byte)(x >> 56) )) );
}
}
}
/*
* Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.IO;
using System;
namespace ActiveMQ.OpenWire
{
/// <summary>
/// Support class that switches from one endian to the other.
/// </summary>
[CLSCompliant(false)]
public class EndianSupport
{
public static char SwitchEndian(char x)
{
return (char) (
(((char)( (byte)(x) )) << 8 ) |
(((char)( (byte)(x >> 8) )) )
);
}
public static short SwitchEndian(short x)
{
return (short) (
(((ushort)( (byte)(x) )) << 8 ) |
(((ushort)( (byte)(x >> 8) )) )
);
}
public static int SwitchEndian(int x)
{
return
(((int)( (byte)(x) )) << 24 ) |
(((int)( (byte)(x >> 8) )) << 16 ) |
(((int)( (byte)(x >> 16) )) << 8 ) |
(((int)( (byte)(x >> 24) )) );
}
public static long SwitchEndian(long x)
{
return
(((long)( (byte)(x ) )) << 56 ) |
(((long)( (byte)(x >> 8) )) << 48 ) |
(((long)( (byte)(x >> 16) )) << 40 ) |
(((long)( (byte)(x >> 24) )) << 32 ) |
(((long)( (byte)(x >> 32) )) << 24 ) |
(((long)( (byte)(x >> 40) )) << 16 ) |
(((long)( (byte)(x >> 48) )) << 8 ) |
(((long)( (byte)(x >> 56) )) );
}
public static ushort SwitchEndian(ushort x)
{
return (ushort) (
(((ushort)( (byte)(x) )) << 8 ) |
(((ushort)( (byte)(x >> 8) )) )
);
}
public static uint SwitchEndian(uint x)
{
return
(((uint)( (byte)(x ) )) << 24 ) |
(((uint)( (byte)(x >> 8) )) << 16 ) |
(((uint)( (byte)(x >> 16) )) << 8 ) |
(((uint)( (byte)(x >> 24) )) );
}
public static ulong SwitchEndian(ulong x)
{
return
(((ulong)( (byte)(x ) )) << 56 ) |
(((ulong)( (byte)(x >> 8) )) << 48 ) |
(((ulong)( (byte)(x >> 16) )) << 40 ) |
(((ulong)( (byte)(x >> 24) )) << 32 ) |
(((ulong)( (byte)(x >> 32) )) << 24 ) |
(((ulong)( (byte)(x >> 40) )) << 16 ) |
(((ulong)( (byte)(x >> 48) )) << 8 ) |
(((ulong)( (byte)(x >> 56) )) );
}
}
}

View File

@ -1,219 +1,224 @@
/*
* Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.IO;
using System.Text;
namespace ActiveMQ.OpenWire
{
/// <summary>
/// A BinaryWriter that switches the endian orientation of the read opperations so that they
/// are compatible with marshalling used by OpenWire.
/// </summary>
[CLSCompliant(false)]
public class OpenWireBinaryReader : BinaryReader
{
public OpenWireBinaryReader(Stream input) : base(input)
{
}
/// <summary>
/// Method Read
/// </summary>
/// <returns>An int</returns>
/// <param name="buffer">A char[]</param>
/// <param name="index">An int</param>
/// <param name="count">An int</param>
public override int Read(char[] buffer, int index, int count)
{
int size = base.Read(buffer, index, count);
for( int i=0; i < size; i++ ) {
buffer[index+i] = EndianSupport.SwitchEndian(buffer[index+i]);
}
return size;
}
/// <summary>
/// Method ReadChars
/// </summary>
/// <returns>A char[]</returns>
/// <param name="count">An int</param>
public override char[] ReadChars(int count)
{
char[] rc = base.ReadChars(count);
if( rc!=null ) {
for( int i=0; i < rc.Length; i++ ) {
rc[i] = EndianSupport.SwitchEndian(rc[i]);
}
}
return rc;
}
/// <summary>
/// Method ReadInt16
/// </summary>
/// <returns>A short</returns>
public override short ReadInt16()
{
return EndianSupport.SwitchEndian(base.ReadInt16());
}
/// <summary>
/// Method ReadChar
/// </summary>
/// <returns>A char</returns>
public override char ReadChar()
{
return EndianSupport.SwitchEndian(base.ReadChar());
}
/// <summary>
/// Method ReadInt64
/// </summary>
/// <returns>A long</returns>
public override long ReadInt64()
{
return EndianSupport.SwitchEndian(base.ReadInt64());
}
/// <summary>
/// Method ReadUInt64
/// </summary>
/// <returns>An ulong</returns>
public override ulong ReadUInt64()
{
return EndianSupport.SwitchEndian(base.ReadUInt64());
}
/// <summary>
/// Method ReadUInt32
/// </summary>
/// <returns>An uint</returns>
public override uint ReadUInt32()
{
return EndianSupport.SwitchEndian(base.ReadUInt32());
}
/// <summary>
/// Method ReadUInt16
/// </summary>
/// <returns>An ushort</returns>
public override ushort ReadUInt16()
{
return EndianSupport.SwitchEndian(base.ReadUInt16());
}
/// <summary>
/// Method ReadInt32
/// </summary>
/// <returns>An int</returns>
public override int ReadInt32()
{
int x = base.ReadInt32();
int y = EndianSupport.SwitchEndian(x);
return y;
}
/// <summary>
/// Method ReadString
/// </summary>
/// <returns>A string</returns>
public override String ReadString()
{
short utflen = ReadInt16();
if (utflen > -1)
{
StringBuilder str = new StringBuilder(utflen);
byte[] bytearr = new byte[utflen];
int c, char2, char3;
int count = 0;
Read(bytearr, 0, utflen);
while (count < utflen)
{
c = bytearr[count] & 0xff;
switch (c >> 4)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
/* 0xxxxxxx */
count++;
str.Append((char) c);
break;
case 12:
case 13:
/* 110x xxxx 10xx xxxx */
count += 2;
if (count > utflen)
{
throw CreateDataFormatException();
}
char2 = bytearr[count - 1];
if ((char2 & 0xC0) != 0x80)
{
throw CreateDataFormatException();
}
str.Append((char) (((c & 0x1F) << 6) | (char2 & 0x3F)));
break;
case 14:
/* 1110 xxxx 10xx xxxx 10xx xxxx */
count += 3;
if (count > utflen)
{
throw CreateDataFormatException();
}
char2 = bytearr[count - 2];
char3 = bytearr[count - 1];
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
{
throw CreateDataFormatException();
}
str.Append((char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)));
break;
default :
/* 10xx xxxx, 1111 xxxx */
throw CreateDataFormatException();
}
}
// The number of chars produced may be less than utflen
return str.ToString();
}
else
{
return null;
}
}
private static Exception CreateDataFormatException()
{
// TODO: implement a better exception
return new IOException("Data format error!");
}
}
}
/*
* Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.IO;
using System.Text;
namespace ActiveMQ.OpenWire
{
/// <summary>
/// A BinaryWriter that switches the endian orientation of the read opperations so that they
/// are compatible with marshalling used by OpenWire.
/// </summary>
[CLSCompliant(false)]
public class OpenWireBinaryReader : BinaryReader
{
public OpenWireBinaryReader(Stream input) : base(input)
{
}
/// <summary>
/// Method Read
/// </summary>
/// <returns>An int</returns>
/// <param name="buffer">A char[]</param>
/// <param name="index">An int</param>
/// <param name="count">An int</param>
public override int Read(char[] buffer, int index, int count)
{
int size = base.Read(buffer, index, count);
for( int i=0; i < size; i++ ) {
buffer[index+i] = EndianSupport.SwitchEndian(buffer[index+i]);
}
return size;
}
/// <summary>
/// Method ReadChars
/// </summary>
/// <returns>A char[]</returns>
/// <param name="count">An int</param>
public override char[] ReadChars(int count)
{
char[] rc = base.ReadChars(count);
if( rc!=null ) {
for( int i=0; i < rc.Length; i++ ) {
rc[i] = EndianSupport.SwitchEndian(rc[i]);
}
}
return rc;
}
/// <summary>
/// Method ReadInt16
/// </summary>
/// <returns>A short</returns>
public override short ReadInt16()
{
return EndianSupport.SwitchEndian(base.ReadInt16());
}
/// <summary>
/// Method ReadChar
/// </summary>
/// <returns>A char</returns>
public override char ReadChar()
{
return (char) (
(((char)( (byte)(base.ReadByte()) )) << 8 ) |
(((char)( (byte)(base.ReadByte()) )) )
);
// return EndianSupport.SwitchEndian(base.ReadChar());
}
/// <summary>
/// Method ReadInt64
/// </summary>
/// <returns>A long</returns>
public override long ReadInt64()
{
return EndianSupport.SwitchEndian(base.ReadInt64());
}
/// <summary>
/// Method ReadUInt64
/// </summary>
/// <returns>An ulong</returns>
public override ulong ReadUInt64()
{
return EndianSupport.SwitchEndian(base.ReadUInt64());
}
/// <summary>
/// Method ReadUInt32
/// </summary>
/// <returns>An uint</returns>
public override uint ReadUInt32()
{
return EndianSupport.SwitchEndian(base.ReadUInt32());
}
/// <summary>
/// Method ReadUInt16
/// </summary>
/// <returns>An ushort</returns>
public override ushort ReadUInt16()
{
return EndianSupport.SwitchEndian(base.ReadUInt16());
}
/// <summary>
/// Method ReadInt32
/// </summary>
/// <returns>An int</returns>
public override int ReadInt32()
{
int x = base.ReadInt32();
int y = EndianSupport.SwitchEndian(x);
return y;
}
/// <summary>
/// Method ReadString
/// </summary>
/// <returns>A string</returns>
public override String ReadString()
{
short utflen = ReadInt16();
if (utflen > -1)
{
StringBuilder str = new StringBuilder(utflen);
byte[] bytearr = new byte[utflen];
int c, char2, char3;
int count = 0;
Read(bytearr, 0, utflen);
while (count < utflen)
{
c = bytearr[count] & 0xff;
switch (c >> 4)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
/* 0xxxxxxx */
count++;
str.Append((char) c);
break;
case 12:
case 13:
/* 110x xxxx 10xx xxxx */
count += 2;
if (count > utflen)
{
throw CreateDataFormatException();
}
char2 = bytearr[count - 1];
if ((char2 & 0xC0) != 0x80)
{
throw CreateDataFormatException();
}
str.Append((char) (((c & 0x1F) << 6) | (char2 & 0x3F)));
break;
case 14:
/* 1110 xxxx 10xx xxxx 10xx xxxx */
count += 3;
if (count > utflen)
{
throw CreateDataFormatException();
}
char2 = bytearr[count - 2];
char3 = bytearr[count - 1];
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
{
throw CreateDataFormatException();
}
str.Append((char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)));
break;
default :
/* 10xx xxxx, 1111 xxxx */
throw CreateDataFormatException();
}
}
// The number of chars produced may be less than utflen
return str.ToString();
}
else
{
return null;
}
}
private static Exception CreateDataFormatException()
{
// TODO: implement a better exception
return new IOException("Data format error!");
}
}
}

View File

@ -1,195 +1,196 @@
/*
* Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using ActiveMQ.Commands;
using System;
using System.Collections;
using System.IO;
using System.Text;
namespace ActiveMQ.OpenWire
{
/// <summary>
/// A BinaryWriter that switches the endian orientation of the write opperations so that they
/// are compatible with marshalling used by OpenWire.
/// </summary>
[CLSCompliant(false)]
public class OpenWireBinaryWriter : BinaryWriter
{
public OpenWireBinaryWriter(Stream output) : base(output)
{
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="value">A long</param>
public override void Write(long value)
{
base.Write(EndianSupport.SwitchEndian(value));
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="value">An ushort</param>
public override void Write(ushort value)
{
base.Write(EndianSupport.SwitchEndian(value));
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="value">An int</param>
public override void Write(int value)
{
int x = EndianSupport.SwitchEndian(value);
base.Write(x);
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="chars">A char[]</param>
/// <param name="index">An int</param>
/// <param name="count">An int</param>
public override void Write(char[] chars, int index, int count)
{
char[] t = new char[count];
for( int i=0; i < count; i++ ) {
t[index+i] = EndianSupport.SwitchEndian(t[index+i]);
}
base.Write(t);
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="chars">A char[]</param>
public override void Write(char[] chars)
{
Write(chars, 0, chars.Length);
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="value">An uint</param>
public override void Write(uint value)
{
base.Write(EndianSupport.SwitchEndian(value));
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="ch">A char</param>
public override void Write(char ch)
{
base.Write(EndianSupport.SwitchEndian(ch));
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="value">An ulong</param>
public override void Write(ulong value)
{
base.Write(EndianSupport.SwitchEndian(value));
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="value">A short</param>
public override void Write(short value)
{
base.Write(EndianSupport.SwitchEndian(value));
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="text">A string</param>
public override void Write(String text)
{
if (text != null)
{
if( text.Length > short.MaxValue ) {
throw new IOException("Cannot marshall string longer than: "+short.MaxValue+" characters, supplied steing was: "+text.Length+" characters");
}
short strlen = (short)text.Length;
short utflen = 0;
int c, count = 0;
char[] charr = text.ToCharArray();
for (int i = 0; i < strlen; i++)
{
c = charr[i];
if ((c >= 0x0001) && (c <= 0x007F))
{
utflen++;
}
else if (c > 0x07FF)
{
utflen += 3;
}
else
{
utflen += 2;
}
}
Write(utflen);
byte[] bytearr = new byte[utflen];
for (int i = 0; i < strlen; i++)
{
c = charr[i];
if ((c >= 0x0001) && (c <= 0x007F))
{
bytearr[count++] = (byte) c;
}
else if (c > 0x07FF)
{
bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
}
else
{
bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
}
}
Write(bytearr);
}
else
{
Write((short)-1);
}
}
}
}
/*
* Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using ActiveMQ.Commands;
using System;
using System.Collections;
using System.IO;
using System.Text;
namespace ActiveMQ.OpenWire
{
/// <summary>
/// A BinaryWriter that switches the endian orientation of the write opperations so that they
/// are compatible with marshalling used by OpenWire.
/// </summary>
[CLSCompliant(false)]
public class OpenWireBinaryWriter : BinaryWriter
{
public OpenWireBinaryWriter(Stream output) : base(output)
{
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="value">A long</param>
public override void Write(long value)
{
base.Write(EndianSupport.SwitchEndian(value));
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="value">An ushort</param>
public override void Write(ushort value)
{
base.Write(EndianSupport.SwitchEndian(value));
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="value">An int</param>
public override void Write(int value)
{
int x = EndianSupport.SwitchEndian(value);
base.Write(x);
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="chars">A char[]</param>
/// <param name="index">An int</param>
/// <param name="count">An int</param>
public override void Write(char[] chars, int index, int count)
{
char[] t = new char[count];
for( int i=0; i < count; i++ ) {
t[index+i] = EndianSupport.SwitchEndian(t[index+i]);
}
base.Write(t);
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="chars">A char[]</param>
public override void Write(char[] chars)
{
Write(chars, 0, chars.Length);
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="value">An uint</param>
public override void Write(uint value)
{
base.Write(EndianSupport.SwitchEndian(value));
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="ch">A char</param>
public override void Write(char ch)
{
base.Write( (byte)( ( ch>>8 ) & 0xFF ) );
base.Write( (byte)( ch & 0xFF ) );
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="value">An ulong</param>
public override void Write(ulong value)
{
base.Write(EndianSupport.SwitchEndian(value));
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="value">A short</param>
public override void Write(short value)
{
base.Write(EndianSupport.SwitchEndian(value));
}
/// <summary>
/// Method Write
/// </summary>
/// <param name="text">A string</param>
public override void Write(String text)
{
if (text != null)
{
if( text.Length > short.MaxValue ) {
throw new IOException("Cannot marshall string longer than: "+short.MaxValue+" characters, supplied steing was: "+text.Length+" characters");
}
short strlen = (short)text.Length;
short utflen = 0;
int c, count = 0;
char[] charr = text.ToCharArray();
for (int i = 0; i < strlen; i++)
{
c = charr[i];
if ((c >= 0x0001) && (c <= 0x007F))
{
utflen++;
}
else if (c > 0x07FF)
{
utflen += 3;
}
else
{
utflen += 2;
}
}
Write(utflen);
byte[] bytearr = new byte[utflen];
for (int i = 0; i < strlen; i++)
{
c = charr[i];
if ((c >= 0x0001) && (c <= 0x007F))
{
bytearr[count++] = (byte) c;
}
else if (c > 0x07FF)
{
bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
}
else
{
bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
}
}
Write(bytearr);
}
else
{
Write((short)-1);
}
}
}
}

View File

@ -17,6 +17,7 @@
using NMS;
using System;
using System.Collections;
using System.IO;
namespace ActiveMQ.OpenWire
{
@ -25,26 +26,20 @@ namespace ActiveMQ.OpenWire
/// </summary>
public class PrimitiveMap : IPrimitiveMap
{
public const byte NULL = 0;
public const byte BOOLEAN_TYPE = 1;
public const byte BYTE_TYPE = 2;
public const byte CHAR_TYPE = 3;
public const byte SHORT_TYPE = 4;
public const byte INTEGER_TYPE = 5;
public const byte LONG_TYPE = 6;
public const byte DOUBLE_TYPE = 7;
public const byte FLOAT_TYPE = 8;
public const byte STRING_TYPE = 9;
public const byte BYTE_ARRAY_TYPE = 10;
private IDictionary dictionary = new Hashtable();
/// <summary>
/// Unmarshalls the map from the given data or if the data is null just
/// return an empty map
/// </summary>
public static PrimitiveMap Unmarshal(byte[] data)
{
PrimitiveMap answer = new PrimitiveMap();
answer.dictionary = BaseDataStreamMarshaller.UnmarshalPrimitiveMap(data);
return answer;
}
public byte[] Marshal()
{
return BaseDataStreamMarshaller.MarshalPrimitiveMap(dictionary);
}
public void Clear()
{
dictionary.Clear();
@ -234,6 +229,227 @@ namespace ActiveMQ.OpenWire
}
}
}
/// <summary>
/// Method ToString
/// </summary>
/// <returns>A string</returns>
public override String ToString()
{
String s="{";
bool first=true;
foreach (DictionaryEntry entry in dictionary)
{
if( !first ) {
s+=", ";
}
first=false;
String name = (String) entry.Key;
Object value = entry.Value;
s+=name+"="+value;
}
s += "}";
return s;
}
/// <summary>
/// Unmarshalls the map from the given data or if the data is null just
/// return an empty map
/// </summary>
public static PrimitiveMap Unmarshal(byte[] data)
{
PrimitiveMap answer = new PrimitiveMap();
answer.dictionary = UnmarshalPrimitiveMap(data);
return answer;
}
public byte[] Marshal()
{
return MarshalPrimitiveMap(dictionary);
}
/// <summary>
/// Marshals the primitive type map to a byte array
/// </summary>
public static byte[] MarshalPrimitiveMap(IDictionary map)
{
if (map == null)
{
return null;
}
else
{
MemoryStream memoryStream = new MemoryStream();
MarshalPrimitiveMap(map, new OpenWireBinaryWriter(memoryStream));
return memoryStream.GetBuffer();
}
}
public static void MarshalPrimitiveMap(IDictionary map, BinaryWriter dataOut)
{
if (map == null)
{
dataOut.Write((int)-1);
}
else
{
dataOut.Write(map.Count);
foreach (DictionaryEntry entry in map)
{
String name = (String) entry.Key;
dataOut.Write(name);
Object value = entry.Value;
MarshalPrimitive(dataOut, value);
}
}}
/// <summary>
/// Unmarshals the primitive type map from the given byte array
/// </summary>
public static IDictionary UnmarshalPrimitiveMap(byte[] data)
{
if (data == null)
{
return new Hashtable();
}
else
{
return UnmarshalPrimitiveMap(new OpenWireBinaryReader(new MemoryStream(data)));
}
}
public static IDictionary UnmarshalPrimitiveMap(BinaryReader dataIn)
{
int size = dataIn.ReadInt32();
if (size < 0)
{
return null;
}
else
{
IDictionary answer = new Hashtable(size);
for (int i=0; i < size; i++)
{
String name = dataIn.ReadString();
answer[name] = UnmarshalPrimitive(dataIn);
}
return answer;
}
}
public static void MarshalPrimitive(BinaryWriter dataOut, Object value)
{
if (value == null)
{
dataOut.Write(NULL);
}
else if (value is bool)
{
dataOut.Write(BOOLEAN_TYPE);
dataOut.Write((bool) value);
}
else if (value is byte)
{
dataOut.Write(BYTE_TYPE);
dataOut.Write(((byte)value));
}
else if (value is char)
{
dataOut.Write(CHAR_TYPE);
dataOut.Write((char) value);
}
else if (value is short)
{
dataOut.Write(SHORT_TYPE);
dataOut.Write((short) value);
}
else if (value is int)
{
dataOut.Write(INTEGER_TYPE);
dataOut.Write((int) value);
}
else if (value is long)
{
dataOut.Write(LONG_TYPE);
dataOut.Write((long) value);
}
else if (value is float)
{
dataOut.Write(FLOAT_TYPE);
dataOut.Write((float) value);
}
else if (value is double)
{
dataOut.Write(DOUBLE_TYPE);
dataOut.Write((double) value);
}
else if (value is byte[])
{
byte[] data = (byte[]) value;
dataOut.Write(BYTE_ARRAY_TYPE);
dataOut.Write(data.Length);
dataOut.Write(data);
}
else if (value is string)
{
dataOut.Write(STRING_TYPE);
dataOut.Write((string) value);
}
else
{
throw new IOException("Object is not a primitive: " + value);
}
}
public static Object UnmarshalPrimitive(BinaryReader dataIn)
{
Object value=null;
switch (dataIn.ReadByte())
{
case BYTE_TYPE:
value = dataIn.ReadByte();
break;
case BOOLEAN_TYPE:
value = dataIn.ReadBoolean();
break;
case CHAR_TYPE:
value = dataIn.ReadChar();
break;
case SHORT_TYPE:
value = dataIn.ReadInt16();
break;
case INTEGER_TYPE:
value = dataIn.ReadInt32();
break;
case LONG_TYPE:
value = dataIn.ReadInt64();
break;
case FLOAT_TYPE:
value = dataIn.ReadSingle();
break;
case DOUBLE_TYPE:
value = dataIn.ReadDouble();
break;
case BYTE_ARRAY_TYPE:
int size = dataIn.ReadInt32();
byte[] data = new byte[size];
dataIn.Read(data, 0, size);
value = data;
break;
case STRING_TYPE:
value = dataIn.ReadString();
break;
}
return value;
}
}
}

View File

@ -0,0 +1,112 @@
/*
* Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using NUnit.Framework;
using System;
using System.IO;
using ActiveMQ.OpenWire;
namespace ActiveMQ.OpenWire
{
[TestFixture]
public class PrimitiveMapTest
{
bool a = true;
byte b = 123;
char c = 'c';
short d = 0x1234;
int e = 0x12345678;
long f = 0x1234567812345678;
string g = "Hello World!";
bool h = false;
byte i = 0xFF;
short j = -0x1234;
int k = -0x12345678;
long l = -0x1234567812345678;
[Test]
public void TestNotMarshalled()
{
PrimitiveMap map = CreatePrimitiveMap();
AssertPrimitiveMap(map);
}
[Test]
public void TestMarshalled()
{
PrimitiveMap map = CreatePrimitiveMap();
Console.WriteLine("data: "+map);
byte[] data = map.Marshal();
map = PrimitiveMap.Unmarshal(data);
Console.WriteLine("data: "+map);
AssertPrimitiveMap(map);
}
protected PrimitiveMap CreatePrimitiveMap()
{
PrimitiveMap map = new PrimitiveMap();
map["a"] = a;
map["b"] = b;
map["c"] = c;
map["d"] = d;
map["e"] = e;
map["f"] = f;
map["g"] = g;
map["h"] = h;
map["i"] = i;
map["j"] = j;
map["k"] = k;
map["l"] = l;
return map;
}
protected void AssertPrimitiveMap(PrimitiveMap map) {
// use generic API to access entries
Assert.AreEqual(a, map["a"], "generic map entry: a");
Assert.AreEqual(b, map["b"], "generic map entry: b");
Assert.AreEqual(c, map["c"], "generic map entry: c");
Assert.AreEqual(d, map["d"], "generic map entry: d");
Assert.AreEqual(e, map["e"], "generic map entry: e");
Assert.AreEqual(f, map["f"], "generic map entry: f");
Assert.AreEqual(g, map["g"], "generic map entry: g");
Assert.AreEqual(h, map["h"], "generic map entry: h");
Assert.AreEqual(i, map["i"], "generic map entry: i");
Assert.AreEqual(j, map["j"], "generic map entry: j");
Assert.AreEqual(k, map["k"], "generic map entry: k");
Assert.AreEqual(l, map["l"], "generic map entry: l");
// use type safe APIs
Assert.AreEqual(a, map.GetBool("a"), "map entry: a");
Assert.AreEqual(b, map.GetByte("b"), "map entry: b");
Assert.AreEqual(c, map.GetChar("c"), "map entry: c");
Assert.AreEqual(d, map.GetShort("d"), "map entry: d");
Assert.AreEqual(e, map.GetInt("e"), "map entry: e");
Assert.AreEqual(f, map.GetLong("f"), "map entry: f");
Assert.AreEqual(g, map.GetString("g"), "map entry: g");
Assert.AreEqual(h, map.GetBool("h"), "map entry: h");
Assert.AreEqual(i, map.GetByte("i"), "map entry: i");
Assert.AreEqual(j, map.GetShort("j"), "map entry: j");
Assert.AreEqual(k, map.GetInt("k"), "map entry: k");
Assert.AreEqual(l, map.GetLong("l"), "map entry: l");
}
}
}

View File

@ -1,122 +1,122 @@
/*
* Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using NMS;
using NUnit.Framework;
using System;
namespace NMS
{
[ TestFixture ]
public class MessageTest : JMSTestSupport
{
bool a = true;
byte b = 123;
char c = 'c';
short d = 0x1234;
int e = 0x12345678;
long f = 0x1234567812345678;
string g = "Hello World!";
bool h = false;
byte i = 0xFF;
short j = -0x1234;
int k = -0x12345678;
long l = -0x1234567812345678;
[SetUp]
override public void SetUp()
{
base.SetUp();
}
[TearDown]
override public void TearDown()
{
base.TearDown();
}
[ Test ]
public override void SendAndSyncReceive()
{
base.SendAndSyncReceive();
}
protected override IMessage CreateMessage()
{
IMessage message = session.CreateMessage();
message.Properties["a"] = a;
message.Properties["b"] = b;
message.Properties["c"] = c;
message.Properties["d"] = d;
message.Properties["e"] = e;
message.Properties["f"] = f;
message.Properties["g"] = g;
message.Properties["h"] = h;
message.Properties["i"] = i;
message.Properties["j"] = j;
message.Properties["k"] = k;
message.Properties["l"] = l;
return message;
}
protected override void AssertValidMessage(IMessage message)
{
Console.WriteLine("Received message: " + message);
Console.WriteLine("Received Count: " + message.Properties.Count);
Assert.AreEqual(ToHex(f), ToHex(message.Properties.GetLong("f")), "map entry: f as hex");
// use generic API to access entries
Assert.AreEqual(a, message.Properties["a"], "generic map entry: a");
Assert.AreEqual(b, message.Properties["b"], "generic map entry: b");
Assert.AreEqual(c, message.Properties["c"], "generic map entry: c");
Assert.AreEqual(d, message.Properties["d"], "generic map entry: d");
Assert.AreEqual(e, message.Properties["e"], "generic map entry: e");
Assert.AreEqual(f, message.Properties["f"], "generic map entry: f");
Assert.AreEqual(g, message.Properties["g"], "generic map entry: g");
Assert.AreEqual(h, message.Properties["h"], "generic map entry: h");
Assert.AreEqual(i, message.Properties["i"], "generic map entry: i");
Assert.AreEqual(j, message.Properties["j"], "generic map entry: j");
Assert.AreEqual(k, message.Properties["k"], "generic map entry: k");
Assert.AreEqual(l, message.Properties["l"], "generic map entry: l");
// use type safe APIs
Assert.AreEqual(a, message.Properties.GetBool("a"), "map entry: a");
Assert.AreEqual(b, message.Properties.GetByte("b"), "map entry: b");
Assert.AreEqual(c, message.Properties.GetChar("c"), "map entry: c");
Assert.AreEqual(d, message.Properties.GetShort("d"), "map entry: d");
Assert.AreEqual(e, message.Properties.GetInt("e"), "map entry: e");
Assert.AreEqual(f, message.Properties.GetLong("f"), "map entry: f");
Assert.AreEqual(g, message.Properties.GetString("g"), "map entry: g");
Assert.AreEqual(h, message.Properties.GetBool("h"), "map entry: h");
Assert.AreEqual(i, message.Properties.GetByte("i"), "map entry: i");
Assert.AreEqual(j, message.Properties.GetShort("j"), "map entry: j");
Assert.AreEqual(k, message.Properties.GetInt("k"), "map entry: k");
Assert.AreEqual(l, message.Properties.GetLong("l"), "map entry: l");
}
protected string ToHex(long value)
{
return String.Format("{0:x}", value);
}
}
}
/*
* Copyright 2006 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using NMS;
using NUnit.Framework;
using System;
namespace NMS
{
[ TestFixture ]
public class MessageTest : JMSTestSupport
{
bool a = true;
byte b = 123;
char c = 'c';
short d = 0x1234;
int e = 0x12345678;
long f = 0x1234567812345678;
string g = "Hello World!";
bool h = false;
byte i = 0xFF;
short j = -0x1234;
int k = -0x12345678;
long l = -0x1234567812345678;
[SetUp]
override public void SetUp()
{
base.SetUp();
}
[TearDown]
override public void TearDown()
{
base.TearDown();
}
[ Test ]
public override void SendAndSyncReceive()
{
base.SendAndSyncReceive();
}
protected override IMessage CreateMessage()
{
IMessage message = session.CreateMessage();
message.Properties["a"] = a;
message.Properties["b"] = b;
message.Properties["c"] = c;
message.Properties["d"] = d;
message.Properties["e"] = e;
message.Properties["f"] = f;
message.Properties["g"] = g;
message.Properties["h"] = h;
message.Properties["i"] = i;
message.Properties["j"] = j;
message.Properties["k"] = k;
message.Properties["l"] = l;
return message;
}
protected override void AssertValidMessage(IMessage message)
{
Console.WriteLine("Received message: " + message);
Console.WriteLine("Received Count: " + message.Properties.Count);
Assert.AreEqual(ToHex(f), ToHex(message.Properties.GetLong("f")), "map entry: f as hex");
// use generic API to access entries
Assert.AreEqual(a, message.Properties["a"], "generic map entry: a");
Assert.AreEqual(b, message.Properties["b"], "generic map entry: b");
Assert.AreEqual(c, message.Properties["c"], "generic map entry: c");
Assert.AreEqual(d, message.Properties["d"], "generic map entry: d");
Assert.AreEqual(e, message.Properties["e"], "generic map entry: e");
Assert.AreEqual(f, message.Properties["f"], "generic map entry: f");
Assert.AreEqual(g, message.Properties["g"], "generic map entry: g");
Assert.AreEqual(h, message.Properties["h"], "generic map entry: h");
Assert.AreEqual(i, message.Properties["i"], "generic map entry: i");
Assert.AreEqual(j, message.Properties["j"], "generic map entry: j");
Assert.AreEqual(k, message.Properties["k"], "generic map entry: k");
Assert.AreEqual(l, message.Properties["l"], "generic map entry: l");
// use type safe APIs
Assert.AreEqual(a, message.Properties.GetBool("a"), "map entry: a");
Assert.AreEqual(b, message.Properties.GetByte("b"), "map entry: b");
Assert.AreEqual(c, message.Properties.GetChar("c"), "map entry: c");
Assert.AreEqual(d, message.Properties.GetShort("d"), "map entry: d");
Assert.AreEqual(e, message.Properties.GetInt("e"), "map entry: e");
Assert.AreEqual(f, message.Properties.GetLong("f"), "map entry: f");
Assert.AreEqual(g, message.Properties.GetString("g"), "map entry: g");
Assert.AreEqual(h, message.Properties.GetBool("h"), "map entry: h");
Assert.AreEqual(i, message.Properties.GetByte("i"), "map entry: i");
Assert.AreEqual(j, message.Properties.GetShort("j"), "map entry: j");
Assert.AreEqual(k, message.Properties.GetInt("k"), "map entry: k");
Assert.AreEqual(l, message.Properties.GetLong("l"), "map entry: l");
}
protected string ToHex(long value)
{
return String.Format("{0:x}", value);
}
}
}