Using Groovy copy streams with info

1,172 views
Skip to first unread message

Pilgrim, Peter

unread,
May 17, 2010, 6:40:13 AM5/17/10
to jav...@googlegroups.com

 

            def total = 0;

            def len = 0;

            def buffer = new byte[131072];

            def InputStream is = conn.getInputStream();

            while ( ( len = is.read(buffer) ) != -1 ) {

                        output.write( buffer, 0, len );

                        total += len;

            }

            output.flush();

            println "Downloaded total: ${total} bytes successfully.";

 

 

What is the syntax for creating a byte array in Groovy?

 

I thought it was originally ``def data = new byte[8192]’’, but the Groovy compiler does not like it at all.

 

Does Groovy even support the ``byte’’ type? If not, then the bytes are converted implicitly to “ints” then what I am trying to do in Groovy will not work.

 

From 5000ft, I am attempting to download data using Groovy into a file using an efficient buffer.

 

 

--

Peter Pilgrim | E-Channel Services, Technical Lead, eBusiness

Lloyds Banking Group | Wholesale Markets & Treasury IT

33 Old Broad Street, London, EC2N 1HZ, UK

' +44 (0)207 158 6135 | ( +44 (0)1234 567 8901

+ peter....@lloydsbanking.com

: www.lloydsbankinggroup.com

 

 



This e-mail is private and confidential and may contain privileged material. If you have received this e-mail in error, please notify the sender and delete it immediately. You must not copy, distribute, disclose or use any of the information in it or any attachments.

Lloyds Banking Group plc. Registered Office: The Mound, Edinburgh EH1 1YZ. Registered in Scotland, number 95000. Telephone: 0131 225 4555.

Lloyds TSB Bank plc. Registered Office: 25 Gresham Street, London EC2V 7HN. Registered in England and Wales, number 2065. Telephone: 020 7626 1500.

Lloyds TSB Scotland plc. Registered Office: Henry Duncan House, 120 George Street, Edinburgh EH2 4LH. Registered in Scotland, number 95237. Telephone: 0131 225 4555.

Cheltenham & Gloucester plc. Registered Office: Barnett Way, Gloucester GL4 3RL. Registered in England and Wales, number 2299428. Telephone: 01452 372372.

Cheltenham & Gloucester Savings is a division of Lloyds TSB Bank plc.
Lloyds TSB Bank plc, Lloyds TSB Scotland plc and Cheltenham & Gloucester plc are authorised and regulated by the Financial Services Authority.

Telephone calls may be monitored or recorded.

______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________

--
You received this message because you are subscribed to the Google Groups "JAVAWUG" group.
To post to this group, send email to jav...@googlegroups.com.
To unsubscribe from this group, send email to javawug+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/javawug?hl=en.

Peter Pilgrim

unread,
May 17, 2010, 9:56:43 AM5/17/10
to jav...@googlegroups.com
Hey bother.

I had another `def buffer' hundred lines above this one.
The solution to compile was to refactor by name.

Then I found a groovy nugget

output << input

This copies the bytes of the input stream to the output stream (fast!)

So unless you need to monitor the total, length and/or are expecting
errors then go with this useful call

groovy-jdk.OutputStream#leftShift( InputStream in );
--
Peter A. Pilgrim
JavaFX / Java EE Software Development / Design / Architecture
(``Java Champion #91`'')

**** See you at Martin Odersky's London Scala Workshop 2010 ****

:: http://audio.fm/profile/peter_pilgrim ::
:: http://jroller.com/peter_pilgrim ::
:: http://jroller.com/javawug ::
:: https://java-champions.dev.java.net/ ::
:: http://www.linkedin.com/in/peterpilgrim2000 ::
:: http://www.facebook.com/profile.php?id=570597454 ::
:: http://twitter.com/peter_pilgrim ::
:: http://twitter.com/javawugcom ::
:: A Sun Certified Enterprise Architect for Java EE 5 platform ::

Pilgrim, Peter

unread,
May 17, 2010, 7:13:57 AM5/17/10
to jav...@googlegroups.com

Don’t bother.

 

Solved with

 

Output << input

 

If you want to monitor actually how many bytes were copied from stream to stream descend into Java and supply some data object to fill and return.

 

 

 

--

Peter Pilgrim | E-Channel Services, Technical Lead, eBusiness

Lloyds Banking Group | Wholesale Markets & Treasury IT

33 Old Broad Street, London, EC2N 1HZ, UK

' +44 (0)207 158 6135 | ( +44 (0)1234 567 8901

+ peter....@lloydsbanking.com

: www.lloydsbankinggroup.com

 

Faisal

unread,
May 18, 2010, 1:35:50 PM5/18/10
to jav...@googlegroups.com
Hi Peter, Thanks for RnD
Reply all
Reply to author
Forward
0 new messages