[groovy-dev] OutputStream.leftShift(Object)

classic Classic list List threaded Threaded
11 messages Options
12
Reply | Threaded
Open this post in threaded view
|

[groovy-dev] OutputStream.leftShift(Object)

tugwilson
This seems a rather puzzling method. uses a Writer ti write to the  
stream and it returns the Writer.

This doesn't feel right. Perhaps I'm missing something. Can anybody  
explain why it's the way it is?


John Wilson
The Wilson Partnership
http://www.wilson.co.uk


Reply | Threaded
Open this post in threaded view
|

RE: [groovy-dev] OutputStream.leftShift(Object)

Dierk König
that's for patterns like

a << b << c

cheers
Mittie

> -----Original Message-----
> From: John Wilson [mailto:[hidden email]]
> Sent: Sonntag, 6. November 2005 11:29
> To: [hidden email]
> Subject: [groovy-dev] OutputStream.leftShift(Object)
>
>
> This seems a rather puzzling method. uses a Writer ti write to the  
> stream and it returns the Writer.
>
> This doesn't feel right. Perhaps I'm missing something. Can anybody  
> explain why it's the way it is?
>
>
> John Wilson
> The Wilson Partnership
> http://www.wilson.co.uk
>
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] OutputStream.leftShift(Object)

tugwilson

On 6 Nov 2005, at 11:00, Dierk Koenig wrote:

> that's for patterns like
>
> a << b << c


but why return a Writer? Surely it should return an OutputStream?

and why use a Writer to write the Object? We use Writers when we are  
dealing with characters; OutputStreams deal with bytes.


John Wilson
The Wilson Partnership
http://www.wilson.co.uk


Reply | Threaded
Open this post in threaded view
|

RE: [groovy-dev] OutputStream.leftShift(Object)

Dierk König
from the book :-)
---
The leftShift operator on Writer objects is a really clever beast. It relays
to Writer's write method, which in the GDK tries a best-effort for writing
the argument. The idea is to write a String representation with special
support for arrays, Maps, and Collections. For general objects, toString()
is used.
If the argument is of type InputStream or Reader, it's content is pumped
into the writer. Listing 8.8 shows this in action.

reader = new StringReader('abc')
writer = new StringWriter()

writer << "\nsome String"   << "\n"
writer << [a:1, b:2]        << "\n"
writer << [3,4]             << "\n"
writer << new Date(0)       << "\n"
writer << reader            << "\n"

assert writer.toString() == '''
some String
["a":1, "b":2]
[3, 4]
Thu Jan 01 01:00:00 CET 1970
abc
'''

---

cheers
Mittie



> -----Original Message-----
> From: John Wilson [mailto:[hidden email]]
> Sent: Sonntag, 6. November 2005 12:02
> To: [hidden email]
> Subject: Re: [groovy-dev] OutputStream.leftShift(Object)
>
>
>
> On 6 Nov 2005, at 11:00, Dierk Koenig wrote:
>
> > that's for patterns like
> >
> > a << b << c
>
>
> but why return a Writer? Surely it should return an OutputStream?
>
> and why use a Writer to write the Object? We use Writers when we are
> dealing with characters; OutputStreams deal with bytes.
>
>
> John Wilson
> The Wilson Partnership
> http://www.wilson.co.uk
>

Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] OutputStream.leftShift(Object)

tugwilson

On 6 Nov 2005, at 11:26, Dierk Koenig wrote:

> The leftShift operator on Writer objects is a really clever beast.


Possibly:)

However we are not writing to a Writer here we are writing to an  
OutputStream. We use Writes when we are writing characters and we use  
OutputStreams when we are writing bytes.

I have three problems with OutputStream.leftShift(Object).

1/ I can't see any good reason why this method exists with this  
signature. I don't think that there is an obvious meaning for the  
operation. I would have expected the object to be serialised to the  
OutputStream!

2/ It certainly should not return Writer.

3/ If the method must exist then there must be a way of specifying  
the encoding - using the default encoding never a good idea.

