Skip to content

Commit d3e6fe4

Browse files
committed
Merge pull request #86 from avatar29A/implement-stringdecoder-and-string-encoder-85
Implementation the StringEncoder and StringDecoder and all them depen…
2 parents a5832b5 + a8b4061 commit d3e6fe4

13 files changed

Lines changed: 823 additions & 22 deletions

src/DotNetty.Buffers/AbstractByteBuffer.cs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,5 +933,109 @@ public string ToString(int index, int length, Encoding encoding)
933933
{
934934
return ByteBufferUtil.DecodeString(this, index, length, encoding);
935935
}
936+
937+
public int ForEachByte(ByteProcessor processor)
938+
{
939+
int index = this.ReaderIndex;
940+
int length = this.WriterIndex - index;
941+
this.EnsureAccessible();
942+
943+
return this.ForEachByteAsc0(index, length, processor);
944+
}
945+
946+
public int ForEachByte(int index, int length, ByteProcessor processor)
947+
{
948+
this.CheckIndex(index, length);
949+
950+
return this.ForEachByteAsc0(index, length, processor);
951+
}
952+
953+
int ForEachByteAsc0(int index, int length, ByteProcessor processor)
954+
{
955+
if (processor == null)
956+
{
957+
throw new NullReferenceException("processor");
958+
}
959+
960+
if (length == 0)
961+
{
962+
return -1;
963+
}
964+
965+
int endIndex = index + length;
966+
int i = index;
967+
try
968+
{
969+
do
970+
{
971+
if (processor.Process(this._GetByte(i)))
972+
{
973+
i++;
974+
}
975+
else
976+
{
977+
return i;
978+
}
979+
} while (i < endIndex);
980+
}
981+
catch
982+
{
983+
throw;
984+
}
985+
986+
return -1;
987+
}
988+
989+
public int ForEachByteDesc(ByteProcessor processor)
990+
{
991+
int index = this.ReaderIndex;
992+
int length = this.WriterIndex - index;
993+
this.EnsureAccessible();
994+
995+
return this.ForEachByteDesc0(index, length, processor);
996+
}
997+
998+
public int ForEachByteDesc(int index, int length, ByteProcessor processor)
999+
{
1000+
this.CheckIndex(index, length);
1001+
1002+
return this.ForEachByteDesc0(index, length, processor);
1003+
}
1004+
1005+
int ForEachByteDesc0(int index, int length, ByteProcessor processor)
1006+
{
1007+
1008+
if (processor == null)
1009+
{
1010+
throw new NullReferenceException("processor");
1011+
}
1012+
1013+
if (length == 0)
1014+
{
1015+
return -1;
1016+
}
1017+
1018+
int i = index + length - 1;
1019+
try
1020+
{
1021+
do
1022+
{
1023+
if (processor.Process(this._GetByte(i)))
1024+
{
1025+
i--;
1026+
}
1027+
else
1028+
{
1029+
return i;
1030+
}
1031+
} while (i >= index);
1032+
}
1033+
catch
1034+
{
1035+
throw;
1036+
}
1037+
1038+
return -1;
1039+
}
9361040
}
9371041
}

