I'm still working on my push server! I have successfully implemented encryption using javax.crypto.cipher. This requires I read/write bytes to the socket's stream. I can send and receive just fine. Encryption works. But when there's nothing going over the air, the server throws an OutOfMemoryException. The readBytes function is:
public static byte[] readBytes() throws IOException {
int len = dis.readInt();
byte[] data = new byte[len];
if (len > 0) {
dis.readFully(data);
}
return data;
}
The code that calls this is:
public String read() {
byte[] encrypted,decrypted = null;
try {
encrypted = readBytes();
if (encrypted.equals(new Byte[] {0})) return "";
if (encrypted.equals(null)) return null;
cipher.init(Cipher.DECRYPT_MODE, key);
decrypted = cipher.doFinal(encrypted);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String afterEncryption = new String(decrypted);
return afterEncryption;
}
And read() is called in a while-loop:
while (true&loggedin&((inputLine=read())!=null)) {
And the exception that is thrown is:
Exception in thread "ServerThread" java.lang.OutOfMemoryError: Java heap space
at ServerThread.readBytes(ServerThread.java:277)
at ServerThread.read(ServerThread.java:245)
at ServerThread.run(ServerThread.java:108)
Line 277 being the one that declares the byte array. The function that sends the byte array is:
public static void sendBytes(byte[] myByteArray, int start, int len) throws IOException {
if (len < 0)
throw new IllegalArgumentException("Negative length not allowed");
if (start < 0 || start >= myByteArray.length)
throw new IndexOutOfBoundsException("Out of bounds: " + start);
if (len > 0) {
dos.writeInt(len);
dos.write(myByteArray, start, len);
}
}
sendBytes is only run when read() stops blocking. That's how the loop is designed to work.
If this code looks familiar, its because I found it on stack overflow!
The handshake works flawlessly, but as soon as it stops sending things, I get the exception about five seconds later.
Thank you for any help you can give me.