[freenet-cvs] r17861 - in trunk/apps/thingamablog/src/net/sf/thingamablog/util: . freenet/fcp io

dieppe at freenetproject.org dieppe at freenetproject.org
Wed Feb 13 03:37:20 UTC 2008


Author: dieppe
Date: 2008-02-13 03:37:20 +0000 (Wed, 13 Feb 2008)
New Revision: 17861

Added:
   trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/
   trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/Closer.java
   trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/LineInputStream.java
   trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/ReplacingOutputStream.java
   trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/StreamCopier.java
   trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/TempFileInputStream.java
Modified:
   trunk/apps/thingamablog/src/net/sf/thingamablog/util/freenet/fcp/ClientPutComplexDir.java
   trunk/apps/thingamablog/src/net/sf/thingamablog/util/freenet/fcp/Connection.java
Log:
Updates : add forgotten package needed by freenet.fcp package.


Modified: trunk/apps/thingamablog/src/net/sf/thingamablog/util/freenet/fcp/ClientPutComplexDir.java
===================================================================
--- trunk/apps/thingamablog/src/net/sf/thingamablog/util/freenet/fcp/ClientPutComplexDir.java	2008-02-13 03:26:33 UTC (rev 17860)
+++ trunk/apps/thingamablog/src/net/sf/thingamablog/util/freenet/fcp/ClientPutComplexDir.java	2008-02-13 03:37:20 UTC (rev 17861)
@@ -29,7 +29,7 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import de.todesbaum.util.io.Closer;
+import src.net.sf.thingamablog.util.io.Closer;
 
 /**
  * Implementation of the <code>ClientPutComplexDir</code> command. This

Modified: trunk/apps/thingamablog/src/net/sf/thingamablog/util/freenet/fcp/Connection.java
===================================================================
--- trunk/apps/thingamablog/src/net/sf/thingamablog/util/freenet/fcp/Connection.java	2008-02-13 03:26:33 UTC (rev 17860)
+++ trunk/apps/thingamablog/src/net/sf/thingamablog/util/freenet/fcp/Connection.java	2008-02-13 03:37:20 UTC (rev 17861)
@@ -31,10 +31,10 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import de.todesbaum.util.io.Closer;
-import de.todesbaum.util.io.LineInputStream;
-import de.todesbaum.util.io.StreamCopier;
-import de.todesbaum.util.io.TempFileInputStream;
+import src.net.sf.thingamablog.util.io.Closer;
+import src.net.sf.thingamablog.util.io.LineInputStream;
+import src.net.sf.thingamablog.util.io.StreamCopier;
+import src.net.sf.thingamablog.util.io.TempFileInputStream;
 
 /**
  * A physical connection to a Freenet node.

Added: trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/Closer.java
===================================================================
--- trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/Closer.java	                        (rev 0)
+++ trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/Closer.java	2008-02-13 03:37:20 UTC (rev 17861)
@@ -0,0 +1,189 @@
+/*
+ * todesbaum-lib - Copyright (C) 2006 David Roden
+ * 
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 2 of the License, or (at your option) any later
+ * version.
+ * 
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package src.net.sf.thingamablog.util.io;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * Helper class that can close all kinds of resources without throwing exception
+ * so that clean-up code can be written with less code. All methods check that
+ * the given resource is not <code>null</code> before invoking the close()
+ * method of the respective type.
+ * 
+ * @author <a href="mailto:bombe at freenetproject.org">David &lsquo;Bombe&squo;
+ *         Roden</a>
+ * @version $Id: Closer.java 15384 2007-09-29 12:11:36Z bombe $
+ */
+public class Closer {
+
+	/**
+	 * Closes the given result set.
+	 * 
+	 * @param resultSet
+	 *            The result set to close
+	 * @see ResultSet#close()
+	 */
+	public static void close(ResultSet resultSet) {
+		if (resultSet != null) {
+			try {
+				resultSet.close();
+			} catch (SQLException ioe1) {
+			}
+		}
+	}
+
+	/**
+	 * Closes the given statement.
+	 * 
+	 * @param statement
+	 *            The statement to close
+	 * @see Statement#close()
+	 */
+	public static void close(Statement statement) {
+		if (statement != null) {
+			try {
+				statement.close();
+			} catch (SQLException ioe1) {
+			}
+		}
+	}
+
+	/**
+	 * Closes the given connection.
+	 * 
+	 * @param connection
+	 *            The connection to close
+	 * @see Connection#close()
+	 */
+	public static void close(Connection connection) {
+		if (connection != null) {
+			try {
+				connection.close();
+			} catch (SQLException ioe1) {
+			}
+		}
+	}
+
+	/**
+	 * Closes the given server socket.
+	 * 
+	 * @param serverSocket
+	 *            The server socket to close
+	 * @see ServerSocket#close()
+	 */
+	public static void close(ServerSocket serverSocket) {
+		if (serverSocket != null) {
+			try {
+				serverSocket.close();
+			} catch (IOException ioe1) {
+			}
+		}
+	}
+
+	/**
+	 * Closes the given socket.
+	 * 
+	 * @param socket
+	 *            The socket to close
+	 * @see Socket#close()
+	 */
+	public static void close(Socket socket) {
+		if (socket != null) {
+			try {
+				socket.close();
+			} catch (IOException ioe1) {
+			}
+		}
+	}
+
+	/**
+	 * Closes the given input stream.
+	 * 
+	 * @param inputStream
+	 *            The input stream to close
+	 * @see InputStream#close()
+	 */
+	public static void close(InputStream inputStream) {
+		if (inputStream != null) {
+			try {
+				inputStream.close();
+			} catch (IOException ioe1) {
+			}
+		}
+	}
+
+	/**
+	 * Closes the given output stream.
+	 * 
+	 * @param outputStream
+	 *            The output stream to close
+	 * @see OutputStream#close()
+	 */
+	public static void close(OutputStream outputStream) {
+		if (outputStream != null) {
+			try {
+				outputStream.close();
+			} catch (IOException ioe1) {
+			}
+		}
+	}
+
+	/**
+	 * Closes the given reader.
+	 * 
+	 * @param reader
+	 *            The reader to close
+	 * @see Reader#close()
+	 */
+	public static void close(Reader reader) {
+		if (reader != null) {
+			try {
+				reader.close();
+			} catch (IOException ioe1) {
+			}
+		}
+	}
+
+	/**
+	 * Closes the given writer.
+	 * 
+	 * @param writer
+	 *            The write to close
+	 * @see Writer#close()
+	 */
+	public static void close(Writer writer) {
+		if (writer != null) {
+			try {
+				writer.close();
+			} catch (IOException ioe1) {
+			}
+		}
+	}
+
+}

