X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fde%2Ftodesbaum%2Futil%2Ffreenet%2Ffcp2%2FClientPutComplexDir.java;h=be61b81028e4e8808ed5fb5a53d598435777fcb4;hb=4aa8626a4290644be70b2fe35de7c9d392d4574f;hp=237cb1c79f2d3c3a1f2c9f8853cdf758af721493;hpb=b80ec05dc6609b273b111469af0ec040f8ea0b42;p=jSite.git
diff --git a/src/de/todesbaum/util/freenet/fcp2/ClientPutComplexDir.java b/src/de/todesbaum/util/freenet/fcp2/ClientPutComplexDir.java
index 237cb1c..be61b81 100644
--- a/src/de/todesbaum/util/freenet/fcp2/ClientPutComplexDir.java
+++ b/src/de/todesbaum/util/freenet/fcp2/ClientPutComplexDir.java
@@ -1,5 +1,5 @@
/*
- * todesbaum-lib -
+ * todesbaum-lib -
* Copyright (C) 2006 David Roden
*
* This program is free software; you can redistribute it and/or modify
@@ -32,65 +32,99 @@ import java.util.List;
import de.todesbaum.util.io.Closer;
/**
- * Implementation of the ClientPutComplexDir
command. This
- * command can be used to insert directories that do not exist on disk.
- *
+ * Implementation of the ClientPutComplexDir
command. This command
+ * can be used to insert directories that do not exist on disk.
+ *
* @author David Roden <droden@gmail.com>
* @version $Id$
*/
-public class ClientPutComplexDir extends ClientPutDir {
+public class ClientPutComplexDir extends ClientPutDir {
/** The file entries of this directory. */
private List fileEntries = new ArrayList();
-
+
/** Whether this request has payload. */
private boolean hasPayload = false;
-
+
/** The input streams for the payload. */
private File payloadFile;
-
+
/** The total number of bytes of the payload. */
private long payloadLength = 0;
+ /** The temp directory to use. */
+ private final String tempDirectory;
+
/**
- * Creates a new ClientPutComplexDir
command with the specified identifier and URI.
- * @param identifier The identifier of the command
- * @param uri The URI of the command
+ * Creates a new ClientPutComplexDir
command with the specified
+ * identifier and URI.
+ *
+ * @param identifier
+ * The identifier of the command
+ * @param uri
+ * The URI of the command
*/
public ClientPutComplexDir(String identifier, String uri) {
+ this(identifier, uri, null);
+ }
+
+ /**
+ * Creates a new ClientPutComplexDir
command with the specified
+ * identifier and URI.
+ *
+ * @param identifier
+ * The identifier of the command
+ * @param uri
+ * The URI of the command
+ * @param tempDirectory
+ * The temp directory to use, or {@code null} to use the default
+ * temp directory
+ */
+ public ClientPutComplexDir(String identifier, String uri, String tempDirectory) {
super("ClientPutComplexDir", identifier, uri);
+ this.tempDirectory = tempDirectory;
}
/**
* Adds a file to the directory inserted by this request.
- * @param fileEntry The file entry to add to the directory
+ *
+ * @param fileEntry
+ * The file entry to add to the directory
+ * @throws IOException
+ * if an I/O error occurs when creating the payload stream
*/
- public void addFileEntry(FileEntry fileEntry) {
- if (payloadFile == null){
- try {
- payloadFile = File.createTempFile("payload", ".dat");
- payloadFile.deleteOnExit();
- } catch (IOException e) {
+ public void addFileEntry(FileEntry fileEntry) throws IOException {
+ if (fileEntry instanceof DirectFileEntry) {
+ if (payloadFile == null) {
+ try {
+ payloadFile = File.createTempFile("payload", ".dat", (tempDirectory != null) ? new File(tempDirectory) : null);
+ payloadFile.deleteOnExit();
+ } catch (IOException e) {
+ /* ignore. */
+ }
}
- }
- if (payloadFile != null) {
- InputStream payloadInputStream = ((DirectFileEntry) fileEntry).getDataInputStream();
- FileOutputStream payloadOutputStream = null;
- try {
- payloadOutputStream = new FileOutputStream(payloadFile, true);
- byte[] buffer = new byte[65536];
- int read = 0;
- while ((read = payloadInputStream.read(buffer)) != -1) {
- payloadOutputStream.write(buffer, 0, read);
+ if (payloadFile != null) {
+ InputStream payloadInputStream = ((DirectFileEntry) fileEntry).getDataInputStream();
+ FileOutputStream payloadOutputStream = null;
+ try {
+ payloadOutputStream = new FileOutputStream(payloadFile, true);
+ byte[] buffer = new byte[65536];
+ int read = 0;
+ while ((read = payloadInputStream.read(buffer)) != -1) {
+ payloadOutputStream.write(buffer, 0, read);
+ }
+ payloadOutputStream.flush();
+ fileEntries.add(fileEntry);
+ } catch (IOException ioe1) {
+ payloadFile.delete();
+ throw ioe1;
+ } finally {
+ Closer.close(payloadOutputStream);
+ Closer.close(payloadInputStream);
}
- payloadOutputStream.flush();
- fileEntries.add(fileEntry);
- } catch (IOException ioe1) {
- /* hmm, ignore? */
- } finally {
- Closer.close(payloadOutputStream);
- Closer.close(payloadInputStream);
}
+ } else {
+ fileEntries.add(fileEntry);
}
}
@@ -101,7 +135,7 @@ public class ClientPutComplexDir extends ClientPutDir {
protected void write(Writer writer) throws IOException {
super.write(writer);
int fileIndex = 0;
- for (FileEntry fileEntry: fileEntries) {
+ for (FileEntry fileEntry : fileEntries) {
writer.write("Files." + fileIndex + ".Name=" + fileEntry.getFilename() + LINEFEED);
if (fileEntry.getContentType() != null) {
writer.write("Files." + fileIndex + ".Metadata.ContentType=" + fileEntry.getContentType() + LINEFEED);