Skip to content

Commit

Permalink
clean project code: by JetBrains Rider (Code cleanup)
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed May 21, 2024
1 parent e66146b commit 6290651
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 177 deletions.
283 changes: 151 additions & 132 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Byter
Byter is a bytes serializer. It can serialize and deserialize from primitive type.

Byter is a bytes serializer. It can serialize and deserialize from primitive type.

> ###### Byter is very stable, super easy to learn, extremely fast and inexpensive (2 bytes or ``sizeof(char)`` of overhead per data written) and ``100%`` written in ``C#`` and it's FREE!
<br><hr><br>

## Install

- #### Nuget [SEE HERE](https://www.nuget.org/packages/Byter)
###### .NET CLI
```rb
Expand All @@ -19,10 +21,13 @@ Byter is a bytes serializer. It can serialize and deserialize from primitive typ
## Usage

#### Namespace

```csharp
using Byter
```

#### Types

```ts
[
`byte`,
Expand All @@ -46,123 +51,129 @@ using Byter

<hr/>


## Writer

> Constructor

-
```cs
_ = new Writer(); // Create default instance
_ = new Writer(new Writer()); // Create instance and copy from existing Writer
_ = new Writer(ref new Writer()); // Create instance and copy from existing Writer (using ref)
```
```cs
_ = new Writer(); // Create default instance
_ = new Writer(new Writer()); // Create instance and copy from existing Writer
_ = new Writer(ref new Writer()); // Create instance and copy from existing Writer (using ref)
```

<br>

> Proprietary
- #### ``Length``
```cs
using Byter;
var w = new Writer();
- #### ``Length``
```cs
using Byter;

// Get lenght of buffer
int lenght = w.Length;
```
var w = new Writer();

// Get lenght of buffer
int lenght = w.Length;
```

<br/>

> Methods
- #### ``Write(dynamic value)``
```cs
using Byter;

// Create writer instance;
using var w = new Writer();

// Write string
w.Write("KEZERO");

// Write char
w.Write('K');

// Write Float3 (Vector3)
w.Write(new Float3(10F, 10F, 10F));

// Get bytes (buffer)
byte[] buffer = w.GetBytes();

// Get byte list (buffer)
List<byte> bytes = w.GetList();
```

- #### ``GetBytes()``
```cs
using Byter;

var w = new Writer();

// Return buffer on <Writer> instance
byte[] buffer = w.GetBytes();
```

- #### ``GetList()``
```cs
using Byter;

var w = new Writer();
- #### ``Write(dynamic value)``
```cs
using Byter;

// Create writer instance;
using var w = new Writer();

// Return buffer on <Writer> instance as byte list
List<byte> bytes = w.GetList();
```
// Write string
w.Write("KEZERO");

- #### ``Clear()``
```cs
using Byter;
// Write char
w.Write('K');

var w = new Writer();
w.Write((int)1000);
w.Write((float)100f);
// Write Float3 (Vector3)
w.Write(new Float3(10F, 10F, 10F));

// Clear internal buffer and reset internal index
w.Clear();
```
// Get bytes (buffer)
byte[] buffer = w.GetBytes();

// Get byte list (buffer)
List<byte> bytes = w.GetList();
```

- #### ``GetBytes()``
```cs
using Byter;

var w = new Writer();

// Return buffer on <Writer> instance
byte[] buffer = w.GetBytes();
```

- #### ``GetList()``
```cs
using Byter;

var w = new Writer();

// Return buffer on <Writer> instance as byte list
List<byte> bytes = w.GetList();
```

- #### ``Clear()``
```cs
using Byter;

var w = new Writer();
w.Write((int)1000);
w.Write((float)100f);

// Clear internal buffer and reset internal index
w.Clear();
```

<hr>

## Reader

> Constructor
-
```cs
_ = new Reader(new Writer()); // Create instance and copy buffer from existing Writer
_ = new Reader(ref new Writer()); // Create instance and copy buffer from existing Writer (ref Writer)
_ = new Reader(new byte[] { 1, 1, 1, 1 }); // Create instance from buffer (bytes (byte[]))
```
```cs
_ = new Reader(new Writer()); // Create instance and copy buffer from existing Writer
_ = new Reader(ref new Writer()); // Create instance and copy buffer from existing Writer (ref Writer)
_ = new Reader(new byte[] { 1, 1, 1, 1 }); // Create instance from buffer (bytes (byte[]))
```

<br/>

> Proprietary
- #### ``Length``
```cs
using Byter;
byte[] buffer = ...;
var r = new Reader(buffer);
- #### ``Length``
```cs
using Byter;

// Get lenght of buffer
int lenght = r.Length;
```
byte[] buffer = ...;
var r = new Reader(buffer);

- #### ``Position``
```cs
using Byter;
// Get lenght of buffer
int lenght = r.Length;
```

byte[] buffer = ...;
var r = new Reader(buffer);
- #### ``Position``
```cs
using Byter;

byte[] buffer = ...;
var r = new Reader(buffer);

// return current index of readed buffer
int position = r.Position;
```

// return current index of readed buffer
int position = r.Position;
```

- #### ``Success``
```cs
using Byter;
Expand All @@ -177,67 +188,74 @@ using Byter
bool success = r.Success;
```
- ###### WARNING
Internally, before data is written a prefix is added in front of it, so when reading it always compares the prefix of the (data type) you want to read with the strings in the read buffer. if the prefixes do not match then o (Reader. Success = False), eg. If you write an (int) and try to read a float (Reader.Success = False) because the prefix of an (int) is different from that of a (float), it is recommended to read all the data and at the end check the success, if it is (Reader.Success = False) then one or more data is corrupt. This means that Writer and Reader add dipping to your write and read data.
Internally, before data is written a prefix is added in front of it, so when reading it always compares the prefix
of the (data type) you want to read with the strings in the read buffer. if the prefixes do not match then o (
Reader. Success = False), eg. If you write an (int) and try to read a float (Reader.Success = False) because the
prefix of an (int) is different from that of a (float), it is recommended to read all the data and at the end
check the success, if it is (Reader.Success = False) then one or more data is corrupt. This means that Writer and
Reader add dipping to your write and read data.

<br/>

> Methods
- #### ``Read<T>()`` ``Read<string>(Encoding encoding)``;
```cs
using Byter;

byte[] buffer = ...;

// Create reader instance
using r = new Reader(buffer);

string name = r.Read<string>();
char firstLatter = r.Read<char>();
Float3 position = r.Read<Float3>();

// Check if is success
if (r.Success)
{
Console.WriteLine($"Name: {name}");
Console.WriteLine($"First Latter: {firstLatter}");
Console.WriteLine($"Position: {position}");
}
else
{
Console.WriteLine("Error on get data");
}
```

- #### ``Seek(int position)``;
```cs
using Byter;
- #### ``Read<T>()`` ``Read<string>(Encoding encoding)``;
```cs
using Byter;

byte[] buffer = ...;

// Create reader instance
using r = new Reader(buffer);

string name = r.Read<string>();
char firstLatter = r.Read<char>();
Float3 position = r.Read<Float3>();

// Check if is success
if (r.Success)
{
Console.WriteLine($"Name: {name}");
Console.WriteLine($"First Latter: {firstLatter}");
Console.WriteLine($"Position: {position}");
}
else
{
Console.WriteLine("Error on get data");
}
```

using var w = new Writer();
w.Write("KEZERO");
w.Write((int) 1024);
- #### ``Seek(int position)``;
```cs
using Byter;

using var r = new Reader(ref w);
using var w = new Writer();
w.Write("KEZERO");
w.Write((int) 1024);

string name = r.Read<string>(); // name: KEZERO
int age = r.Read<int>(); // age: 1024
// Move index (Reader.Position) for target value "MIN VALUE IS 0";
r.Seek(10); // move current index for read for 10 (when start read using .Read<Type> will start read on 10 index from buffer);
using var r = new Reader(ref w);

// Reset internal position
r.Seek(0);
string name = r.Read<string>(); // name: KEZERO
int age = r.Read<int>(); // age: 1024
// Move index (Reader.Position) for target value "MIN VALUE IS 0";
r.Seek(10); // move current index for read for 10 (when start read using .Read<Type> will start read on 10 index from buffer);
string name2 = r.Read<string>(); // name: KEZERO (because the start index of this string on buffer is 0)
int age2 = r.Read<int>(); age: 1024;
// Reset internal position
r.Seek(0);

// NEED READ LAST INT
r.Seek(r.Position - sizeof(int) + sizeof(char) /* int size is 4 + char size is 2. The 2 bytes is overhead of protocol */);
int age3 = r.Read<int>(); age: 1024 (because i return 4bytes before old current value)
```
string name2 = r.Read<string>(); // name: KEZERO (because the start index of this string on buffer is 0)
int age2 = r.Read<int>(); age: 1024;

// NEED READ LAST INT
r.Seek(r.Position - sizeof(int) + sizeof(char) /* int size is 4 + char size is 2. The 2 bytes is overhead of protocol */);
int age3 = r.Read<int>(); age: 1024 (because i return 4bytes before old current value)
```

<br/><hr/><br/>

#### Sample

```csharp
using Byter;

Expand Down Expand Up @@ -297,6 +315,7 @@ reader.Dispose(); // Destroy Reader
<br/><hr/><br/>

#### Install using git submodule

```rb
# Install - recommend a stable branch e.g. "1.x" or use a fork repository, --depth clone last sources
git submodule add --name byter --depth 1 --branch main "https://github.com/alec1o/byter" vendor/byter
Expand Down
Loading

0 comments on commit 6290651

Please sign in to comment.