Added: trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/LineInputStream.java
===================================================================
--- trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/LineInputStream.java	                        (rev 0)
+++ trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/LineInputStream.java	2008-02-13 03:37:20 UTC (rev 17861)
@@ -0,0 +1,64 @@
+/*
+ * todesbaum-lib - 
+ * Copyright (C) 2006 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package src.net.sf.thingamablog.util.io;
+
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+
+/**
+ * @author David Roden &lt;droden at gmail.com&gt;
+ * @version $Id: LineInputStream.java 9647 2006-07-17 18:24:50Z bombe $
+ */
+public class LineInputStream extends FilterInputStream {
+	
+	private boolean skipLinefeed = false;
+	private StringBuffer lineBuffer = new StringBuffer();
+
+	/**
+	 * @param in
+	 */
+	public LineInputStream(InputStream in) {
+		super(in);
+	}
+
+	public synchronized String readLine() throws IOException {
+		lineBuffer.setLength(0);
+		int c = 0;
+		while (c != -1) {
+			c = read();
+			if ((c == -1) && lineBuffer.length() == 0)
+				return null;
+			if (skipLinefeed && (c == '\n')) {
+				skipLinefeed = false;
+				continue;
+			}
+			skipLinefeed = (c == '\r');
+			if ((c == '\r') || (c == '\n')) {
+				c = -1;
+			} else {
+				lineBuffer.append((char) c);
+			}
+		}
+		return lineBuffer.toString();
+	}
+	
+}

