2 * Sonitus - Connection.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sonitus.data;
20 import java.io.IOException;
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
30 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32 public abstract class Connection implements Runnable {
34 /** The source to consume. */
35 private final Source source;
38 * Creates a new connection that will read from the given source.
43 public Connection(Source source) {
55 byte[] buffer = source.get(bufferSize());
57 } catch (IOException ioe1) {
63 } catch (IOException ioe1) {
64 /* well, what can we do? nothing. */
73 * Returns the number of bytes that will be requested from the source.
75 * @return The number of bytes that will be requested from the source
77 protected abstract int bufferSize();
80 * Feeds the read data into the sink. The given buffer is always filled and
81 * never contains excess elements.
86 * if an I/O error occurs
88 protected abstract void feed(byte[] buffer) throws IOException;
91 * Notifies the sink that the source does not deliver any more data.
94 * if an I/O error occurs
96 protected abstract void finish() throws IOException;