public synchronized static void write ( int fd , InputStream input ) throws IOException { // ensure bytes are available if ( input . available ( ) <= 0 ) { throw new IOException ( "No available bytes in input stream to write to serial port." ) ; } ByteArrayOutputStream buffer = new ByteArrayOutputStream ( ) ; int length ; byte [ ] data = new byte [ 1024 ] ; while ( ( length = input . read ( data , 0 , data . length ) ) != - 1 ) { buffer . write ( data , 0 , length ) ; } buffer . flush ( ) ; // write bytes to serial port write ( fd , buffer . toByteArray ( ) , buffer . size ( ) ) ; }
Read content from an input stream of data and write it to the serial port transmit buffer .