src/DotNetty.Buffers/ByteBufferUtil.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public static string HexDump(byte[] array)
160160
public static string HexDump(byte[] array, int fromIndex, int length)
161161
{
162162
Contract.Requires(length >= 0);
163-
163+
164164
if (length == 0)
165165
{
166166
return "";
@@ -570,6 +570,57 @@ static void AppendHexDumpRowPrefix(StringBuilder dump, int row, int rowStartInde
570570
}
571571
}
572572

573+
574+
/// <summary>
575+
/// Encode the given <see cref="CharBuffer"/> using the given <see cref="Encoding"/> into a new <see cref="IByteBuffer"/> which
576+
/// is allocated via the <see cref="IByteBufferAllocator"/>.
577+
/// </summary>
578+
/// <param name="alloc">The <see cref="IByteBufferAllocator"/> to allocate {@link IByteBuffer}.</param>
579+
/// <param name="src">src The <see cref="String"/> to encode.</param>
580+
/// <param name="encoding">charset The specified <see cref="Encoding"/></param>
581+
public static IByteBuffer EncodeString(IByteBufferAllocator alloc, string src, Encoding encoding)
582+
{
583+
return EncodeString0(alloc, src, encoding, 0);
584+
}
585+
586+
/// <summary>
587+
/// Encode the given <see cref="CharBuffer"/> using the given <see cref="Encoding"/> into a new <see cref="IByteBuffer"/> which
588+
/// is allocated via the <see cref="IByteBufferAllocator"/>.
589+
/// </summary>
590+
/// <param name="alloc">The <see cref="IByteBufferAllocator"/> to allocate {@link IByteBuffer}.</param>
591+
/// <param name="src">src The <see cref="String"/> to encode.</param>
592+
/// <param name="encoding">charset The specified <see cref="Encoding"/></param>
593+
/// <param name="extraCapacity">the extra capacity to alloc except the space for decoding.</param>
594+
public static IByteBuffer EncodeString(IByteBufferAllocator alloc, string src, Encoding encoding, int extraCapacity)
595+
{
596+
return EncodeString0(alloc, src, encoding, extraCapacity);
597+
}
598+
599+
static IByteBuffer EncodeString0(IByteBufferAllocator alloc, string src, Encoding encoding, int extraCapacity)
600+
{
601+
int length = encoding.GetMaxByteCount(src.Length) + extraCapacity;
602+
var release = true;
603+
604+
IByteBuffer dst = alloc.Buffer(length);
605+
Contract.Assert(dst.HasArray, "Operation expects allocator to operate array-based buffers.");
606+
607+
try
608+
{
609+
encoding.GetBytes(src, 0, src.Length, dst.Array, dst.ArrayOffset);
610+
dst.SetWriterIndex(length);
611+
release = false;
612+
613+
return dst;
614+
}
615+
finally
616+
{
617+
if (release)
618+
{
619+
dst.Release();
620+
}
621+
}
622+
}
623+
573624
public static string DecodeString(IByteBuffer src, int readerIndex, int len, Encoding encoding)
574625
{
575626
if (len == 0)

src/DotNetty.Buffers/EmptyByteBuffer.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,28 @@ IByteBuffer CheckLength(int length)
646646
return this;
647647
}
648648

649+
public int ForEachByte(ByteProcessor processor)
650+
{
651+
return -1;
652+
}
653+
654+
public int ForEachByte(int index, int length, ByteProcessor processor)
655+
{
656+
CheckIndex(index, length);
657+
return -1;
658+
}
659+
660+
public int ForEachByteDesc(ByteProcessor processor)
661+
{
662+
return -1;
663+
}
664+
665+
public int ForEachByteDesc(int index, int length, ByteProcessor processor)
666+
{
667+
CheckIndex(index, length);
668+
return -1;
669+
}
670+
649671
public string ToString(Encoding encoding)
650672
{
651673
return string.Empty;

src/DotNetty.Buffers/IByteBuffer.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace DotNetty.Buffers
99
using System.Threading;
1010
using System.Threading.Tasks;
1111
using DotNetty.Common;
12+
using DotNetty.Common.Utilities;
1213

1314
/// <summary>
1415
/// Inspired by the Netty ByteBuffer implementation (https://github.com/netty/netty/blob/master/buffer/src/main/java/io/netty/buffer/ByteBuf.java)
@@ -504,7 +505,7 @@ public interface IByteBuffer : IReferenceCounted
504505
IByteBuffer ReadBytes(byte[] destination, int dstIndex, int length);
505506

506507
IByteBuffer ReadBytes(Stream destination, int length);
507-
508+
508509
/// <summary>
509510
/// Increases the current <see cref="ReaderIndex"/> by the specified <see cref="length"/> in this buffer.
510511
/// </summary>
@@ -590,5 +591,47 @@ public interface IByteBuffer : IReferenceCounted
590591
string ToString(Encoding encoding);
591592

592593
string ToString(int index, int length, Encoding encoding);
594+
595+
/// <summary>
596+
/// Iterates over the readable bytes of this buffer with the specified <c>processor</c> in ascending order.
597+
/// </summary>
598+
/// <returns><c>1</c> if the processor iterated to or beyond the end of the readable bytes.
599+
/// The last-visited index If the <see cref="ByteProcessor.Process(byte)"/> returned <c>false</c>.
600+
/// </returns>
601+
/// <param name="processor">Processor.</param>
602+
int ForEachByte(ByteProcessor processor);
603+
604+
/// <summary>
605+
/// Iterates over the specified area of this buffer with the specified {@code processor} in ascending order.
606+
/// (i.e. {@code index}, {@code (index + 1)}, .. {@code (index + length - 1)})
607+
/// </summary>
608+
/// <returns>{@code -1} if the processor iterated to or beyond the end of the specified area.
609+
/// The last-visited index If the {@link ByteProcessor#process(byte)} returned {@code false}.
610+
/// </returns>
611+
/// <param name="index">Index.</param>
612+
/// <param name="length">Length.</param>
613+
/// <param name="processor">Processor.</param>
614+
int ForEachByte(int index, int length, ByteProcessor processor);
615+
616+
/// <summary>
617+
/// Iterates over the readable bytes of this buffer with the specified {@code processor} in descending order.
618+
/// </summary>
619+
/// <returns>{@code -1} if the processor iterated to or beyond the beginning of the readable bytes.
620+
/// The last-visited index If the {@link ByteProcessor#process(byte)} returned {@code false}.
621+
/// </returns>
622+
/// <param name="processor">Processor.</param>
623+
int ForEachByteDesc(ByteProcessor processor);
624+
625+
/// <summary>
626+
/// Iterates over the specified area of this buffer with the specified {@code processor} in descending order.
627+
/// (i.e. {@code (index + length - 1)}, {@code (index + length - 2)}, ... {@code index})
628+
/// </summary>
629+
/// <returns>{@code -1} if the processor iterated to or beyond the beginning of the specified area.
630+
/// The last-visited index If the {@link ByteProcessor#process(byte)} returned {@code false}.
631+
/// </returns>
632+
/// <param name="index">Index.</param>
633+
/// <param name="length">Length.</param>
634+
/// <param name="processor">Processor.</param>
635+
int ForEachByteDesc(int index, int length, ByteProcessor processor);
593636
}
594637
}

