Skip to content

Feature/byte based messages#11

Merged
ofek29 merged 4 commits intomainfrom
feature/byte-based-messages
Aug 24, 2025
Merged

Feature/byte based messages#11
ofek29 merged 4 commits intomainfrom
feature/byte-based-messages

Conversation

@ofek29
Copy link
Owner

@ofek29 ofek29 commented Jul 21, 2025

This pull request refactors the Message and MessageQueue classes to improve payload handling by switching from String to byte[] for payload storage. It also updates serialization to use Base64 encoding for safer storage and modifies tests to reflect these changes.

Core Refactoring and Enhancements:

  • Payload storage refactor: Changed Message class to store payloads as byte[] instead of String, added helper methods for encoding/decoding payloads as strings, and removed the id field for simplicity. (src/main/java/com/ofek/queue/Message.java)
  • Base64 encoding for persistence: Updated MessageQueue to encode message payloads as Base64 when saving to files and decode them when loading. (src/main/java/com/ofek/queue/MessageQueue.java) [1] [2]

API Changes:

  • New overload for produce: Added a method in Producer to accept byte[] payloads directly. (src/main/java/com/ofek/queue/Producer.java)

Test Updates:

  • Test compatibility: Updated all test cases to use getPayloadAsString() for assertions instead of directly accessing getPayload(). (src/test/java/com/ofek/queue/ConsumerTest.java, src/test/java/com/ofek/queue/IntegrationTest.java, src/test/java/com/ofek/queue/MessageQueueTest.java) [1] [2] [3]
  • File format validation: Modified tests to decode Base64-encoded payloads when verifying file persistence. (src/test/java/com/ofek/queue/MessageQueueTest.java) [1] [2] [3]

Cleanup:

  • Removed message ID: Dropped the id field and related logic from Message and associated tests, simplifying the class. (src/main/java/com/ofek/queue/Message.java, src/test/java/com/ofek/queue/MessageQueueTest.java) [1] [2]

These changes improve the flexibility of payload handling, ensure safe storage of binary data, and streamline the codebase.

@ofek29 ofek29 requested review from avifenesh and Copilot July 21, 2025 21:36
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request refactors the message queue system to use byte arrays instead of strings for payload storage, enabling better handling of binary data. The changes also remove message ID functionality for simplification and implement Base64 encoding for file persistence.

Key changes:

  • Refactored Message class to store payloads as byte[] with helper methods for string conversion
  • Updated serialization to use Base64 encoding for safe file storage of binary data
  • Removed message ID field and related functionality from the entire codebase

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/main/java/com/ofek/queue/Message.java Core refactor to byte array storage, removed ID field, added string conversion methods
src/main/java/com/ofek/queue/MessageQueue.java Updated file persistence to use Base64 encoding/decoding for binary data
src/main/java/com/ofek/queue/Producer.java Added overload method to accept byte array payloads directly
src/test/java/com/ofek/queue/MessageTest.java Comprehensive test updates for byte array functionality and removed ID-related tests
src/test/java/com/ofek/queue/MessageQueueTest.java Updated assertions to use getPayloadAsString() and Base64 decoding for file validation
src/test/java/com/ofek/queue/ProducerTest.java Updated test assertions and removed ID uniqueness tests
src/test/java/com/ofek/queue/ConsumerTest.java Updated all payload assertions to use new string conversion method
src/test/java/com/ofek/queue/IntegrationTest.java Updated integration test assertions for new payload access pattern

Message message = new Message(payload);
queue.offer(message);
loadedCount++;
}
Copy link

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an unmatched closing brace. The if statement on line 189 that checks parts.length == 2 was removed, but its closing brace remains, creating a syntax error.

Copilot uses AI. Check for mistakes.
String line = lines.get(i);
assertTrue(line.contains("Batch message " + i));
assertTrue(line.contains("|")); // ID|payload format
String decodedMessage = new String(Base64.getDecoder().decode(lines.get(i)));
Copy link

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Base64 decoding should specify the character encoding explicitly. Use new String(Base64.getDecoder().decode(lines.get(i)), StandardCharsets.UTF_8) to ensure consistent encoding behavior across different platforms.

Copilot uses AI. Check for mistakes.
for (int i = 0; i < messageCount; i++) {
String line = lines.get(i);
assertTrue(line.contains("Small batch message " + i));
String decodedMessage = new String(Base64.getDecoder().decode(line));
Copy link

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Base64 decoding should specify the character encoding explicitly. Use new String(Base64.getDecoder().decode(line), StandardCharsets.UTF_8) to ensure consistent encoding behavior across different platforms.

Suggested change
String decodedMessage = new String(Base64.getDecoder().decode(line));
String decodedMessage = new String(Base64.getDecoder().decode(line), StandardCharsets.UTF_8);

Copilot uses AI. Check for mistakes.
for (int i = 0; i < messageCount; i++) {
String line = lines.get(i);
assertTrue(line.contains("Large batch message " + i));
String decodedMessage = new String(Base64.getDecoder().decode(line));
Copy link

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Base64 decoding should specify the character encoding explicitly. Use new String(Base64.getDecoder().decode(line), StandardCharsets.UTF_8) to ensure consistent encoding behavior across different platforms.

Suggested change
String decodedMessage = new String(Base64.getDecoder().decode(line));
String decodedMessage = new String(Base64.getDecoder().decode(line), StandardCharsets.UTF_8);

Copilot uses AI. Check for mistakes.
List<String> lines = Files.readAllLines(testFile);
assertEquals(1, lines.size());
assertTrue(lines.get(0).contains("Before shutdown"));
String decodedMessage = new String(Base64.getDecoder().decode(lines.get(0)));
Copy link

Copilot AI Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Base64 decoding should specify the character encoding explicitly. Use new String(Base64.getDecoder().decode(lines.get(0)), StandardCharsets.UTF_8) to ensure consistent encoding behavior across different platforms.

Suggested change
String decodedMessage = new String(Base64.getDecoder().decode(lines.get(0)));
String decodedMessage = new String(Base64.getDecoder().decode(lines.get(0)), StandardCharsets.UTF_8);

Copilot uses AI. Check for mistakes.
@ofek29 ofek29 merged commit d3bdf9b into main Aug 24, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant