public long readToByteChannel11 ( ucar . nc2 . Variable v2 , Section section , WritableByteChannel channel ) throws java . io . IOException , ucar . ma2 . InvalidRangeException { Array data = readData ( v2 , section ) ; float [ ] ftdata = new float [ ( int ) data . getSize ( ) ] ; byte [ ] bytedata = new byte [ ( int ) data . getSize ( ) * 4 ] ; IndexIterator iter = data . getIndexIterator ( ) ; int i = 0 ; ByteBuffer buffer = ByteBuffer . allocateDirect ( bytedata . length ) ; while ( iter . hasNext ( ) ) { ftdata [ i ] = iter . getFloatNext ( ) ; bytedata [ i ] = new Float ( ftdata [ i ] ) . byteValue ( ) ; buffer . put ( bytedata [ i ] ) ; i ++ ; } buffer = ByteBuffer . wrap ( bytedata ) ; // write the bytes to the channel int count = channel . write ( buffer ) ; System . out . println ( "COUNT=" + count ) ; // check if all bytes where written if ( buffer . hasRemaining ( ) ) { // if not all bytes were written, move the unwritten bytes to the beginning and // set position just after the last unwritten byte buffer . compact ( ) ; } else { buffer . clear ( ) ; } return ( long ) count ; }
Read data from a top level Variable and send data to a WritableByteChannel .