Update of /var/cvs/src/org/mmbase/util
In directory james.mmbase.org:/tmp/cvs-serv13741
Modified Files:
Casting.java SerializableInputStream.java
Log Message:
MMB-1810
See also: http://cvs.mmbase.org/viewcvs/src/org/mmbase/util
See also: http://www.mmbase.org/jira/browse/MMB-1810
Index: Casting.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/util/Casting.java,v
retrieving revision 1.130
retrieving revision 1.131
diff -u -b -r1.130 -r1.131
--- Casting.java 21 Apr 2009 12:37:01 -0000 1.130
+++ Casting.java 24 Apr 2009 15:08:51 -0000 1.131
@@ -16,7 +16,7 @@
*
* @author Michiel Meeuwissen
* @since MMBase-1.6
- * @version $Id: Casting.java,v 1.130 2009/04/21 12:37:01 michiel Exp $
+ * @version $Id: Casting.java,v 1.131 2009/04/24 15:08:51 michiel Exp $
*/
import java.util.*;
@@ -407,9 +407,11 @@
return escape(escaper, (String) o);
} else if (o instanceof CharSequence) {
return new StringWrapper((CharSequence) o, escaper);
+ } else if (o instanceof SerializableInputStream) {
+ return escape(escaper, ((SerializableInputStream) o).getName());
} else if (o instanceof InputStream) {
try {
- return escape(escaper, new
String(toSerializableInputStream(o).toByteArray()));
+ return escape(escaper, new
String(toSerializableInputStream(o).get()));
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
@@ -591,16 +593,20 @@
return (byte[])obj;
} else if (obj instanceof FileItem) {
return ((FileItem) obj).get();
+ } else if (obj instanceof SerializableInputStream) {
+ try {
+ SerializableInputStream is = (SerializableInputStream) obj;
+ return is.get();
+ } catch (IOException ioe) {
+ log.error(ioe);
+ return new byte[0];
+ }
} else if (obj instanceof InputStream) {
log.debug("IS " + obj);
InputStream in = (InputStream) obj;
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
- byte[] buf = new byte[1024];
try {
- int tot;
- while ((tot = in.read(buf, 0, 1024)) != -1 ) {
- out.write(buf, 0, tot);
- }
+ IOUtil.copy(in, out);
} catch (IOException ioe) {
log.error(ioe);
} finally {
@@ -629,7 +635,7 @@
try {
return new SerializableInputStream((FileItem) obj);
} catch (IOException ioe) {
- log.error(ioe);
+ log.error(ioe.getMessage(), ioe);
return new SerializableInputStream(new byte[0]);
}
} else {
Index: SerializableInputStream.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/util/SerializableInputStream.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- SerializableInputStream.java 21 Apr 2009 12:37:01 -0000 1.5
+++ SerializableInputStream.java 24 Apr 2009 15:08:51 -0000 1.6
@@ -13,6 +13,7 @@
import java.io.*;
import org.mmbase.util.logging.*;
import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.disk.DiskFileItem;
/**
* Sometimes you need an InputStream to be Serializable. This wraps
@@ -20,7 +21,7 @@
*
* @since MMBase-1.9
* @author Michiel Meeuwissen
- * @version $Id: SerializableInputStream.java,v 1.5 2009/04/21 12:37:01
michiel Exp $
+ * @version $Id: SerializableInputStream.java,v 1.6 2009/04/24 15:08:51
michiel Exp $
* @todo IllegalStateException or so, if the inputstreas is used (already).
*/
@@ -59,7 +60,9 @@
private InputStream wrapped;
+ private File file = null;
private String name;
+ private String contentType;
public SerializableInputStream(InputStream wrapped, long s) {
this.wrapped = wrapped;
@@ -72,19 +75,34 @@
this.size = array.length;
this.name = null;
}
+
public SerializableInputStream(FileItem fi) throws IOException {
- this.wrapped = fi.getInputStream();
this.size = fi.getSize();
this.name = fi.getName();
+ this.contentType = fi.getContentType();
+ file = File.createTempFile(getClass().getName(), this.name);
+ try {
+ fi.write(file);
+ } catch (Exception e) {
+ throw new IOException(e);
+ }
+ this.wrapped = new FileInputStream(file);
+
+
}
+
public long getSize() {
return size;
}
public String getName() {
return name;
}
- public byte[] toByteArray() throws IOException {
+
+ public String getContentType() {
+ return contentType;
+ }
+ public byte[] get() throws IOException {
if (wrapped.markSupported()) {
byte[] b = toByteArray(wrapped);
wrapped.reset();
@@ -96,6 +114,36 @@
}
}
+ public void moveTo(File f) {
+ if (name == null) {
+ name = f.getName();
+ }
+ if (file != null) {
+ if (file.equals(f)) {
+ log.debug("File is already there " + f);
+ return;
+ } else if (file.renameTo(f)) {
+ log.debug("Renamed " + file + " to " + f);
+ file = f;
+ return;
+ } else {
+ log.debug("Could not rename " + file + " to " + f + " will
copy/delete in stead");
+ }
+ }
+ try {
+ FileOutputStream os = new FileOutputStream(f);
+ IOUtil.copy(wrapped, os);
+ os.close();
+ wrapped = new FileInputStream(f);
+ if (file != null) {
+ file.delete();
+ }
+ file = f;
+ } catch (IOException ioe) {
+ throw new RuntimeException(ioe);
+ }
+ }
+
private void writeObject(java.io.ObjectOutputStream out) throws
IOException {
wrapped.reset();
out.writeObject(toByteArray(wrapped));
@@ -104,16 +152,50 @@
byte[] b = (byte[]) in.readObject();
wrapped = new ByteArrayInputStream(b);
}
- public int available() throws IOException { return wrapped.available(); }
- public void mark(int readlimit) { wrapped.mark(readlimit); }
- public boolean markSupported() { return wrapped.markSupported(); }
+ public int available() throws IOException {
+ return wrapped.available();
+ }
+ private void supportMark() {
+ try {
+ if (file == null) {
+ file = File.createTempFile(getClass().getName(), this.name);
+ FileOutputStream os = new FileOutputStream(file);
+ IOUtil.copy(wrapped, os);
+ os.close();
+ }
+ wrapped = new FileInputStream(file);
+ } catch (IOException ioe) {
+ throw new RuntimeException(ioe);
+ }
+ }
+
+ public void mark(int readlimit) {
+ if (wrapped.markSupported()) {
+ wrapped.mark(readlimit);
+ } else {
+ supportMark();
+ wrapped.mark(readlimit);
+ }
+ }
+ public boolean markSupported() {
+ return true;
+ }
public int read() throws IOException { use(); return wrapped.read(); }
public int read(byte[] b) throws IOException { use(); return
wrapped.read(b); }
public int read(byte[] b, int off, int len) throws IOException { use();
return wrapped.read(b, off, len); }
- public void reset() throws IOException { wrapped.reset() ; }
- public long skip(long n) throws IOException { return wrapped.skip(n); }
+
+ public void reset() throws IOException {
+ if (wrapped.markSupported()) {
+ wrapped.reset() ;
+ } else {
+ supportMark();
+ }
+ }
+ public long skip(long n) throws IOException {
+ return wrapped.skip(n);
+ }
public String toString() {
- return "SERIALIZABLE " + wrapped + (used ? " (used)" : "") + "(" +
size + " byte)";
+ return "SERIALIZABLE " + wrapped + (used ? " (used)" : "") + " (" +
size + " byte, " + ( name == null ? "[no name]" : name) + ")";
}
}
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs