Note that there are some explanatory texts on larger screens.

plurals
  1. POServer throwing OutOfMemory Exception when reading byte array from TCP connection
    primarykey
    data
    text
    <p>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:</p> <pre><code>public static byte[] readBytes() throws IOException { int len = dis.readInt(); byte[] data = new byte[len]; if (len &gt; 0) { dis.readFully(data); } return data; } </code></pre> <p>The code that calls this is:</p> <pre><code> 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; } </code></pre> <p>And read() is called in a while-loop:</p> <pre><code>while (true&amp;loggedin&amp;((inputLine=read())!=null)) { </code></pre> <p>And the exception that is thrown is:</p> <pre><code>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) </code></pre> <p>Line 277 being the one that declares the byte array. The function that sends the byte array is:</p> <pre><code> public static void sendBytes(byte[] myByteArray, int start, int len) throws IOException { if (len &lt; 0) throw new IllegalArgumentException("Negative length not allowed"); if (start &lt; 0 || start &gt;= myByteArray.length) throw new IndexOutOfBoundsException("Out of bounds: " + start); if (len &gt; 0) { dos.writeInt(len); dos.write(myByteArray, start, len); } } </code></pre> <p>sendBytes is only run when read() stops blocking. That's how the loop is designed to work.</p> <p>If this code looks familiar, its because I found it on stack overflow!</p> <p>The handshake works flawlessly, but as soon as it stops sending things, I get the exception about five seconds later.</p> <p>Thank you for any help you can give me.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload