Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
P
Protocol Buffer
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tammam.alsoleman
Protocol Buffer
Commits
ebb7f2d0
Commit
ebb7f2d0
authored
Nov 05, 2025
by
tammam.alsoleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Encoding & Decoding & Comparison with JSON
parent
99843415
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
262 additions
and
75 deletions
+262
-75
Main.java
src/main/java/org/example/Main.java
+262
-75
No files found.
src/main/java/org/example/Main.java
View file @
ebb7f2d0
package
org
.
example
;
import
com.google.protobuf.InvalidProtocolBufferException
;
import
org.ds.proto.Author
;
import
org.ds.proto.Book
;
import
org.ds.proto.BookType
;
import
org.ds.proto.Library
;
import
org.ds.proto.*
;
import
java.util.Arrays
;
public
class
Main
{
public
static
void
main
(
String
[]
args
)
throws
InvalidProtocolBufferException
{
byte
[]
msg
=
sender
();
receiver
(
msg
);
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Protocol Buffers Encoding & Decoding Demo\n"
);
// 1. ترميز وفك تشفير كتاب مفرد
encodeDecodeSingleBook
();
System
.
out
.
println
(
"\n"
+
"="
.
repeat
(
50
)
+
"\n"
);
// 2. ترميز وفك تشفير مكتبة (مجموعة كتب)
encodeDecodeLibrary
();
System
.
out
.
println
(
"\n"
+
"="
.
repeat
(
50
)
+
"\n"
);
// 3. ترميز وفك تشفير طلبات الخدمة
encodeDecodeServiceMessages
();
}
private
static
void
receiver
(
byte
[]
msg
)
throws
InvalidProtocolBufferException
{
System
.
out
.
println
(
"Received Data:"
);
// Display binary data
for
(
int
i
=
0
;
i
<
msg
.
length
;
i
++)
{
System
.
out
.
print
(
msg
[
i
]
+
" "
);
private
static
void
encodeDecodeSingleBook
()
{
System
.
out
.
println
(
"1. Single Book Encoding & Decoding:"
);
try
{
// 🔹 الخطوة 1: إنشاء كائن Book
Book
originalBook
=
Book
.
newBuilder
()
.
setTitle
(
"Designing Data-Intensive Applications"
)
.
setAuthor
(
Author
.
newBuilder
()
.
setName
(
"Martin Kleppmann"
)
.
setCountry
(
"United Kingdom"
)
.
build
())
.
setIsbn
(
"978-1449373320"
)
.
setYear
(
2017
)
.
setType
(
BookType
.
TECHNICAL
)
.
addAllCategories
(
Arrays
.
asList
(
"Databases"
,
"Distributed Systems"
,
"Software Architecture"
))
.
setAvailable
(
true
)
.
build
();
System
.
out
.
println
(
" Original Book Created:"
);
printBookDetails
(
originalBook
,
" "
);
// 🔹 الخطوة 2: الترميز إلى مصفوفة بايتات
byte
[]
encodedData
=
originalBook
.
toByteArray
();
System
.
out
.
println
(
" Encoding to byte array..."
);
System
.
out
.
println
(
" Encoded size: "
+
encodedData
.
length
+
" bytes"
);
System
.
out
.
print
(
" Byte array: "
);
printByteArrayPreview
(
encodedData
);
// 🔹 الخطوة 3: فك التشفير من مصفوفة البايتات
System
.
out
.
println
(
" Decoding from byte array..."
);
Book
decodedBook
=
Book
.
parseFrom
(
encodedData
);
System
.
out
.
println
(
" Decoded Book:"
);
printBookDetails
(
decodedBook
,
" "
);
// 🔹 الخطوة 4: التحقق من التكافؤ
System
.
out
.
println
(
" Verification:"
);
System
.
out
.
println
(
" Titles match: "
+
originalBook
.
getTitle
().
equals
(
decodedBook
.
getTitle
()));
System
.
out
.
println
(
" Authors match: "
+
originalBook
.
getAuthor
().
getName
().
equals
(
decodedBook
.
getAuthor
().
getName
()));
System
.
out
.
println
(
" Years match: "
+
(
originalBook
.
getYear
()
==
decodedBook
.
getYear
()));
System
.
out
.
println
(
" Full objects equal: "
+
originalBook
.
equals
(
decodedBook
));
}
catch
(
InvalidProtocolBufferException
e
)
{
System
.
err
.
println
(
" Error in encoding/decoding: "
+
e
.
getMessage
());
}
System
.
out
.
println
();
System
.
out
.
println
(
"Total bytes: "
+
msg
.
length
);
}
// Deserialize
var
library
=
Library
.
parseFrom
(
msg
);
System
.
out
.
println
(
"Library contents:"
);
System
.
out
.
println
(
library
.
getBooksList
());
private
static
void
encodeDecodeLibrary
()
{
System
.
out
.
println
(
"2. Library (Book Collection) Encoding & Decoding:"
);
// Display detailed book information
System
.
out
.
println
(
"\nDetailed Book Information:"
);
for
(
Book
book
:
library
.
getBooksList
())
{
System
.
out
.
println
(
"Title: "
+
book
.
getTitle
());
System
.
out
.
println
(
"Author: "
+
book
.
getAuthor
().
getName
()
+
" from "
+
book
.
getAuthor
().
getCountry
());
System
.
out
.
println
(
"ISBN: "
+
book
.
getIsbn
());
System
.
out
.
println
(
"Year: "
+
book
.
getYear
());
System
.
out
.
println
(
"Type: "
+
book
.
getType
());
System
.
out
.
println
(
"Categories: "
+
book
.
getCategoriesList
());
System
.
out
.
println
(
"Available: "
+
book
.
getAvailable
());
System
.
out
.
println
(
"---"
);
try
{
// 🔹 الخطوة 1: إنشاء مكتبة متعددة الكتب
Book
book1
=
Book
.
newBuilder
()
.
setTitle
(
"Clean Code: A Handbook of Agile Software Craftsmanship"
)
.
setAuthor
(
Author
.
newBuilder
()
.
setName
(
"Robert C. Martin"
)
.
setCountry
(
"USA"
)
.
build
())
.
setIsbn
(
"978-0132350884"
)
.
setYear
(
2008
)
.
setType
(
BookType
.
TECHNICAL
)
.
addCategories
(
"Software Engineering"
)
.
addCategories
(
"Programming"
)
.
setAvailable
(
true
)
.
build
();
Book
book2
=
Book
.
newBuilder
()
.
setTitle
(
"The Pragmatic Programmer: Your Journey to Mastery"
)
.
setAuthor
(
Author
.
newBuilder
()
.
setName
(
"Andrew Hunt"
)
.
setCountry
(
"USA"
)
.
build
())
.
setIsbn
(
"978-0135957059"
)
.
setYear
(
2019
)
.
setType
(
BookType
.
TECHNICAL
)
.
addCategories
(
"Programming"
)
.
addCategories
(
"Career Development"
)
.
setAvailable
(
true
)
.
build
();
Book
book3
=
Book
.
newBuilder
()
.
setTitle
(
"Domain-Driven Design: Tackling Complexity in the Heart of Software"
)
.
setAuthor
(
Author
.
newBuilder
()
.
setName
(
"Eric Evans"
)
.
setCountry
(
"USA"
)
.
build
())
.
setIsbn
(
"978-0321125217"
)
.
setYear
(
2003
)
.
setType
(
BookType
.
TECHNICAL
)
.
addCategories
(
"Software Design"
)
.
addCategories
(
"Architecture"
)
.
setAvailable
(
false
)
.
build
();
Library
originalLibrary
=
Library
.
newBuilder
()
.
addBooks
(
book1
)
.
addBooks
(
book2
)
.
addBooks
(
book3
)
.
build
();
System
.
out
.
println
(
" Original Library Created:"
);
System
.
out
.
println
(
" Total books: "
+
originalLibrary
.
getBooksCount
());
for
(
int
i
=
0
;
i
<
originalLibrary
.
getBooksCount
();
i
++)
{
System
.
out
.
println
(
" "
+
(
i
+
1
)
+
". "
+
originalLibrary
.
getBooks
(
i
).
getTitle
());
}
// 🔹 الخطوة 2: الترميز
byte
[]
encodedLibrary
=
originalLibrary
.
toByteArray
();
System
.
out
.
println
(
" Encoding library to byte array..."
);
System
.
out
.
println
(
" Encoded size: "
+
encodedLibrary
.
length
+
" bytes"
);
System
.
out
.
print
(
" Byte array preview: "
);
printByteArrayPreview
(
encodedLibrary
);
// 🔹 الخطوة 3: فك التشفير
System
.
out
.
println
(
" Decoding library from byte array..."
);
Library
decodedLibrary
=
Library
.
parseFrom
(
encodedLibrary
);
System
.
out
.
println
(
" Decoded Library:"
);
System
.
out
.
println
(
" Total books: "
+
decodedLibrary
.
getBooksCount
());
for
(
int
i
=
0
;
i
<
decodedLibrary
.
getBooksCount
();
i
++)
{
Book
book
=
decodedLibrary
.
getBooks
(
i
);
System
.
out
.
println
(
" "
+
(
i
+
1
)
+
". "
+
book
.
getTitle
()
+
" by "
+
book
.
getAuthor
().
getName
()
+
" ("
+
book
.
getYear
()
+
")"
);
}
// 🔹 الخطوة 4: مقارنة الكفاءة مع JSON نظرياً
String
jsonEquivalent
=
createJsonEquivalent
(
originalLibrary
);
System
.
out
.
println
(
" Efficiency Comparison:"
);
System
.
out
.
println
(
" Protobuf size: "
+
encodedLibrary
.
length
+
" bytes"
);
System
.
out
.
println
(
" Estimated JSON size: "
+
jsonEquivalent
.
getBytes
().
length
+
" bytes"
);
int
saving
=
((
jsonEquivalent
.
getBytes
().
length
-
encodedLibrary
.
length
)
*
100
)
/
jsonEquivalent
.
getBytes
().
length
;
System
.
out
.
println
(
" Size reduction: "
+
saving
+
"%"
);
}
catch
(
InvalidProtocolBufferException
e
)
{
System
.
err
.
println
(
" Error in library encoding/decoding: "
+
e
.
getMessage
());
}
}
private
static
byte
[]
sender
()
{
// Create first book
Book
book1
=
Book
.
newBuilder
()
.
setTitle
(
"Protocol Buffers in Distributed Systems"
)
.
setAuthor
(
Author
.
newBuilder
()
.
setName
(
"Ahmed Networks"
)
.
setCountry
(
"Egypt"
)
.
build
())
.
setIsbn
(
"978-1234567890"
)
.
setYear
(
2024
)
.
setType
(
BookType
.
TECHNICAL
)
.
addCategories
(
"Networks"
)
.
addCategories
(
"Programming"
)
.
setAvailable
(
true
)
.
build
();
// Create second book
Book
book2
=
Book
.
newBuilder
()
.
setTitle
(
"gRPC Fundamentals and Communications"
)
.
setAuthor
(
Author
.
newBuilder
()
.
setName
(
"Mohamed Developer"
)
.
setCountry
(
"Saudi Arabia"
)
.
build
())
.
setIsbn
(
"978-0987654321"
)
.
setYear
(
2023
)
.
setType
(
BookType
.
SCIENCE
)
.
addCategories
(
"Communications"
)
.
addCategories
(
"API"
)
.
setAvailable
(
true
)
.
build
();
// Create library and add books
Library
library
=
Library
.
newBuilder
()
.
addBooks
(
book1
)
.
addBooks
(
book2
)
.
build
();
// Serialize to byte array
byte
[]
arrayToSend
=
library
.
toByteArray
();
// Display sent data
System
.
out
.
println
(
" Sent Data:"
);
for
(
int
i
=
0
;
i
<
arrayToSend
.
length
;
i
++)
{
System
.
out
.
print
(
arrayToSend
[
i
]
+
" "
);
private
static
void
encodeDecodeServiceMessages
()
{
System
.
out
.
println
(
"3. Service Messages Encoding & Decoding:"
);
try
{
// 🔹 ترميز وفك تشفير طلب إضافة كتاب
Book
newBook
=
Book
.
newBuilder
()
.
setTitle
(
"gRPC: Up and Running"
)
.
setAuthor
(
Author
.
newBuilder
()
.
setName
(
"Kasun Indrasiri"
)
.
setCountry
(
"Sri Lanka"
)
.
build
())
.
setIsbn
(
"978-1492058330"
)
.
setYear
(
2020
)
.
setType
(
BookType
.
TECHNICAL
)
.
addCategories
(
"gRPC"
)
.
addCategories
(
"Microservices"
)
.
setAvailable
(
true
)
.
build
();
AddBookRequest
addRequest
=
AddBookRequest
.
newBuilder
()
.
setBook
(
newBook
)
.
build
();
System
.
out
.
println
(
" AddBookRequest Encoding:"
);
System
.
out
.
println
(
" Book: "
+
addRequest
.
getBook
().
getTitle
());
byte
[]
encodedRequest
=
addRequest
.
toByteArray
();
System
.
out
.
println
(
" Encoded size: "
+
encodedRequest
.
length
+
" bytes"
);
AddBookRequest
decodedRequest
=
AddBookRequest
.
parseFrom
(
encodedRequest
);
System
.
out
.
println
(
" Decoded book: "
+
decodedRequest
.
getBook
().
getTitle
());
// 🔹 ترميز وفك تشفير استجابة إضافة كتاب
AddBookResponse
addResponse
=
AddBookResponse
.
newBuilder
()
.
setSuccess
(
true
)
.
setMessage
(
"Book added successfully to library database"
)
.
setBookId
(
"LIB-2024-001"
)
.
build
();
System
.
out
.
println
(
" AddBookResponse Encoding:"
);
System
.
out
.
println
(
" Success: "
+
addResponse
.
getSuccess
());
System
.
out
.
println
(
" Message: "
+
addResponse
.
getMessage
());
byte
[]
encodedResponse
=
addResponse
.
toByteArray
();
System
.
out
.
println
(
" Encoded size: "
+
encodedResponse
.
length
+
" bytes"
);
AddBookResponse
decodedResponse
=
AddBookResponse
.
parseFrom
(
encodedResponse
);
System
.
out
.
println
(
" Decoded success: "
+
decodedResponse
.
getSuccess
());
System
.
out
.
println
(
" Decoded book ID: "
+
decodedResponse
.
getBookId
());
// 🔹 ترميز وفك تشفير طلب البحث
GetBookRequest
searchRequest
=
GetBookRequest
.
newBuilder
()
.
setIsbn
(
"978-1492058330"
)
.
build
();
System
.
out
.
println
(
" GetBookRequest Encoding:"
);
System
.
out
.
println
(
" ISBN: "
+
searchRequest
.
getIsbn
());
byte
[]
encodedSearch
=
searchRequest
.
toByteArray
();
System
.
out
.
println
(
" Encoded size: "
+
encodedSearch
.
length
+
" bytes"
);
System
.
out
.
print
(
" Byte array: "
);
printByteArrayPreview
(
encodedSearch
);
GetBookRequest
decodedSearch
=
GetBookRequest
.
parseFrom
(
encodedSearch
);
System
.
out
.
println
(
" Decoded ISBN: "
+
decodedSearch
.
getIsbn
());
}
catch
(
InvalidProtocolBufferException
e
)
{
System
.
err
.
println
(
" Error in service messages encoding/decoding: "
+
e
.
getMessage
());
}
}
// ========== Methods ==========
private
static
void
printBookDetails
(
Book
book
,
String
indent
)
{
System
.
out
.
println
(
indent
+
"Title: "
+
book
.
getTitle
());
System
.
out
.
println
(
indent
+
"Author: "
+
book
.
getAuthor
().
getName
()
+
" ("
+
book
.
getAuthor
().
getCountry
()
+
")"
);
System
.
out
.
println
(
indent
+
"ISBN: "
+
book
.
getIsbn
());
System
.
out
.
println
(
indent
+
"Year: "
+
book
.
getYear
());
System
.
out
.
println
(
indent
+
"Type: "
+
book
.
getType
());
System
.
out
.
println
(
indent
+
"Categories: "
+
book
.
getCategoriesList
());
System
.
out
.
println
(
indent
+
"Available: "
+
book
.
getAvailable
());
}
private
static
void
printByteArrayPreview
(
byte
[]
data
)
{
int
previewLength
=
Math
.
min
(
data
.
length
,
15
);
for
(
int
i
=
0
;
i
<
previewLength
;
i
++)
{
System
.
out
.
print
(
data
[
i
]
+
" "
);
}
if
(
data
.
length
>
previewLength
)
{
System
.
out
.
print
(
"... (+"
+
(
data
.
length
-
previewLength
)
+
" more bytes)"
);
}
System
.
out
.
println
();
System
.
out
.
println
(
" Total bytes to send: "
+
arrayToSend
.
length
);
}
return
arrayToSend
;
private
static
String
createJsonEquivalent
(
Library
library
)
{
// محاكاة تقريبية لحجم JSON
StringBuilder
json
=
new
StringBuilder
(
"["
);
for
(
Book
book
:
library
.
getBooksList
())
{
if
(
json
.
length
()
>
1
)
json
.
append
(
","
);
json
.
append
(
"{"
)
.
append
(
"\"title\":\""
).
append
(
book
.
getTitle
()).
append
(
"\","
)
.
append
(
"\"author\":{"
)
.
append
(
"\"name\":\""
).
append
(
book
.
getAuthor
().
getName
()).
append
(
"\","
)
.
append
(
"\"country\":\""
).
append
(
book
.
getAuthor
().
getCountry
()).
append
(
"\""
)
.
append
(
"},"
)
.
append
(
"\"isbn\":\""
).
append
(
book
.
getIsbn
()).
append
(
"\","
)
.
append
(
"\"year\":"
).
append
(
book
.
getYear
()).
append
(
","
)
.
append
(
"\"type\":\""
).
append
(
book
.
getType
()).
append
(
"\","
)
.
append
(
"\"categories\":"
).
append
(
book
.
getCategoriesList
().
toString
()).
append
(
","
)
.
append
(
"\"available\":"
).
append
(
book
.
getAvailable
())
.
append
(
"}"
);
}
json
.
append
(
"]"
);
return
json
.
toString
();
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment