Use bytes instead of chars

This commit is contained in:
MrLetsplay 2024-05-22 15:29:53 +02:00
parent f82812577e
commit 5634a1e555
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
7 changed files with 53 additions and 24 deletions

View File

@ -1,5 +1,6 @@
package me.mrletsplay.shareclientcore.document; package me.mrletsplay.shareclientcore.document;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -51,10 +52,17 @@ public class ArrayCharBag implements CharBag {
} }
@Override @Override
public String toString() { public byte[] getContents() {
return chars.stream() byte[] bytes = new byte[chars.size()];
.map(c -> c.value()) for(int i = 0; i < bytes.length; i++) {
.reduce(new StringBuilder(), (b, s) -> b.append(s), (a, b) -> a.append(b)).toString(); bytes[i] = chars.get(i).value();
}
return bytes;
}
@Override
public String getContentsAsString() {
return new String(getContents(), StandardCharsets.UTF_8);
} }
} }

View File

@ -8,10 +8,10 @@ import java.util.Objects;
import me.mrletsplay.shareclientcore.connection.SerializableObject; import me.mrletsplay.shareclientcore.connection.SerializableObject;
public record Char(Identifier[] position, int lamport, char value) implements SerializableObject { public record Char(Identifier[] position, int lamport, byte value) implements SerializableObject {
public static final Char START_OF_DOCUMENT = new Char(new Identifier[] { new Identifier(1, Identifier.DEFAULT_SITE) }, 0, '^'); public static final Char START_OF_DOCUMENT = new Char(new Identifier[] { new Identifier(1, Identifier.DEFAULT_SITE) }, 0, (byte) '^');
public static final Char END_OF_DOCUMENT = new Char(new Identifier[] { new Identifier(Util.BASE - 1, Identifier.DEFAULT_SITE) }, 0, '$'); public static final Char END_OF_DOCUMENT = new Char(new Identifier[] { new Identifier(Util.BASE - 1, Identifier.DEFAULT_SITE) }, 0, (byte) '$');
@Override @Override
public int hashCode() { public int hashCode() {
@ -44,7 +44,7 @@ public record Char(Identifier[] position, int lamport, char value) implements Se
} }
out.writeInt(lamport); out.writeInt(lamport);
out.writeChar(value); out.writeByte(value);
} }
public static Char deserialize(DataInputStream in) throws IOException { public static Char deserialize(DataInputStream in) throws IOException {
@ -54,7 +54,7 @@ public record Char(Identifier[] position, int lamport, char value) implements Se
} }
int lamport = in.readInt(); int lamport = in.readInt();
char value = in.readChar(); byte value = in.readByte();
return new Char(pos, lamport, value); return new Char(pos, lamport, value);
} }

View File

