Skip to content

Commit c9a8ec4

Browse files
authored
Merge pull request #31 from styxke/utf8_fix
Fixed a small bug in case the server responds with UT8 responses.
2 parents 5594ead + 298147a commit c9a8ec4

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/Base/Net/SimpleMLLPClient.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Text;
34
using System.IO;
5+
using System.Linq;
46
using System.Security.Cryptography.X509Certificates;
57
using System.Net.Sockets;
68
using System.Net.Security;
@@ -118,14 +120,18 @@ public string SendHL7Message(string message, double timeout = 30000)
118120
sw.Flush();
119121

120122
// Read the reply
123+
List<byte> buffer = new List<byte>();
121124
StringBuilder sb = new StringBuilder();
122125
bool messageComplete = false;
123126
DateTime startReadingTime = DateTime.Now;
124127
while (!messageComplete)
125128
{
126129
int b = streamToUse.ReadByte();
127130
if (b != -1)
128-
sb.Append((char) b);
131+
{
132+
buffer.Add((byte)b);
133+
sb.Append((char)b);
134+
}
129135

130136
messageComplete = MLLP.ValidateMLLPMessage(sb);
131137

@@ -135,9 +141,8 @@ public string SendHL7Message(string message, double timeout = 30000)
135141
if (!messageComplete && DateTime.Now.Subtract(startReadingTime).TotalMilliseconds > timeout)
136142
throw new TimeoutException($"Reading the HL7 reply timed out after {timeout} milliseconds.");
137143
}
138-
MLLP.StripMLLPContainer(sb);
139-
140-
return sb.ToString();
144+
byte[] messageBytes = MLLP.StripMLLPContainer(buffer);
145+
return (encodingForStream ?? Encoding.UTF8).GetString(messageBytes);
141146
}
142147

143148
/// <summary>

src/Base/Util/MLLP.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Text;
1+
using System.Collections.Generic;
2+
using System.Text;
3+
using System.Linq;
24

35
namespace NHapiTools.Base.Util
46
{
@@ -24,6 +26,12 @@ public static void StripMLLPContainer(StringBuilder sb)
2426
sb.Remove(0, 1);
2527
sb.Remove(sb.Length - 2, 2);
2628
}
29+
30+
public static byte[] StripMLLPContainer(List<byte> bytes)
31+
{
32+
// Strip the message of the MLLP container characters
33+
return bytes.Skip(1).Take(bytes.Count - 3).ToArray();
34+
}
2735

2836
/// <summary>
2937
/// Validate the MLLP message containing the HL7 message

0 commit comments

Comments
 (0)