for text based message system, if there's a limit of x KB, you can still transfer (~5 * x) KB by
- compressing the original payload
- encoding compressed binary to text
- sending it
receiver reverses the process to get original payload by
- testing to make sure message is encoded
- decoding it to compressed binary
- uncompressing decoded binary
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
String payload = """
{
}""";
System.out.printf("original size: %s bytes\n", payload.length());
{
}""";
System.out.printf("original size: %s bytes\n", payload.length());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gos = new GZIPOutputStream(baos);
gos.write(payload.getBytes());
gos.close();
byte[] compressed = baos.toByteArray();
System.out.printf("compressed size: %s bytes\n", compressed.length);
String encoded = Base64.encodeBase64String(compressed);
System.out.printf("encoded size: %s bytes\n", encoded.length());
System.out.println("is message encoded? " + Base64.isBase64(encoded));
byte[] decoded = Base64.decodeBase64(encoded.getBytes());
System.out.printf("decoded size: %s bytes\n", decoded.length);
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(decoded));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = gis.read(buffer)) > 0) {
output.write(buffer, 0, bytesRead);
}
gis.close();
String uncompressed = output.toString();
System.out.printf("uncompressed size: %s bytes\n", uncompressed.length());
String uncompressed = output.toString();
System.out.printf("uncompressed size: %s bytes\n", uncompressed.length());
original size: 9450 bytes
compressed size: 1235 bytes
encoded size: 1648 bytes
is message encoded? true
decoded size: 1235 bytes
uncompressed size: 9450 bytes
No comments:
Post a Comment