@ -1,5 +1,6 @@
package me.mrletsplay.shareclientcore.document; package me.mrletsplay.shareclientcore.document;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
public interface CharBag { public interface CharBag {
@ -38,10 +39,16 @@ public interface CharBag {
public List<Char> toList(); public List<Char> toList();
/** /**
* Collects the chars in this bag ordered by their position into a string * Collects the chars in this bag ordered by their position into a byte array
* @return A string containing the chars * @return A byte array containing the contents of this bag
*/ */
@Override public byte[] getContents();
public String toString();
/**
* Collects the chars in this bag ordered by their position into a string using the {@link StandardCharsets#UTF_8 UTF-8} charset.<br>
* Note: This will not work properly for binary contents, make sure to only call this on bags containing text!
* @return A string containing the contents of this bag
*/
public String getContentsAsString();
} }

View File

@ -5,7 +5,7 @@ package me.mrletsplay.shareclientcore.document;
*/ */
public interface DocumentListener { public interface DocumentListener {
public void onInsert(int index, char character); public void onInsert(int index, byte b);
public void onDelete(int index); public void onDelete(int index);

View File

@ -1,5 +1,6 @@
package me.mrletsplay.shareclientcore.document; package me.mrletsplay.shareclientcore.document;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -46,7 +47,7 @@ public class SharedDocument implements MessageListener {
Char charBefore = charBag.get(index); Char charBefore = charBag.get(index);
Char charAfter = charBag.get(index +1); Char charAfter = charBag.get(index +1);
char[] chars = str.toCharArray(); byte[] chars = str.getBytes(StandardCharsets.UTF_8);
Change[] changes = new Change[chars.length]; Change[] changes = new Change[chars.length];
for(int i = 0; i < chars.length; i++) { for(int i = 0; i < chars.length; i++) {
Identifier[] newPos = Util.generatePositionBetween(charBefore.position(), charAfter.position(), site); Identifier[] newPos = Util.generatePositionBetween(charBefore.position(), charAfter.position(), site);
@ -144,8 +145,21 @@ public class SharedDocument implements MessageListener {
return chars.subList(1, chars.size() - 1); return chars.subList(1, chars.size() - 1);
} }
public String getContents() { /**
String contents = charBag.toString(); * @return The contents of the document
* @see CharBag#getContents()
*/
public byte[] getContents() {
byte[] contents = charBag.getContents();
return Arrays.copyOfRange(contents, 1, contents.length - 1);
}
/**
* @return The contents of the document converted to a UTF-8 encoded string
* @see CharBag#getContentsAsString()
*/
public String getContentsAsString() {
String contents = charBag.getContentsAsString();
return contents.substring(1, contents.length() - 1); return contents.substring(1, contents.length() - 1);
} }

View File

@ -14,11 +14,11 @@ public class DocumentTest {
public void testLocalInsert() { public void testLocalInsert() {
SharedDocument doc = new SharedDocument(new DummyConnection(), "test"); SharedDocument doc = new SharedDocument(new DummyConnection(), "test");
doc.localInsert(0, "Hello"); doc.localInsert(0, "Hello");
assertEquals("Hello", doc.getContents()); assertEquals("Hello", doc.getContentsAsString());
doc.localInsert(5, " World"); doc.localInsert(5, " World");
assertEquals("Hello World", doc.getContents()); assertEquals("Hello World", doc.getContentsAsString());
doc.localInsert(5, " Test"); doc.localInsert(5, " Test");
assertEquals("Hello Test World", doc.getContents()); assertEquals("Hello Test World", doc.getContentsAsString());
} }
@Test @Test
@ -34,7 +34,7 @@ public class DocumentTest {
SharedDocument doc = new SharedDocument(new DummyConnection(), "test"); SharedDocument doc = new SharedDocument(new DummyConnection(), "test");
doc.localInsert(0, "Hello World!"); doc.localInsert(0, "Hello World!");
doc.localDelete(5, 6); doc.localDelete(5, 6);
assertEquals("Hello!", doc.getContents()); assertEquals("Hello!", doc.getContentsAsString());
} }
@Test @Test

View File

@ -42,7 +42,7 @@ public class MessageTest {
@Test @Test
public void testChangeMessage() throws IOException { public void testChangeMessage() throws IOException {
Change change = new Change("Project:src/test.txt", ChangeType.ADD, new Char(new Identifier[] {new Identifier(0, 1), new Identifier(1, 3)}, 42, 'e')); Change change = new Change("Project:src/test.txt", ChangeType.ADD, new Char(new Identifier[] {new Identifier(0, 1), new Identifier(1, 3)}, 42, (byte) 'e'));
ChangeMessage m = new ChangeMessage(change); ChangeMessage m = new ChangeMessage(change);
assertEquals(deserialize(serialize(m)), m, "Deserialized message must equal message"); assertEquals(deserialize(serialize(m)), m, "Deserialized message must equal message");
} }
@ -80,8 +80,8 @@ public class MessageTest {
@Test @Test
public void testFullSyncMessage() throws IOException { public void testFullSyncMessage() throws IOException {
List<Char> chars = Arrays.asList( List<Char> chars = Arrays.asList(
new Char(new Identifier[] {new Identifier(0, 1), new Identifier(1, 3)}, 42, 'e'), new Char(new Identifier[] {new Identifier(0, 1), new Identifier(1, 3)}, 42, (byte) 'e'),
new Char(new Identifier[] {new Identifier(0, 1), new Identifier(1, 3), new Identifier(1, 4)}, 333, 'f') new Char(new Identifier[] {new Identifier(0, 1), new Identifier(1, 3), new Identifier(1, 4)}, 333, (byte) 'f')
); );
FullSyncMessage m = new FullSyncMessage(2, "Project:src/test.txt", chars); FullSyncMessage m = new FullSyncMessage(2, "Project:src/test.txt", chars);
assertEquals(deserialize(serialize(m)), m, "Deserialized message must equal message"); assertEquals(deserialize(serialize(m)), m, "Deserialized message must equal message");