Created
June 19, 2014 00:02
-
-
Save rcythr/e1b58eede76a8b0fad41 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BufferIterator | |
| { | |
| private BlockingCollection<byte[]> queue; | |
| private byte[] cur_msg = null; | |
| private int cur_msg_index; | |
| public BufferIterator(BlockingCollection<byte[]> queue) | |
| { | |
| this.queue = queue; | |
| } | |
| public byte[] Read() | |
| { | |
| if (cur_msg != null) | |
| { | |
| byte[] result = new byte[cur_msg.Length - cur_msg_index]; | |
| for (int i=0; cur_msg_index < cur_msg.Length; ++i, ++cur_msg_index ) | |
| { | |
| result[i] = cur_msg[cur_msg_index]; | |
| } | |
| cur_msg = null; | |
| cur_msg_index = 0; | |
| return result; | |
| } | |
| else | |
| { | |
| return queue.Take(); | |
| } | |
| } | |
| public byte[] Read(int max) | |
| { | |
| byte[] result = new byte[max]; | |
| int filled_index = 0; | |
| while (filled_index < max) | |
| { | |
| if (cur_msg == null || cur_msg_index >= cur_msg.Length) | |
| { | |
| cur_msg = queue.Take(); | |
| cur_msg_index = 0; | |
| } | |
| int num_to_read = System.Math.Min(cur_msg.Length - cur_msg_index, max - filled_index); | |
| for (int i = 0; i < num_to_read; ++i, ++cur_msg_index, ++filled_index) | |
| { | |
| result[filled_index] = cur_msg[cur_msg_index]; | |
| } | |
| } | |
| return result; | |
| } | |
| public string ReadString() | |
| { | |
| return Encoding.ASCII.GetString(Read()); | |
| } | |
| public string ReadString(int max) | |
| { | |
| return Encoding.ASCII.GetString(Read(max)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment