// Constructor for inputstream
public SimulatorInput ( JTextField inputField, JButton ... buttons ) {
this.inputField = inputField ;
this.byteStream = ByteBuffer.allocate ( BUFFERSIZE ) ;
}
// Adds the chars to the buffer
public void post ( ) {
synchronized ( this.byteStream ) {
this.byteStream.put ( ( this.inputField.getText ( ) + "\r\n" ).getBytes ( ) ) ;
this.inputField.setText ( "" ) ;
this.byteStream.notifyAll ( ) ;
}
}
//read...
@Override public int read ( ) throws IOException {
synchronized ( this.byteStream ) {
while ( !this.byteStream.hasRemaining ( ) ) {
try {
this.byteStream.wait ( ) ;
} catch ( InterruptedException e ) {
throw new IOException ( e ) ;
}
}
return this.byteStream.get ( ) ;
}
}
//the error
Exception in thread "AWT-EventQueue-0" java.nio.BufferOverflowException
at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:165)
at java.nio.ByteBuffer.put(ByteBuffer.java:813)
at simulator.gui.SimulatorInput.post(SimulatorInput.java:44)
at simulator.gui.SimulatorConsole$Listener.keyTyped(SimulatorConsole.java:60)
at java.awt.Component.processKeyEvent(Component.java:5993)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2799)
at java.awt.Component.processEvent(Component.java:5815)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:697)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:962)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:834)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:661)
at java.awt.Component.dispatchEventImpl(Component.java:4282)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)