public void TestReadBytesbyteArray()
        {
            BytesMessage msg = new BytesMessage();

            byte[] data = new byte[50];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)i;
            }
            msg.WriteBytes(data);
            msg.Reset();
            byte[] test = new byte[data.Length];
            msg.ReadBytes(test);
            for (int i = 0; i < test.Length; i++)
            {
                Assert.IsTrue(test[i] == i);
            }
        }
Beispiel #2
0
    /// <summary>
    /// event_MessageHandler
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    void event_MessageHandler(object sender, EMSMessageEventArgs args)
    {
        Console.WriteLine("Received message: " + args.Message);
        if (args.Message is BytesMessage)
        {
            Console.WriteLine("Writing file to disk\n");
            var          filename = args.Message.GetStringProperty("FILE_NAME");
            var          filesize = args.Message.GetStringProperty("FILE_SIZE");
            byte[]       data     = null;
            BytesMessage bm       = (BytesMessage)args.Message;
            data = new byte[(int)bm.BodyLength]; // I could send in FILE_SIZE here.

            // fill the data variable with the bytes in the ByteMessage
            bm.ReadBytes(data);

            try
            {
                File.WriteAllBytes(filename + ".xml", data);
            }
            catch (IOException ex)
            {
                Console.Error.WriteLine(ex.StackTrace);
                Environment.Exit(0);
            }
        }

        // Acknowledge we received the message.  If the message did not get written to disk
        // we want to not acknowledge the message was received.
        // need to read up more about this...
        if (ackMode == Session.CLIENT_ACKNOWLEDGE ||
            ackMode == Session.EXPLICIT_CLIENT_ACKNOWLEDGE ||
            ackMode == Session.EXPLICIT_CLIENT_DUPS_OK_ACKNOWLEDGE)
        {
            args.Message.Acknowledge();
        }
    }
 /// <summary> Extract a byte array from the given BytesMessage.</summary>
 /// <param name="message">the message to convert
 /// </param>
 /// <returns> the resulting byte array
 /// </returns>
 /// <throws>  EMSException if thrown by EMS methods </throws>
 protected virtual byte[] ExtractByteArrayFromMessage(BytesMessage message)
 {
     byte[] bytes = new byte[(int)message.BodyLength];
     message.ReadBytes(bytes);
     return(bytes);
 }
        public void TestWriteOnlyBody()
        {
            BytesMessage message = new BytesMessage();

            message.ClearBody();

            try
            {
                message.WriteBoolean(true);
                message.WriteByte((byte)1);
                message.WriteBytes(new byte[1]);
                message.WriteBytes(new byte[3], 0, 2);
                message.WriteChar('a');
                message.WriteDouble(1.5);
                message.WriteSingle((float)1.5);
                message.WriteInt32(1);
                message.WriteInt64(1);
                message.WriteObject("stringobj");
                message.WriteInt16((short)1);
                message.WriteString("utfstring");
            }
            catch (MessageNotWriteableException)
            {
                Assert.Fail("Should be writeable");
            }

            try
            {
                message.ReadBoolean();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadByte();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadBytes(new byte[1]);
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadBytes(new byte[2], 2);
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadChar();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadDouble();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadSingle();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadInt32();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadInt64();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadString();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadInt16();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }

            try
            {
                message.ReadString();
                Assert.Fail("Should have thrown exception");
            }
            catch (MessageNotReadableException)
            {
            }
        }