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
- 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;
{
}""";
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);
}
String uncompressed = output.toString();
System.out.printf("uncompressed size: %s bytes\n", uncompressed.length());