Added: trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/ReplacingOutputStream.java
===================================================================
--- trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/ReplacingOutputStream.java	                        (rev 0)
+++ trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/ReplacingOutputStream.java	2008-02-13 03:37:20 UTC (rev 17861)
@@ -0,0 +1,83 @@
+/*
+ * todesbaum-lib - 
+ * Copyright (C) 2006 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package src.net.sf.thingamablog.util.io;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+
+
+/**
+ * @author David Roden &lt;droden at gmail.com&gt;
+ * @version $Id: ReplacingOutputStream.java 9647 2006-07-17 18:24:50Z bombe $
+ */
+public class ReplacingOutputStream extends FilterOutputStream {
+
+	private Map<String, String> replacements = new HashMap<String, String>();
+	private StringBuffer ringBuffer = new StringBuffer();
+	
+	/**
+	 * @param out
+	 */
+	public ReplacingOutputStream(OutputStream out) {
+		super(out);
+	}
+
+	public void addReplacement(String token, String value) {
+		replacements.put(token, value);
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void write(int b) throws IOException {
+		ringBuffer.append((char) b);
+		Iterator<Entry<String, String>> entries = replacements.entrySet().iterator();
+		boolean found = false;
+		Entry<String, String> entry = null;
+		while (!found && entries.hasNext()) {
+			entry = entries.next();
+			if (entry.getKey().startsWith(ringBuffer.toString())) {
+				found = true;
+			}
+		}
+		if (!found) {
+			String buffer = ringBuffer.toString();
+			for (int index = 0, size = buffer.length(); index < size; index++) {
+				super.write(buffer.charAt(index));
+			}
+			ringBuffer.setLength(0);
+		} else {
+			if (entry.getKey().equals(ringBuffer.toString())) {
+				String buffer = entry.getValue();
+				for (int index = 0, size = buffer.length(); index < size; index++) {
+					super.write(buffer.charAt(index));
+				}
+				ringBuffer.setLength(0);
+			}
+		}
+	}
+	
+}

Added: trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/StreamCopier.java
===================================================================
--- trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/StreamCopier.java	                        (rev 0)
+++ trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/StreamCopier.java	2008-02-13 03:37:20 UTC (rev 17861)
@@ -0,0 +1,152 @@
+/*
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 2 of the License, or (at your option) any later
+ * version.
+ * 
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package src.net.sf.thingamablog.util.io;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * Copies input from an {@link InputStream} to an {@link OutputStream}.
+ * 
+ * @author <a href="mailto:droden at gmail.com">David Roden</a>
+ * @version $Id: StreamCopier.java 9647 2006-07-17 18:24:50Z bombe $
+ */
+public class StreamCopier {
+
+	/**
+	 * The default size of the buffer.
+	 */
+	private static final int BUFFER_SIZE = 64 * 1024;
+
+	/**
+	 * The {@link InputStream} to read from.
+	 */
+	private InputStream inputStream;
+
+	/**
+	 * The {@link OutputStream} to write to.
+	 */
+	private OutputStream outputStream;
+
+	/**
+	 * The number of bytes to copy.
+	 */
+	private long length;
+
+	/**
+	 * The size of the buffer.
+	 */
+	private int bufferSize;
+
+	/**
+	 * Creates a new StreamCopier with the specified parameters and the default
+	 * buffer size.
+	 * 
+	 * @param inputStream
+	 *            The {@link InputStream} to read from
+	 * @param outputStream
+	 *            The {@link OutputStream} to write to
+	 * @param length
+	 *            The number of bytes to copy
+	 */
+	public StreamCopier(InputStream inputStream, OutputStream outputStream, long length) {
+		this(inputStream, outputStream, length, BUFFER_SIZE);
+	}
+
+	/**
+	 * Creates a new StreamCopier with the specified parameters and the default
+	 * buffer size.
+	 * 
+	 * @param inputStream
+	 *            The {@link InputStream} to read from
+	 * @param outputStream
+	 *            The {@link OutputStream} to write to
+	 * @param length
+	 *            The number of bytes to copy
+	 * @param bufferSize
+	 *            The number of bytes to copy at a time
+	 */
+	public StreamCopier(InputStream inputStream, OutputStream outputStream, long length, int bufferSize) {
+		this.inputStream = inputStream;
+		this.outputStream = outputStream;
+		this.length = length;
+		this.bufferSize = bufferSize;
+	}
+
+	/**
+	 * Copies the stream data. If the input stream is depleted before the
+	 * requested number of bytes have been read an {@link EOFException} is
+	 * thrown.
+	 * 
+	 * @throws EOFException
+	 *             if the input stream is depleted before the requested number
+	 *             of bytes has been read
+	 * @throws IOException
+	 *             if an I/O error occurs
+	 */
+	public void copy() throws EOFException, IOException {
+		copy(inputStream, outputStream, length, bufferSize);
+	}
+
+	/**
+	 * Copies <code>length</code> bytes from the <code>inputStream</code> to
+	 * the <code>outputStream</code>.
+	 * 
+	 * @param inputStream
+	 *            The input stream to read from
+	 * @param outputStream
+	 *            The output stream to write to
+	 * @param length
+	 *            The number of bytes to copy
+	 * @throws IOException
+	 *             if an I/O exception occurs
+	 */
+	public static void copy(InputStream inputStream, OutputStream outputStream, long length) throws IOException {
+		copy(inputStream, outputStream, length, BUFFER_SIZE);
+	}
+
+	/**
+	 * Copies <code>length</code> bytes from the <code>inputStream</code> to
+	 * the <code>outputStream</code> using a buffer with the specified size
+	 * 
+	 * @param inputStream
+	 *            The input stream to read from
+	 * @param outputStream
+	 *            The output stream to write to
+	 * @param length
+	 *            The number of bytes to copy
+	 * @param bufferSize
+	 *            The size of the copy buffer
+	 * @throws IOException
+	 *             if an I/O exception occurs
+	 */
+	public static void copy(InputStream inputStream, OutputStream outputStream, long length, int bufferSize) throws IOException {
+		long remaining = length;
+		byte[] buffer = new byte[bufferSize];
+		while (remaining > 0) {
+			int read = inputStream.read(buffer, 0, (int) Math.min(Integer.MAX_VALUE, Math.min(bufferSize, remaining)));
+			if (read == -1) {
+				throw new EOFException();
+			}
+			outputStream.write(buffer, 0, read);
+			remaining -= read;
+		}
+	}
+
+}

Added: trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/TempFileInputStream.java
===================================================================
--- trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/TempFileInputStream.java	                        (rev 0)
+++ trunk/apps/thingamablog/src/net/sf/thingamablog/util/io/TempFileInputStream.java	2008-02-13 03:37:20 UTC (rev 17861)
@@ -0,0 +1,57 @@
+/*
+ * todesbaum-lib - 
+ * Copyright (C) 2006 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package src.net.sf.thingamablog.util.io;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+/**
+ * @author David Roden &lt;droden at gmail.com&gt;
+ * @version $Id: TempFileInputStream.java 8492 2006-04-07 11:56:30Z bombe $
+ */
+public class TempFileInputStream extends FileInputStream {
+
+	private File tempFile;
+
+	/**
+	 * @param name
+	 * @throws FileNotFoundException
+	 */
+	public TempFileInputStream(String name) throws FileNotFoundException {
+		this(new File(name));
+	}
+
+	/**
+	 * @param file
+	 * @throws FileNotFoundException
+	 */
+	public TempFileInputStream(File file) throws FileNotFoundException {
+		super(file);
+		tempFile = file;
+	}
+
+	public void close() throws IOException {
+		super.close();
+		tempFile.delete();
+	}
+
+}




More information about the cvs mailing list