|
View:
New views
8 Messages
—
Rating Filter:
Alert me
|
|
|
write byte[] to fileSorry - I don't have my Groovy in Action with me; what's the grooviest way of creating a new file from a variable of type byte[]? Thanks! --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: write byte[] to fileOn Sunday 06 July 2008 09:44:14 pm Corey wrote:
> Sorry - I don't have my Groovy in Action with me; what's the grooviest > way of creating a new file from a variable of type byte[]? > http://groovy.codehaus.org/JN2025-Streams .... is making my head swim. I've got a variable of type byte[], I want to write that to a file - help? --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: write byte[] to fileOn Monday 07 July 2008 12:09:16 am Russel Winder wrote:
> On Sun, 2008-07-06 at 23:12 -0700, Corey wrote: > > On Sunday 06 July 2008 09:44:14 pm Corey wrote: > > > Sorry - I don't have my Groovy in Action with me; what's the grooviest > > > way of creating a new file from a variable of type byte[]? > > > > http://groovy.codehaus.org/JN2025-Streams > > > > .... is making my head swim. > > > > I've got a variable of type byte[], I want to write that to a file - > > help? > > Is there anything wrong with: > > ( new File ( 'file/name' ) ).write ( variable ) Thanks for the response, but yep - that was my first attempt, which along with most other things I've been trying has ended up with something along the lines of: " groovy.lang.MissingMethodException: No signature of method: java.io.File.write() is applicable for argument types: ([B) values: {[-1, -40]} " After a lot of reading and trial and error (and gnashing of teeth), I came up with the following a few minutes ago: new FileOutputStream('file/name').withWriter { w -> w << new BufferedInputStream( new ByteArrayInputStream(variable) ) } ... which works. Wasn't too easy -- probably because I'm tired and cranky; and also because I'm coming to groovy without any real background in java. Anyhow, I wonder if I'm still missing out on something a little more straight-forward/simple or obvious. Cheers --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: write byte[] to fileOn Sun, 2008-07-06 at 23:12 -0700, Corey wrote:
> On Sunday 06 July 2008 09:44:14 pm Corey wrote: > > Sorry - I don't have my Groovy in Action with me; what's the grooviest > > way of creating a new file from a variable of type byte[]? > > > > http://groovy.codehaus.org/JN2025-Streams > > .... is making my head swim. > > I've got a variable of type byte[], I want to write that to a file - > help? ( new File ( 'file/name' ) ).write ( variable ) -- Russel. ==================================================== Dr Russel Winder Partner Concertant LLP t: +44 20 7585 2200, +44 20 7193 9203 41 Buckmaster Road, f: +44 8700 516 084 London SW11 1EN, UK. m: +44 7770 465 077 |
|
|
Re: write byte[] to fileCorey wrote:
> On Sunday 06 July 2008 09:44:14 pm Corey wrote: >> Sorry - I don't have my Groovy in Action with me; what's the grooviest >> way of creating a new file from a variable of type byte[]? >> > > http://groovy.codehaus.org/JN2025-Streams > > .... is making my head swim. > > I've got a variable of type byte[], I want to write that to a file - > help? If you have text of any kind, you would use writers and would have the << operator available to you. For binary data, that shortcut isn't there (though it probably should be) but you can use: byte[] bytes = "word".bytes def f = new File("temp.dat") f.delete() def os = f.newOutputStream() os.write(bytes, 0, bytes.length) os.close() assert f.size() == 4 though you might want a try ... catch ... finally in all but the most trivial of scripts. I just added the shortcut in trunk, so you can now do: byte[] bytes = "word".bytes def f = new File("temp.dat") f.delete() f << bytes assert f.size() == 4 Paul. --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: write byte[] to fileCorey wrote:
> On Monday 07 July 2008 12:09:16 am Russel Winder wrote: >> On Sun, 2008-07-06 at 23:12 -0700, Corey wrote: >>> On Sunday 06 July 2008 09:44:14 pm Corey wrote: >>>> Sorry - I don't have my Groovy in Action with me; what's the grooviest >>>> way of creating a new file from a variable of type byte[]? >>> http://groovy.codehaus.org/JN2025-Streams >>> >>> .... is making my head swim. >>> >>> I've got a variable of type byte[], I want to write that to a file - >>> help? >> Is there anything wrong with: >> >> ( new File ( 'file/name' ) ).write ( variable ) > > > Thanks for the response, but yep - that was my first attempt, which along > with most other things I've been trying has ended up with something along > the lines of: > > " > groovy.lang.MissingMethodException: No signature of method: > java.io.File.write() is applicable for argument types: ([B) values: > {[-1, -40]} > " > > After a lot of reading and trial and error (and gnashing of teeth), I > came up with the following a few minutes ago: > > > new FileOutputStream('file/name').withWriter { w -> > > w << new BufferedInputStream( new ByteArrayInputStream(variable) ) > } > > ... which works. > > Wasn't too easy -- probably because I'm tired and cranky; and also because > I'm coming to groovy without any real background in java. > > Anyhow, I wonder if I'm still missing out on something a little more > straight-forward/simple or obvious. If you are going to use a writer, then you don't have binary data. You probably wnt something like: byte[] bytes = "word".bytes def f = new File("temp.dat") f.delete() f << new String(bytes) assert f.size() == 4 Cheers, Paul. --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: write byte[] to fileOn Monday 07 July 2008 01:11:29 am Paul King wrote:
> Corey wrote: > > On Monday 07 July 2008 12:09:16 am Russel Winder wrote: > >> On Sun, 2008-07-06 at 23:12 -0700, Corey wrote: > >>> On Sunday 06 July 2008 09:44:14 pm Corey wrote: > >>>> Sorry - I don't have my Groovy in Action with me; what's the grooviest > >>>> way of creating a new file from a variable of type byte[]? > >>> > >>> http://groovy.codehaus.org/JN2025-Streams > >>> > >>> .... is making my head swim. > >>> > >>> I've got a variable of type byte[], I want to write that to a file - > >>> help? > >> > >> Is there anything wrong with: > >> > >> ( new File ( 'file/name' ) ).write ( variable ) > > > > Thanks for the response, but yep - that was my first attempt, which along > > with most other things I've been trying has ended up with something along > > the lines of: > > > > " > > groovy.lang.MissingMethodException: No signature of method: > > java.io.File.write() is applicable for argument types: ([B) values: > > {[-1, -40]} > > " > > > > After a lot of reading and trial and error (and gnashing of teeth), I > > came up with the following a few minutes ago: > > > > > > new FileOutputStream('file/name').withWriter { w -> > > > > w << new BufferedInputStream( new ByteArrayInputStream(variable) ) > > } > > > > ... which works. > > > > Wasn't too easy -- probably because I'm tired and cranky; and also > > because I'm coming to groovy without any real background in java. > > > > Anyhow, I wonder if I'm still missing out on something a little more > > straight-forward/simple or obvious. > > If you are going to use a writer, then you don't have binary data. > You probably wnt something like: > > byte[] bytes = "word".bytes > def f = new File("temp.dat") > f.delete() > f << new String(bytes) > assert f.size() == 4 > I appreciate the help, many thanks! It turns out that using withWriter didn't actually work quite right. The data I'm working with is an image file, transferred off disk to a web application via multipart form. When I used: new FileOutputStream('file/name').withWriter { w -> w << new BufferedInputStream( new ByteArrayInputStream(variable) ) } The resulting file was unrecognized as an image (jpg), even though it was the expected size. I used your other example, in your other response: def f = new File('file/name') f.delete() def os = f.newOutputStream() os.write(variable, 0, variable.length) os.close() ... which worked. ( thanks! ) But out of pure stubbornness, I went back to try and get it work using the FileOutputStream -- and was successful with: new FileOutputStream('file/name').withStream { s -> s << new BufferedInputStream( new ByteArrayInputStream(variable) ) } ... so now I'm wondering -- what's the pros/cons between the two working solutions? Are they functionally synonymous aside from syntax? I guess I'm coming from the notion that buffered == preferred, and using the closure syntax is more groovyish, plus I don't have to close(). ( also, why do you delete() after the call to new File? ) And anyone know of any good resources for getting more familiar with all this I/O stuff besides the API docs? --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: write byte[] to fileCorey wrote:
> On Monday 07 July 2008 01:11:29 am Paul King wrote: >> Corey wrote: >>> On Monday 07 July 2008 12:09:16 am Russel Winder wrote: >>>> On Sun, 2008-07-06 at 23:12 -0700, Corey wrote: >>>>> On Sunday 06 July 2008 09:44:14 pm Corey wrote: >>>>>> Sorry - I don't have my Groovy in Action with me; what's the grooviest >>>>>> way of creating a new file from a variable of type byte[]? >>>>> http://groovy.codehaus.org/JN2025-Streams >>>>> >>>>> .... is making my head swim. >>>>> >>>>> I've got a variable of type byte[], I want to write that to a file - >>>>> help? >>>> Is there anything wrong with: >>>> >>>> ( new File ( 'file/name' ) ).write ( variable ) >>> Thanks for the response, but yep - that was my first attempt, which along >>> with most other things I've been trying has ended up with something along >>> the lines of: >>> >>> " >>> groovy.lang.MissingMethodException: No signature of method: >>> java.io.File.write() is applicable for argument types: ([B) values: >>> {[-1, -40]} >>> " >>> >>> After a lot of reading and trial and error (and gnashing of teeth), I >>> came up with the following a few minutes ago: >>> >>> >>> new FileOutputStream('file/name').withWriter { w -> >>> >>> w << new BufferedInputStream( new ByteArrayInputStream(variable) ) >>> } >>> >>> ... which works. >>> >>> Wasn't too easy -- probably because I'm tired and cranky; and also >>> because I'm coming to groovy without any real background in java. >>> >>> Anyhow, I wonder if I'm still missing out on something a little more >>> straight-forward/simple or obvious. >> If you are going to use a writer, then you don't have binary data. >> You probably wnt something like: >> >> byte[] bytes = "word".bytes >> def f = new File("temp.dat") >> f.delete() >> f << new String(bytes) >> assert f.size() == 4 >> > > I appreciate the help, many thanks! > > It turns out that using withWriter didn't actually work quite right. > > The data I'm working with is an image file, transferred off disk to a > web application via multipart form. > > When I used: > > new FileOutputStream('file/name').withWriter { w -> > > w << new BufferedInputStream( new ByteArrayInputStream(variable) ) > } > > The resulting file was unrecognized as an image (jpg), even though it > was the expected size. > > I used your other example, in your other response: > > def f = new File('file/name') > f.delete() > def os = f.newOutputStream() > os.write(variable, 0, variable.length) > os.close() > > ... which worked. ( thanks! ) > > But out of pure stubbornness, I went back to try and get it work using > the FileOutputStream -- and was successful with: > > new FileOutputStream('file/name').withStream { s -> > > s << new BufferedInputStream( new ByteArrayInputStream(variable) ) > } If you want to use a with-closure your best bet is the one on file: byte[] bytes = "word".bytes def f = new File("temp.dat") f.delete() f.withOutputStream { s -> s << bytes } assert f.size() == 4 > ... so now I'm wondering -- what's the pros/cons between the two working > solutions? Are they functionally synonymous aside from syntax? > > I guess I'm coming from the notion that buffered == preferred, and using > the closure syntax is more groovyish, plus I don't have to close(). > > ( also, why do you delete() after the call to new File? ) The write and << operators append to the file. If you know the file is new you don't need it. > And anyone know of any good resources for getting more familiar with all > this I/O stuff besides the API docs? GinA or one of the other Groovy books or the PLEAC Groovy examples. Cheers, Paul. > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
| Free Forum Powered by Nabble | Forum Help |