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
Show 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
;
package
org
.
example
;
import
com.google.protobuf.InvalidProtocolBufferException
;
import
com.google.protobuf.InvalidProtocolBufferException
;
import
org.ds.proto.Author
;
import
org.ds.proto.*
;
import
org.ds.proto.Book
;
import
org.ds.proto.BookType
;
import
java.util.Arrays
;
import
org.ds.proto.Library
;
public
class
Main
{
public
class
Main
{
public
static
void
main
(
String
[]
args
)
throws
InvalidProtocolBufferException
{
public
static
void
main
(
String
[]
args
)
{
byte
[]
msg
=
sender
();
System
.
out
.
println
(
"Protocol Buffers Encoding & Decoding Demo\n"
);
receiver
(
msg
);
}
// 1. ترميز وفك تشفير كتاب مفرد
encodeDecodeSingleBook
();
System
.
out
.
println
(
"\n"
+
"="
.
repeat
(
50
)
+
"\n"
);
// 2. ترميز وفك تشفير مكتبة (مجموعة كتب)
encodeDecodeLibrary
();
private
static
void
receiver
(
byte
[]
msg
)
throws
InvalidProtocolBufferException
{
System
.
out
.
println
(
"\n"
+
"="
.
repeat
(
50
)
+
"\n"
);
System
.
out
.
println
(
"Received Data:"
);
// Display binary data
// 3. ترميز وفك تشفير طلبات الخدمة
for
(
int
i
=
0
;
i
<
msg
.
length
;
i
++)
{
encodeDecodeServiceMessages
();
System
.
out
.
print
(
msg
[
i
]
+
" "
);
}
}
System
.
out
.
println
();
System
.
out
.
println
(
"Total bytes: "
+
msg
.
length
);
// Deserialize
private
static
void
encodeDecodeSingleBook
()
{
var
library
=
Library
.
parseFrom
(
msg
);
System
.
out
.
println
(
"1. Single Book Encoding & Decoding:"
);
System
.
out
.
println
(
"Library contents:"
);
System
.
out
.
println
(
library
.
getBooksList
());
// Display detailed book information
try
{
System
.
out
.
println
(
"\nDetailed Book Information:"
);
// 🔹 الخطوة 1: إنشاء كائن Book
for
(
Book
book
:
library
.
getBooksList
())
{
Book
originalBook
=
Book
.
newBuilder
()
System
.
out
.
println
(
"Title: "
+
book
.
getTitle
());
.
setTitle
(
"Designing Data-Intensive Applications"
)
System
.
out
.
println
(
"Author: "
+
book
.
getAuthor
().
getName
()
+
" from "
+
book
.
getAuthor
().
getCountry
());
.
setAuthor
(
Author
.
newBuilder
()
System
.
out
.
println
(
"ISBN: "
+
book
.
getIsbn
());
.
setName
(
"Martin Kleppmann"
)
System
.
out
.
println
(
"Year: "
+
book
.
getYear
());
.
setCountry
(
"United Kingdom"
)
System
.
out
.
println
(
"Type: "
+
book
.
getType
());
.
build
())
System
.
out
.
println
(
"Categories: "
+
book
.
getCategoriesList
());
.
setIsbn
(
"978-1449373320"
)
System
.
out
.
println
(
"Available: "
+
book
.
getAvailable
());
.
setYear
(
2017
)
System
.
out
.
println
(
"---"
);
.
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
());
}
}
}
}
private
static
byte
[]
sender
()
{
private
static
void
encodeDecodeLibrary
()
{
// Create first book
System
.
out
.
println
(
"2. Library (Book Collection) Encoding & Decoding:"
);
try
{
// 🔹 الخطوة 1: إنشاء مكتبة متعددة الكتب
Book
book1
=
Book
.
newBuilder
()
Book
book1
=
Book
.
newBuilder
()
.
setTitle
(
"Protocol Buffers in Distributed Systems
"
)
.
setTitle
(
"Clean Code: A Handbook of Agile Software Craftsmanship
"
)
.
setAuthor
(
Author
.
newBuilder
()
.
setAuthor
(
Author
.
newBuilder
()
.
setName
(
"Ahmed Networks
"
)
.
setName
(
"Robert C. Martin
"
)
.
setCountry
(
"Egypt
"
)
.
setCountry
(
"USA
"
)
.
build
())
.
build
())
.
setIsbn
(
"978-1234567890
"
)
.
setIsbn
(
"978-0132350884
"
)
.
setYear
(
2024
)
.
setYear
(
2008
)
.
setType
(
BookType
.
TECHNICAL
)
.
setType
(
BookType
.
TECHNICAL
)
.
addCategories
(
"Networks
"
)
.
addCategories
(
"Software Engineering
"
)
.
addCategories
(
"Programming"
)
.
addCategories
(
"Programming"
)
.
setAvailable
(
true
)
.
setAvailable
(
true
)
.
build
();
.
build
();
// Create second book
Book
book2
=
Book
.
newBuilder
()
Book
book2
=
Book
.
newBuilder
()
.
setTitle
(
"gRPC Fundamentals and Communications
"
)
.
setTitle
(
"The Pragmatic Programmer: Your Journey to Mastery
"
)
.
setAuthor
(
Author
.
newBuilder
()
.
setAuthor
(
Author
.
newBuilder
()
.
setName
(
"Mohamed Developer
"
)
.
setName
(
"Andrew Hunt
"
)
.
setCountry
(
"Saudi Arabia
"
)
.
setCountry
(
"USA
"
)
.
build
())
.
build
())
.
setIsbn
(
"978-0987654321
"
)
.
setIsbn
(
"978-0135957059
"
)
.
setYear
(
2023
)
.
setYear
(
2019
)
.
setType
(
BookType
.
SCIENCE
)
.
setType
(
BookType
.
TECHNICAL
)
.
addCategories
(
"Communications
"
)
.
addCategories
(
"Programming
"
)
.
addCategories
(
"API
"
)
.
addCategories
(
"Career Development
"
)
.
setAvailable
(
true
)
.
setAvailable
(
true
)
.
build
();
.
build
();
// Create library and add books
Book
book3
=
Book
.
newBuilder
()
Library
library
=
Library
.
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
(
book1
)
.
addBooks
(
book2
)
.
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
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
();
.
build
();
// Serialize to byte array
System
.
out
.
println
(
" AddBookResponse Encoding:"
);
byte
[]
arrayToSend
=
library
.
toByteArray
();
System
.
out
.
println
(
" Success: "
+
addResponse
.
getSuccess
());
System
.
out
.
println
(
" Message: "
+
addResponse
.
getMessage
());
// Display sent data
byte
[]
encodedResponse
=
addResponse
.
toByteArray
();
System
.
out
.
println
(
" Sent Data:"
);
System
.
out
.
println
(
" Encoded size: "
+
encodedResponse
.
length
+
" bytes"
);
for
(
int
i
=
0
;
i
<
arrayToSend
.
length
;
i
++)
{
System
.
out
.
print
(
arrayToSend
[
i
]
+
" "
);
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
();
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