aspose file tools*
The moose likes I/O and Streams and the fly likes Interesting error with CipherInputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Interesting error with CipherInputStream" Watch "Interesting error with CipherInputStream" New topic
Author

Interesting error with CipherInputStream

Kevin Tibbs
Greenhorn

Joined: Apr 16, 2005
Posts: 5
I'm transmiting a file from one computer to another wth CipherInputStream.

However on the in.read(b), I get a NullPointerException error.

Is it possible that the encryption keys are off and so it's reading an encrypted stream that it doesn't know how to decrypt?

code segments:

Getting file:


Sending the File


[ April 17, 2005: Message edited by: Kevin Tibbs ]
[ April 17, 2005: Message edited by: Kevin Tibbs ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8867
    
    8

I'd like to see the exception stack trace.
What do you declare the byte array b to be? Do you allocate it?
read() returns the number of bytes read, or -1 if EOF. You should use this value to control the number of bytes you write out or you will be writing garbage if the array isn't full. You should also use this value to control your loop. b[0] isn't going to tell you anything.
See the javadoc for CypherInputStream for more info.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Kevin Tibbs
Greenhorn

Joined: Apr 16, 2005
Posts: 5
The code in that section has changed slightly (improved while loop control), and so has the Exception...

On the second pass through the loop the Fin.read throws an IndexOutOfBoundsException.



The stack trace (actual several...):
java.lang.IndexOutOfBoundsException
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:194)
at Client.sendFile(Client.java:63)
at FileXmit.actionPerformed(FileXmit.java:178)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1766)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
java.lang.IndexOutOfBoundsException
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:194)
at Client.sendFile(Client.java:63)
at FileXmit.actionPerformed(FileXmit.java:178)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1766)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

[ April 18, 2005: Message edited by: Kevin Tibbs ]
[ April 18, 2005: Message edited by: Kevin Tibbs ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8867
    
    8

In the method

the second argument is an offset into the first argument. The second time through this loop your offset is already outside the byte array and you get an index out of bounds exception.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interesting error with CipherInputStream