src/DotNetty.Buffers/SwappedByteBuffer.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ namespace DotNetty.Buffers
1111
using System.Threading;
1212
using System.Threading.Tasks;
1313
using DotNetty.Common;
14+
using DotNetty.Common.Utilities;
1415

1516
/// <summary>
1617
/// Wrapper which swaps the <see cref="ByteOrder"/> of a <see cref="IByteBuffer"/>.
1718
/// </summary>
18-
public class SwappedByteBuffer : IByteBuffer
19+
public class SwappedByteBuffer : IByteBuffer
1920
{
2021
readonly IByteBuffer buf;
2122
readonly ByteOrder order;
@@ -666,6 +667,26 @@ public Task WriteBytesAsync(Stream stream, int length, CancellationToken cancell
666667
return this.buf.WriteBytesAsync(stream, length, cancellationToken);
667668
}
668669

670+
public int ForEachByte(ByteProcessor processor)
671+
{
672+
return this.buf.ForEachByte(processor);
673+
}
674+
675+
public int ForEachByte(int index, int length, ByteProcessor processor)
676+
{
677+
return this.buf.ForEachByte(index, length, processor);
678+
}
679+
680+
public int ForEachByteDesc(ByteProcessor processor)
681+
{
682+
return this.buf.ForEachByteDesc(processor);
683+
}
684+
685+
public int ForEachByteDesc(int index, int length, ByteProcessor processor)
686+
{
687+
return this.buf.ForEachByteDesc(index, length, processor);
688+
}
689+
669690
public override string ToString()
670691
{
671692
return "Swapped(" + this.buf + ")";

src/DotNetty.Buffers/WrappedByteBuf.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace DotNetty.Buffers
99
using System.Threading;
1010
using System.Threading.Tasks;
1111
using DotNetty.Common;
12+
using DotNetty.Common.Utilities;
1213

1314
public class WrappedByteBuffer : IByteBuffer
1415
{
@@ -726,6 +727,26 @@ public virtual bool Release(int decrement)
726727
return this.Buf.Release(decrement);
727728
}
728729

730+
public int ForEachByte(ByteProcessor processor)
731+
{
732+
return this.ForEachByte(processor);
733+
}
734+
735+
public int ForEachByte(int index, int length, ByteProcessor processor)
736+
{
737+
return this.ForEachByte(index, length, processor);
738+
}
739+
740+
public int ForEachByteDesc(ByteProcessor processor)
741+
{
742+
return this.ForEachByteDesc(processor);
743+
}
744+
745+
public int ForEachByteDesc(int index, int length, ByteProcessor processor)
746+
{
747+
return this.ForEachByteDesc(processor);
748+
}
749+
729750
public virtual string ToString(Encoding encoding)
730751
{
731752
return Buf.ToString(encoding);

src/DotNetty.Codecs/DotNetty.Codecs.csproj

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
55
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -54,18 +54,22 @@
5454
<Compile Include="ReplayingDecoder.cs" />
5555
<Compile Include="TooLongFrameException.cs" />
5656
<Compile Include="UnsupportedMessageTypeException.cs" />
57+
<Compile Include="Strings\StringEncoder.cs" />
58+
<Compile Include="Strings\StringDecoder.cs" />
59+
<Compile Include="LineBasedFrameDecoder.cs" />
60+
<Compile Include="MessageToMessageDecoder.cs" />
5761
</ItemGroup>
5862
<ItemGroup>
5963
<ProjectReference Include="..\DotNetty.Buffers\DotNetty.Buffers.csproj">
60-
<Project>{5de3c557-48bf-4cdb-9f47-474d343dd841}</Project>
64+
<Project>{5DE3C557-48BF-4CDB-9F47-474D343DD841}</Project>
6165
<Name>DotNetty.Buffers</Name>
6266
</ProjectReference>
6367
<ProjectReference Include="..\DotNetty.Common\DotNetty.Common.csproj">
64-
<Project>{de58fe41-5e99-44e5-86bc-fc9ed8761daf}</Project>
68+
<Project>{DE58FE41-5E99-44E5-86BC-FC9ED8761DAF}</Project>
6569
<Name>DotNetty.Common</Name>
6670
</ProjectReference>
6771
<ProjectReference Include="..\DotNetty.Transport\DotNetty.Transport.csproj">
68-
<Project>{8218c9ee-0a4a-432f-a12a-b54202f97b05}</Project>
72+
<Project>{8218C9EE-0A4A-432F-A12A-B54202F97B05}</Project>
6973
<Name>DotNetty.Transport</Name>
7074
</ProjectReference>
7175
</ItemGroup>
@@ -80,4 +84,7 @@
8084
<Target Name="AfterBuild">
8185
</Target>
8286
-->
87+
<ItemGroup>
88+
<Folder Include="Strings\" />
89+
</ItemGroup>
8390
</Project>

0 commit comments

Comments
 (0)