title: CipherOutputStream not working
body: <p>I have a problem with the following code.</p>
  
  <p>If I use the ObjectOutputStream everything runs fine but when I try to use the CipherOutputStream I get the following error in the server side.</p>
  
  <p>If I send one file I don't receive it fully.
  If I send more than one file the last one to be sended is not received fully but the previous are.</p>
  
  <p>What could it be?</p>
  
  <p>I have seen other posts but they didn't help me.</p>
  
  <pre><code>java.lang.ArrayIndexOutOfBoundsException
      at java.lang.System.arraycopy(Native Method)
      at java.io.BufferedOutputStream.write(BufferedOutputStream.java:128)
      at myCloudServer$myCloudServerThread.run(myCloudServer.java:257)
  </code></pre>
  
  <p>Client code. Sending a file.</p>
  
  <pre><code>SocketFactory socketFactory = SSLSocketFactory.getDefault();
  clientSocket = socketFactory.createSocket(serverAddress[0], Integer.parseInt(serverAddress[1]));
  
  ObjectInputStream objectInputStream = new ObjectInputStream(clientSocket.getInputStream());
  ObjectOutputStream objectOutputStream = new ObjectOutputStream(clientSocket.getOutputStream());
  
  [...]
  
  byte[] buffer = new byte[1024];
  objectOutputStream.writeObject(sendingClientFiles.size());
  
  for (File sendingClientFile : sendingClientFiles) {
      [...]
  
      KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
      keyGenerator.init(128);
      SecretKey secretKeyEncrypt = keyGenerator.generateKey();    
  
      Cipher cipherEncrypt = Cipher.getInstance("AES");   
      cipherEncrypt.init(Cipher.ENCRYPT_MODE, secretKeyEncrypt);
      CipherOutputStream cipherOutputStream = new CipherOutputStream(objectOutputStream, cipherEncrypt);    
  
      BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(sendingClientFile));
      int count = 0;
      while ((count = bufferedInputStream.read(buffer, 0, buffer.length)) != -1) {
          //objectOutputStream.write(buffer, 0, count);
          cipherOutputStream.write(buffer, 0, count);
      }
  
      bufferedInputStream.close();
      //objectOutputStream.flush();
      cipherOutputStream.flush();
  }
  </code></pre>
  
  <p>Server code. Receiving a file.</p>
  
  <pre><code>ServerSocketFactory serverSocketFactory = SSLServerSocketFactory.getDefault();
  serverSocket = serverSocketFactory.createServerSocket(port);
  
  Socket clientSocket = serverSocket.accept();
  
  ObjectOutputStream objectOutputStream = new ObjectOutputStream(clientSocket.getOutputStream());
  ObjectInputStream  objectInputStream  = new ObjectInputStream(clientSocket.getInputStream());
  
  [...]
  
  byte[] buffer = new byte[1024];
  int i = 0;
  int receivingClientFiles = (int) objectInputStream.readObject();
  
  while(i &lt; receivingClientFiles) {
      [...]
  
      BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(diretorio + "/" + fileName));
      int count = 0;                      
      int received = 0;
      while (received &lt; fileLength) {
          count = objectInputStream.read(buffer, 0,  buffer.length);
          bufferedOutputStream.write(buffer, 0, count);
          received += count;
      }
      //while ((count = objectInputStream.read(buffer, 0, buffer.length)) != -1) {
      //  bufferedOutputStream.write(buffer, 0, count);
      //}
  
      bufferedOutputStream.close();
  
      i++;
  }
  </code></pre>