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