public void TestReadAndWriteFromBuffer() { List <short> shorts = new List <short>(); for (int i = 0; i < 10; i++) { shorts.Add((short)i); } MemoryStreamWrapper buffer = new MemoryStreamWrapper(new MemoryStream(shorts.Count * 2 + 2)); buffer.WriteInt16((short)shorts.Count); foreach (short s in shorts) { buffer.WriteInt16(s); } buffer.Stream.Position = 0; short count = buffer.ReadInt16(); List <short> readShorts = new List <short>(); for (int i = 0; i < count; i++) { readShorts.Add(buffer.ReadInt16()); } for (int i = 0; i < shorts.Count; i++) { Assert.AreEqual(readShorts[i], shorts[i]); } }
public void PutEntries(List <BufferEntry> entries) { int size = CalculateEntriesSize(entries) * 2; this.buffer = new MemoryStreamWrapper(new MemoryStream(size + Constant.INTEGER_BYTES * 4)); buffer.WriteInt32(size); buffer.WriteInt32(entries.Count); BufferEntry firstEntry = entries[0]; buffer.WriteInt32(firstEntry.TokenInfo.Count); buffer.WriteInt32(firstEntry.PosInfo.Count); buffer.WriteInt32(firstEntry.Features.Count); foreach (BufferEntry entry in entries) { foreach (short s in entry.TokenInfo) { buffer.WriteInt16(s); } buffer.Write(entry.PosInfo.ToArray(), 0, entry.PosInfo.Count); foreach (int feature in entry.Features) { buffer.WriteInt32(feature); } } }
private int Put(int index, string value) { bool katakana = StringHelper.IsKatakanaOnly(value); byte[] bytes; short length; if (katakana) { bytes = GetKatakanaBytes(value); length = (short)(bytes.Length | KATAKANA_FLAG & 0xffff); } else { bytes = GetBytes(value); length = (short)bytes.Length; } buffer.Stream.Position = index; buffer.WriteInt16(length, index); buffer.Stream.Position = index + Constant.SHORT_BYTES; buffer.Write(bytes, 0, bytes.Length); return(index + Constant.SHORT_BYTES + bytes.Length); }