Don’t expose the complete reply object from the notice event.
[xudocci.git] / src / main / java / net / pterodactylus / irc / Connection.java
1 /*
2  * XdccDownloader - Connection.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.irc;
19
20 import static com.google.common.base.Preconditions.checkState;
21 import static java.util.Arrays.asList;
22 import static java.util.concurrent.TimeUnit.SECONDS;
23
24 import java.io.BufferedReader;
25 import java.io.Closeable;
26 import java.io.EOFException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.io.OutputStream;
31 import java.io.UnsupportedEncodingException;
32 import java.net.InetAddress;
33 import java.net.Socket;
34 import java.net.UnknownHostException;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Set;
38 import java.util.concurrent.TimeUnit;
39 import java.util.concurrent.atomic.AtomicBoolean;
40
41 import javax.net.SocketFactory;
42
43 import net.pterodactylus.irc.connection.ChannelNotJoinedHandler;
44 import net.pterodactylus.irc.connection.ConnectionEstablishHandler;
45 import net.pterodactylus.irc.connection.Handler;
46 import net.pterodactylus.irc.connection.MotdHandler;
47 import net.pterodactylus.irc.connection.PrefixHandler;
48 import net.pterodactylus.irc.event.ChannelJoined;
49 import net.pterodactylus.irc.event.ChannelLeft;
50 import net.pterodactylus.irc.event.ChannelMessageReceived;
51 import net.pterodactylus.irc.event.ChannelNicknames;
52 import net.pterodactylus.irc.event.ChannelNoticeReceived;
53 import net.pterodactylus.irc.event.ChannelTopic;
54 import net.pterodactylus.irc.event.ClientQuit;
55 import net.pterodactylus.irc.event.ConnectionClosed;
56 import net.pterodactylus.irc.event.ConnectionEstablished;
57 import net.pterodactylus.irc.event.ConnectionFailed;
58 import net.pterodactylus.irc.event.DccAcceptReceived;
59 import net.pterodactylus.irc.event.DccSendReceived;
60 import net.pterodactylus.irc.event.KickedFromChannel;
61 import net.pterodactylus.irc.event.NicknameChanged;
62 import net.pterodactylus.irc.event.NicknameInUseReceived;
63 import net.pterodactylus.irc.event.NoNicknameGivenReceived;
64 import net.pterodactylus.irc.event.PrivateMessageReceived;
65 import net.pterodactylus.irc.event.PrivateNoticeReceived;
66 import net.pterodactylus.irc.event.ReplyReceived;
67 import net.pterodactylus.irc.event.UnknownReplyReceived;
68 import net.pterodactylus.irc.util.RandomNickname;
69 import net.pterodactylus.xdcc.util.io.BandwidthCountingInputStream;
70 import net.pterodactylus.xdcc.util.io.BandwidthCountingOutputStream;
71
72 import com.google.common.base.Optional;
73 import com.google.common.collect.Sets;
74 import com.google.common.eventbus.EventBus;
75 import com.google.common.eventbus.Subscribe;
76 import com.google.common.io.Closeables;
77 import com.google.common.primitives.Ints;
78 import com.google.common.primitives.Longs;
79 import com.google.common.util.concurrent.AbstractExecutionThreadService;
80 import com.google.common.util.concurrent.Service;
81 import org.apache.log4j.Logger;
82
83 /**
84  * A connection to an IRC server.
85  *
86  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
87  */
88 public class Connection extends AbstractExecutionThreadService implements Service {
89
90         /* The logger. */
91         private static final Logger logger = Logger.getLogger(Connection.class.getName());
92
93         /** The event bus. */
94         private final EventBus eventBus;
95
96         /** The socket factory. */
97         private final SocketFactory socketFactory;
98
99         /** The hostname to connect to. */
100         private final String hostname;
101
102         /** The port to connect to. */
103         private final int port;
104
105         /** The nickname chooser. */
106         private NicknameChooser nicknameChooser = new NicknameChooser() {
107
108                 @Override
109                 public String getNickname() {
110                         return RandomNickname.get();
111                 }
112         };
113
114         /** The nickname. */
115         private String nickname = null;
116
117         /** The username. */
118         private Optional<String> username = Optional.absent();
119
120         /** The real name. */
121         private Optional<String> realName = Optional.absent();
122
123         /** The optional password for the connection. */
124         private Optional<String> password = Optional.absent();
125
126         /** The connection handler. */
127         private ConnectionHandler connectionHandler;
128
129         /** Whether the connection has already been established. */
130         private final AtomicBoolean established = new AtomicBoolean();
131
132         /**
133          * Creates a new connection.
134          *
135          * @param eventBus
136          *              The event bus
137          * @param socketFactory
138          *              The socket factory
139          * @param hostname
140          *              The hostname of the IRC server
141          * @param port
142          *              The port number of the IRC server
143          */
144         public Connection(EventBus eventBus, SocketFactory socketFactory, String hostname, int port) {
145                 this.eventBus = eventBus;
146                 this.socketFactory = socketFactory;
147                 this.hostname = hostname;
148                 this.port = port;
149         }
150
151         //
152         // ACCESSORS
153         //
154
155         /**
156          * Returns the hostname of the remote end of the connection.
157          *
158          * @return The remote’s hostname
159          */
160         public String hostname() {
161                 return hostname;
162         }
163
164         /**
165          * Returns the port number of the remote end of the connection.
166          *
167          * @return The remote’s port number
168          */
169         public int port() {
170                 return port;
171         }
172
173         /**
174          * Returns whether this connection has already been established.
175          *
176          * @return {@code true} as long as this connection is established, {@code
177          *         false} otherwise
178          */
179         public boolean established() {
180                 return established.get();
181         }
182
183         /**
184          * Returns the nickname that is currently in use by this connection. The
185          * nickname is only available once the connection has been {@link #start()}ed.
186          *
187          * @return The current nickname
188          */
189         public String nickname() {
190                 return nickname;
191         }
192
193         //
194         // MUTATORS
195         //
196
197         /**
198          * Sets the nickname chooser. The nickname chooser is only used during the
199          * creation of the connection.
200          *
201          * @param nicknameChooser
202          *              The nickname chooser
203          * @return This connection
204          */
205         public Connection nicknameChooser(NicknameChooser nicknameChooser) {
206                 this.nicknameChooser = nicknameChooser;
207                 return this;
208         }
209
210         /**
211          * Sets the username to use.
212          *
213          * @param username
214          *              The username to use
215          * @return This connection
216          */
217         public Connection username(String username) {
218                 this.username = Optional.fromNullable(username);
219                 return this;
220         }
221
222         /**
223          * Sets the real name to use.
224          *
225          * @param realName
226          *              The real name to use
227          * @return This connection
228          */
229         public Connection realName(String realName) {
230                 this.realName = Optional.fromNullable(realName);
231                 return this;
232         }
233
234         /**
235          * Sets the optional password for the connection.
236          *
237          * @param password
238          *              The password for the connection
239          * @return This connection
240          */
241         public Connection password(String password) {
242                 this.password = Optional.fromNullable(password);
243                 return this;
244         }
245
246         //
247         // ACTIONS
248         //
249
250         /**
251          * Returns the current rate of the connection’s incoming side.
252          *
253          * @return The current input rate (in bytes per second)
254          */
255         public long getInputRate() {
256                 return (connectionHandler != null) ? connectionHandler.getInputRate() : 0;
257         }
258
259         /**
260          * Returns the current rate of the connection’s outgoing side.
261          *
262          * @return The current output rate (in bytes per second)
263          */
264         public long getOutputRate() {
265                 return (connectionHandler != null) ? connectionHandler.getOutputRate() : 0;
266         }
267
268         /**
269          * Checks whether the given source is the client represented by this
270          * connection.
271          *
272          * @param source
273          *              The source to check
274          * @return {@code true} if this connection represents the given source, {@code
275          *         false} otherwise
276          */
277         public boolean isSource(Source source) {
278                 return source.nick().isPresent() && source.nick().get().equals(nickname);
279         }
280
281         /**
282          * Joins the given channel.
283          *
284          * @param channel
285          *              The channel to join
286          * @throws IOException
287          *              if an I/O error occurs
288          */
289         public void joinChannel(final String channel) throws IOException {
290                 connectionHandler.sendCommand("JOIN", channel);
291         }
292
293         /**
294          * Sends a message to the given recipient, which may be a channel or another
295          * nickname.
296          *
297          * @param recipient
298          *              The recipient of the message
299          * @param message
300          *              The message
301          * @throws IOException
302          *              if an I/O error occurs
303          */
304         public void sendMessage(String recipient, String message) throws IOException {
305                 connectionHandler.sendCommand("PRIVMSG", recipient, message);
306         }
307
308         /**
309          * Sends a DCC RESUME request to the given recipient.
310          *
311          * @param recipient
312          *              The recipient of the request
313          * @param filename
314          *              The name of the file to resume
315          * @param port
316          *              The port number from the original DCC SEND request
317          * @param position
318          *              The position at which to resume the transfer
319          * @throws IOException
320          *              if an I/O error occurs
321          */
322         public void sendDccResume(String recipient, String filename, int port, long position) throws IOException {
323                 connectionHandler.sendCommand("PRIVMSG", recipient, String.format("\u0001DCC RESUME %s %d %d\u0001", filename, port, position));
324         }
325
326         /**
327          * Closes this connection.
328          *
329          * @throws IOException
330          *              if an I/O error occurs
331          */
332         public void close() throws IOException {
333                 if (connectionHandler != null) {
334                         connectionHandler.close();
335                 }
336         }
337
338         //
339         // ABSTRACTEXECUTIONTHREADSERVICE METHODS
340         //
341
342         @Override
343         protected void startUp() throws IllegalStateException {
344                 checkState(username.isPresent(), "username must be set");
345                 checkState(realName.isPresent(), "realName must be set");
346         }
347
348         @Override
349         protected void run() {
350
351                 /* connect to remote socket. */
352                 try {
353                         Socket socket = socketFactory.createSocket(hostname, port);
354                         socket.setSoTimeout((int) TimeUnit.MINUTES.toMillis(3));
355                         connectionHandler = new ConnectionHandler(socket.getInputStream(), socket.getOutputStream());
356
357                         /* register connection. */
358                         if (password.isPresent()) {
359                                 connectionHandler.sendCommand("PASSWORD", password.get());
360                         }
361                         connectionHandler.sendCommand("USER", username.get(), "8", "*", realName.get());
362                         nickname = nicknameChooser.getNickname();
363                         connectionHandler.sendCommand("NICK", nickname);
364
365                 } catch (IOException ioe1) {
366                         eventBus.post(new ConnectionFailed(this, ioe1));
367                         return;
368                 }
369
370                 eventBus.register(this);
371                 /* now read replies and react. */
372                 try {
373                         /* some status variables. */
374                         boolean connected = true;
375                         Set<Nickname> nicks = Sets.newHashSet();
376
377                         PrefixHandler prefixHandler = new PrefixHandler();
378                         List<Handler> handlers = asList(
379                                         new MotdHandler(eventBus, this),
380                                         new ChannelNotJoinedHandler(eventBus, this),
381                                         new ConnectionEstablishHandler(eventBus, this),
382                                         prefixHandler
383                         );
384
385                         while (connected) {
386                                 Reply reply = connectionHandler.readReply();
387                                 eventBus.post(new ReplyReceived(this, reply));
388                                 logger.trace(String.format("<< %s", reply));
389                                 String command = reply.command();
390                                 List<String> parameters = reply.parameters();
391
392                                 for (Handler handler : handlers) {
393                                         if (handler.willHandle(reply)) {
394                                                 handler.handleReply(reply);
395                                                 break;
396                                         }
397                                 }
398
399                                 /* most common events. */
400                                 if (command.equalsIgnoreCase("PRIVMSG")) {
401                                         String recipient = parameters.get(0);
402                                         String message = parameters.get(1);
403                                         if (message.startsWith("\u0001") && message.endsWith("\u0001")) {
404                                                 /* CTCP! */
405                                                 handleCtcp(reply.source().get(), message);
406                                         } else if (!prefixHandler.isChannel(recipient)) {
407                                                 eventBus.post(new PrivateMessageReceived(this, reply.source().get(), message));
408                                         } else {
409                                                 eventBus.post(new ChannelMessageReceived(this, recipient, reply.source().get(), message));
410                                         }
411
412                                 } else if (command.equalsIgnoreCase("NOTICE")) {
413                                         String recipient = parameters.get(0);
414                                         String message = parameters.get(1);
415                                         if (message.startsWith("\u0001") && message.endsWith("\u0001")) {
416                                                 /* CTCP! */
417                                                 handleCtcp(reply.source().get(), message);
418                                         } else if (!prefixHandler.isChannel(recipient)) {
419                                                 eventBus.post(new PrivateNoticeReceived(this, reply.source().get(), message));
420                                         } else {
421                                                 eventBus.post(new ChannelNoticeReceived(this, reply.source().get(), recipient, message));
422                                         }
423
424                                 /* 43x replies are for nick change errors. */
425                                 } else if (command.equals("431")) {
426                                         eventBus.post(new NoNicknameGivenReceived(this, reply));
427                                 } else if (command.equals("433")) {
428                                         if (!established.get()) {
429                                                 nickname = nicknameChooser.getNickname();
430                                                 connectionHandler.sendCommand("NICK", nickname);
431                                         } else {
432                                                 eventBus.post(new NicknameInUseReceived(this, reply));
433                                         }
434
435                                 /* client stuff. */
436                                 } else if (command.equalsIgnoreCase("NICK")) {
437                                         eventBus.post(new NicknameChanged(this, reply.source().get(), parameters.get(0)));
438
439                                 /* channel stuff. */
440                                 } else if (command.equalsIgnoreCase("JOIN")) {
441                                         eventBus.post(new ChannelJoined(this, parameters.get(0), reply.source().get()));
442                                 } else if (command.equals("331")) {
443                                         /* no topic is set. */
444                                 } else if (command.equals("332")) {
445                                         eventBus.post(new ChannelTopic(this, parameters.get(1), parameters.get(2)));
446                                 } else if (command.equals("353")) {
447                                         for (String nickname : parameters.get(3).split(" ")) {
448                                                 if (prefixHandler.isNickPrefixed(nickname)) {
449                                                         nicks.add(new Nickname(nickname.substring(1), nickname.substring(0, 1)));
450                                                 } else {
451                                                         nicks.add(new Nickname(nickname, ""));
452                                                 }
453                                         }
454                                 } else if (command.equals("366")) {
455                                         eventBus.post(new ChannelNicknames(this, parameters.get(1), nicks));
456                                         nicks.clear();
457                                 } else if (command.equalsIgnoreCase("PART")) {
458                                         eventBus.post(new ChannelLeft(this, parameters.get(0), reply.source().get(), getOptional(parameters, 1)));
459                                 } else if (command.equalsIgnoreCase("QUIT")) {
460                                         eventBus.post(new ClientQuit(this, reply.source().get(), parameters.get(0)));
461
462                                 /* basic connection housekeeping. */
463                                 } else if (command.equalsIgnoreCase("PING")) {
464                                         connectionHandler.sendCommand("PONG", getOptional(parameters, 0), getOptional(parameters, 1));
465
466                                 } else if (command.equalsIgnoreCase("KICK")) {
467                                         eventBus.post(new KickedFromChannel(this, parameters.get(0), reply.source().get(), parameters.get(1), getOptional(parameters, 2)));
468
469                                 /* okay, everything else. */
470                                 } else {
471                                         eventBus.post(new UnknownReplyReceived(this, reply));
472                                 }
473                         }
474                         eventBus.post(new ConnectionClosed(this));
475                 } catch (IOException ioe1) {
476                         logger.warn("I/O error", ioe1);
477                         eventBus.post(new ConnectionClosed(this, ioe1));
478                 } catch (RuntimeException re1) {
479                         logger.error("Runtime error", re1);
480                         eventBus.post(new ConnectionClosed(this, re1));
481                 } finally {
482                         established.set(false);
483                         eventBus.unregister(this);
484                         logger.info("Closing Connection.");
485                         try {
486                                 Closeables.close(connectionHandler, true);
487                         } catch (IOException ioe1) {
488                                 /* will not be thrown. */
489                         }
490                 }
491
492         }
493
494         @Subscribe
495         public void connectionEstablished(ConnectionEstablished connectionEstablished) {
496                 if (connectionEstablished.connection() == this) {
497                         established.set(true);
498                 }
499         }
500
501         //
502         // PRIVATE METHODS
503         //
504
505         /**
506          * Handles a CTCP message.
507          *
508          * @param client
509          *              The client sending the message
510          * @param message
511          *              The message
512          */
513         private void handleCtcp(Source client, String message) {
514                 String[] messageWords = message.substring(1, message.length() - 1).split(" +");
515                 String ctcpCommand = messageWords[0];
516                 if (ctcpCommand.equalsIgnoreCase("DCC")) {
517                         if (messageWords[1].equalsIgnoreCase("SEND")) {
518                                 Optional<InetAddress> inetAddress = parseInetAddress(messageWords[3]);
519                                 Optional<Integer> port = Optional.fromNullable(Ints.tryParse(messageWords[4]));
520                                 long fileSize = Optional.fromNullable(Longs.tryParse(messageWords[5])).or(-1L);
521                                 if (inetAddress.isPresent() && port.isPresent()) {
522                                         eventBus.post(new DccSendReceived(this, client, messageWords[2], inetAddress.get(), port.get(), fileSize));
523                                 } else {
524                                         logger.warn(String.format("Received malformed DCC SEND: “%s”", message));
525                                 }
526                         } else if (messageWords[1].equalsIgnoreCase("ACCEPT")) {
527                                 Optional<Integer> port = Optional.fromNullable(Ints.tryParse(messageWords[3]));
528                                 long position = (messageWords.length > 4) ? Optional.fromNullable(Longs.tryParse(messageWords[4])).or(-1L) : -1;
529                                 if (port.isPresent()) {
530                                         eventBus.post(new DccAcceptReceived(this, client, messageWords[2], port.get(), position));
531                                 } else {
532                                         logger.warn(String.format("Received malformed DCC ACCEPT: “%s”", message));
533                                 }
534                         }
535                 }
536         }
537
538         /**
539          * Returns an item from the list, or {@link Optional#absent()} if the list is
540          * shorter than required for the given index.
541          *
542          * @param list
543          *              The list to get an item from
544          * @param index
545          *              The index of the item
546          * @param <T>
547          *              The type of the list items
548          * @return This list item wrapped in an {@link Optional}, or {@link
549          *         Optional#absent()} if the list is not long enough
550          */
551         private static <T> Optional<T> getOptional(List<T> list, int index) {
552                 if (index < list.size()) {
553                         return Optional.fromNullable(list.get(index));
554                 }
555                 return Optional.absent();
556         }
557
558         /**
559          * Parses the given {@code ip} and returns an {@link InetAddress} from it.
560          *
561          * @param ip
562          *              The IP to parse
563          * @return The parsed inet address, or {@link Optional#absent()} if no inet
564          *         address could be parsed
565          */
566         private Optional<InetAddress> parseInetAddress(String ip) {
567                 Long ipNumber = Longs.tryParse(ip);
568                 if (ipNumber == null) {
569                         return Optional.absent();
570                 }
571
572                 StringBuilder hostname = new StringBuilder(15);
573                 hostname.append((ipNumber >>> 24) & 0xff).append('.');
574                 hostname.append((ipNumber >>> 16) & 0xff).append('.');
575                 hostname.append((ipNumber >>> 8) & 0xff).append('.');
576                 hostname.append(ipNumber & 0xff);
577                 try {
578                         return Optional.of(InetAddress.getByName(hostname.toString()));
579                 } catch (UnknownHostException uhe1) {
580                         return Optional.absent();
581                 }
582         }
583
584         /** Handles input and output for the connection. */
585         private class ConnectionHandler implements Closeable {
586
587                 /** The output stream of the connection. */
588                 private final BandwidthCountingOutputStream outputStream;
589
590                 /** The input stream. */
591                 private final BandwidthCountingInputStream inputStream;
592
593                 /** The input stream of the connection. */
594                 private final BufferedReader inputStreamReader;
595
596                 /**
597                  * Creates a new connection handler for the given input stream and output
598                  * stream.
599                  *
600                  * @param inputStream
601                  *              The input stream of the connection
602                  * @param outputStream
603                  *              The output stream of the connection
604                  * @throws UnsupportedEncodingException
605                  *              if the encoding (currently “UTF-8”) is not valid
606                  */
607                 private ConnectionHandler(InputStream inputStream, OutputStream outputStream) throws UnsupportedEncodingException {
608                         this.outputStream = new BandwidthCountingOutputStream(outputStream, 5, SECONDS);
609                         this.inputStream = new BandwidthCountingInputStream(inputStream, 5, SECONDS);
610                         inputStreamReader = new BufferedReader(new InputStreamReader(this.inputStream, "UTF-8"));
611                 }
612
613                 //
614                 // ACTIONS
615                 //
616
617                 /**
618                  * Returns the current rate of the connection’s incoming side.
619                  *
620                  * @return The current input rate (in bytes per second)
621                  */
622                 public long getInputRate() {
623                         return inputStream.getCurrentRate();
624                 }
625
626                 /**
627                  * Returns the current rate of the connection’s outgoing side.
628                  *
629                  * @return The current output rate (in bytes per second)
630                  */
631                 public long getOutputRate() {
632                         return outputStream.getCurrentRate();
633                 }
634
635                 /**
636                  * Sends a command with the given parameters, skipping all {@link
637                  * Optional#absent()} optionals.
638                  *
639                  * @param command
640                  *              The command to send
641                  * @param parameters
642                  *              The parameters
643                  * @throws IOException
644                  *              if an I/O error occurs
645                  */
646                 public void sendCommand(String command, Optional<String>... parameters) throws IOException {
647                         List<String> setParameters = new ArrayList<String>();
648                         for (Optional<String> maybeSetParameter : parameters) {
649                                 if (maybeSetParameter.isPresent()) {
650                                         setParameters.add(maybeSetParameter.get());
651                                 }
652                         }
653                         sendCommand(command, setParameters.toArray(new String[setParameters.size()]));
654                 }
655
656                 /**
657                  * Sends a command with the given parameters.
658                  *
659                  * @param command
660                  *              The command to send
661                  * @param parameters
662                  *              The parameters of the command
663                  * @throws IOException
664                  *              if an I/O error occurs
665                  * @throws IllegalArgumentException
666                  *              if any parameter but that last contains a space character
667                  */
668                 public void sendCommand(String command, String... parameters) throws IOException, IllegalArgumentException {
669                         StringBuilder commandBuilder = new StringBuilder();
670
671                         commandBuilder.append(command);
672                         for (int parameterIndex = 0; parameterIndex < parameters.length; ++parameterIndex) {
673                                 String parameter = parameters[parameterIndex];
674                                 /* space is only allowed in the last parameter. */
675                                 commandBuilder.append(' ');
676                                 if (parameter.contains(" ")) {
677                                         if (parameterIndex == (parameters.length - 1)) {
678                                                 commandBuilder.append(':');
679                                         } else {
680                                                 throw new IllegalArgumentException(String.format("parameter “%s” must not contain space!", parameter));
681                                         }
682                                 }
683                                 commandBuilder.append(parameter);
684                         }
685
686                         logger.trace(String.format(">> %s", commandBuilder));
687                         outputStream.write((commandBuilder.toString() + "\r\n").getBytes("UTF-8"));
688                         outputStream.flush();
689                 }
690
691                 /**
692                  * Reads a line of reply from the connection.
693                  *
694                  * @return The reply
695                  * @throws IOException
696                  *              if an I/O error occurs
697                  * @throws EOFException
698                  *              if EOF was reached
699                  */
700                 public Reply readReply() throws IOException, EOFException {
701                         String line = inputStreamReader.readLine();
702                         if (line == null) {
703                                 throw new EOFException();
704                         }
705
706                         return Reply.parseLine(line);
707                 }
708
709                 //
710                 // CLOSEABLE METHODS
711                 //
712
713                 @Override
714                 public void close() throws IOException {
715                         Closeables.close(outputStream, true);
716                         Closeables.close(inputStreamReader, true);
717                         Closeables.close(inputStream, true);
718                 }
719
720         }
721
722 }