I see that there's an equivalent method on Socket with identical  
behaviour :(

I think we would be better with a toWriter (or, possibly, asWriter)  
method on OutputStream and Socket.

out.toWriter(encoding) << object

(I suppose we should also have a version which uses the system  
encoding for those people who don't care if their program works or  
not!).

On an unrelated matter...

You don't seem to cover the case where the object implements  
Writable. Is this covered elsewhere?


John Wilson
The Wilson Partnership
http://www.wilson.co.uk


Reply | Threaded
Open this post in threaded view
|

RE: [groovy-dev] OutputStream.leftShift(Object)

Dierk König
Yep, I agree.
OutputStream should deal with binary data,
Writer with text.
The en-/decodeBase64() methods may be a corner case.

I also opened a Jira issue concerning the consistency of
encoding handling in the to-/asXXX methods some days ago.
Feel free to comment or open a new issue and possibly link
to the existing one.

Yes, Writables get extra space in the book :-)
(when explaining TemplateEngines)
We have not covered Buildables, yet...

cheers
Mittie

> -----Original Message-----
> From: John Wilson [mailto:[hidden email]]
> Sent: Sonntag, 6. November 2005 12:50
> To: [hidden email]
> Subject: Re: [groovy-dev] OutputStream.leftShift(Object)
>
>
>
> On 6 Nov 2005, at 11:26, Dierk Koenig wrote:
>
> > The leftShift operator on Writer objects is a really clever beast.
>
>
> Possibly:)
>
> However we are not writing to a Writer here we are writing to an  
> OutputStream. We use Writes when we are writing characters and we use  
> OutputStreams when we are writing bytes.
>
> I have three problems with OutputStream.leftShift(Object).
>
> 1/ I can't see any good reason why this method exists with this  
> signature. I don't think that there is an obvious meaning for the  
> operation. I would have expected the object to be serialised to the  
> OutputStream!
>
> 2/ It certainly should not return Writer.
>
> 3/ If the method must exist then there must be a way of specifying  
> the encoding - using the default encoding never a good idea.
>
> I see that there's an equivalent method on Socket with identical  
> behaviour :(
>
> I think we would be better with a toWriter (or, possibly, asWriter)  
> method on OutputStream and Socket.
>
> out.toWriter(encoding) << object
>
> (I suppose we should also have a version which uses the system  
> encoding for those people who don't care if their program works or  
> not!).
>
> On an unrelated matter...
>
> You don't seem to cover the case where the object implements  
> Writable. Is this covered elsewhere?
>
>
> John Wilson
> The Wilson Partnership
> http://www.wilson.co.uk
>
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] OutputStream.leftShift(Object)

tugwilson

On 6 Nov 2005, at 13:53, Dierk Koenig wrote:

> I also opened a Jira issue concerning the consistency of
> encoding handling in the to-/asXXX methods some days ago.
> Feel free to comment or open a new issue and possibly link
> to the existing one.

Will do - thanks

>
> Yes, Writables get extra space in the book :-)
> (when explaining TemplateEngines)
> We have not covered Buildables, yet...

also the object which is produced when a closure is bound to a  
StreamingMarkupBuilder is Writable. This has quite an impact on  
performance and store foorprint.


John Wilson
The Wilson Partnership
http://www.wilson.co.uk


Reply | Threaded
Open this post in threaded view
|

RE: [groovy-dev] OutputStream.leftShift(Object)

Dierk König
> also the object which is produced when a closure is bound to a  
> StreamingMarkupBuilder is Writable. This has quite an impact on  
> performance and store foorprint.

Yep. That's especially cool.
Works like a continuous-flow heater as opposed to a boiler :-)

cheers
Mittie
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-dev] OutputStream.leftShift(Object)

tugwilson

On 6 Nov 2005, at 14:21, Dierk Koenig wrote:

> Works like a continuous-flow heater as opposed to a boiler :-)


Nice analogy.

It also means it can be smart as far as encoding is concerned. As it  
has access to the Writer object, it enquires about the encoding being  
used. It then uses that information to decide which characters in the  
XML document needs to be escaped. Eventually it will also generate  
the correct encoding declaration in the XML declaration at the  
begining of the document.


John Wilson
The Wilson Partnership
http://www.wilson.co.uk


Reply | Threaded
Open this post in threaded view
|

RE: [groovy-dev] OutputStream.leftShift(Object)

Dierk König
> > Works like a continuous-flow heater as opposed to a boiler :-)
>
> Nice analogy.
>
> It also means it can be smart as far as encoding is concerned. As it  
> has access to the Writer object, it enquires about the encoding being  
> used. It then uses that information to decide which characters in the  
> XML document needs to be escaped. Eventually it will also generate  
> the correct encoding declaration in the XML declaration at the  
> begining of the document.

Wow. That's how the heater becomes a
Nutri-matic drink dispenser :-)

cheers
Mittie
12