Add logging.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / Connection.java
1 /*
2  * Sonitus - 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.sonitus.data;
19
20 import java.io.IOException;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23
24 /**
25  * A connection reads bytes from a {@link Source} and feeds it to a sink. This
26  * class is meant to be subclassed by each {@link Sink}, overriding the {@link
27  * #feed(byte[])} method to actually feed the data into the sink. The {@link
28  * #feed(byte[])} method is also responsible for blocking for an appropriate
29  * amount of time; this method determines how fast a {@link Source} is
30  * consumed.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public abstract class Connection implements Runnable {
35
36         /** The logger. */
37         private static final Logger logger = Logger.getLogger(Connection.class.getName());
38
39         /** The source to consume. */
40         private final Source source;
41
42         /**
43          * Creates a new connection that will read from the given source.
44          *
45          * @param source
46          *              The source to read
47          */
48         public Connection(Source source) {
49                 this.source = source;
50         }
51
52         //
53         // RUNNABLE METHODS
54         //
55
56         @Override
57         public void run() {
58                 while (true) {
59                         try {
60                                 byte[] buffer = source.get(bufferSize());
61                                 feed(buffer);
62                         } catch (IOException ioe1) {
63                                 logger.log(Level.WARNING, "Sink died!", ioe1);
64                                 break;
65                         }
66                 }
67                 try {
68                         logger.info("Connection finished.");
69                         finish();
70                 } catch (IOException ioe1) {
71                         /* well, what can we do? nothing. */
72                 }
73         }
74
75         //
76         // SUBCLASS METHODS
77         //
78
79         /**
80          * Returns the number of bytes that will be requested from the source.
81          *
82          * @return The number of bytes that will be requested from the source
83          */
84         protected abstract int bufferSize();
85
86         /**
87          * Feeds the read data into the sink. The given buffer is always filled and
88          * never contains excess elements.
89          *
90          * @param buffer
91          *              The data
92          * @throws IOException
93          *              if an I/O error occurs
94          */
95         protected abstract void feed(byte[] buffer) throws IOException;
96
97         /**
98          * Notifies the sink that the source does not deliver any more data.
99          *
100          * @throws IOException
101          *              if an I/O error occurs
102          */
103         protected abstract void finish() throws IOException;
104
105 }