[seam-commits] Seam SVN: r10267 - trunk/ui/src/main/java/org/jboss/seam/ui.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Wed Apr 1 14:05:44 EDT 2009
Author: norman.richards at jboss.com
Date: 2009-04-01 14:05:44 -0400 (Wed, 01 Apr 2009)
New Revision: 10267
Modified:
trunk/ui/src/main/java/org/jboss/seam/ui/DocumentStoreUtils.java
Log:
JBSEAM-4046
Modified: trunk/ui/src/main/java/org/jboss/seam/ui/DocumentStoreUtils.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/DocumentStoreUtils.java 2009-04-01 11:48:55 UTC (rev 10266)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/DocumentStoreUtils.java 2009-04-01 18:05:44 UTC (rev 10267)
@@ -5,6 +5,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
import javax.faces.context.FacesContext;
@@ -17,64 +21,85 @@
public class DocumentStoreUtils
{
+ private static final int BUFFER_SIZE = 32768;
- public static String addResourceToDataStore(FacesContext ctx, UIResource resource)
- {
- String baseName = Pages.getCurrentBaseName();
- String viewId = Pages.getViewId(ctx);
+ public static String addResourceToDataStore(FacesContext ctx, UIResource resource) {
+ String baseName = Pages.getCurrentBaseName();
+ String viewId = Pages.getViewId(ctx);
- DocumentStore store = DocumentStore.instance();
- String id = store.newId();
+ DocumentStore store = DocumentStore.instance();
+ String id = store.newId();
- DocumentType type = new DocumentType("", resource.getContentType());
+ DocumentType type = new DocumentType("", resource.getContentType());
- DocumentData documentData = new DownloadableDocumentData(baseName, type, resource.getData());
- documentData.setFilename(resource.getFileName());
- documentData.setDisposition(resource.getDisposition());
+ DocumentData documentData = new DownloadableDocumentData(baseName, type, resource.getData());
+ documentData.setFilename(resource.getFileName());
+ documentData.setDisposition(resource.getDisposition());
- String url = store.preferredUrlForContent(resource.getFileName(), type.getExtension(), id);
- url = Manager.instance().encodeConversationId(url, viewId);
- store.saveData(id, documentData);
- return url;
- }
+ String url = store.preferredUrlForContent(resource.getFileName(), type.getExtension(), id);
+ url = Manager.instance().encodeConversationId(url, viewId);
+ store.saveData(id, documentData);
+ return url;
+ }
- static class DownloadableDocumentData extends DocumentData
- {
+ static class DownloadableDocumentData
+ extends DocumentData
+ {
+ private Object data;
- private Object data;
+ public DownloadableDocumentData(String baseName, DocumentType type, Object data) {
+ super(baseName, type);
+ this.data = data;
+ }
- public DownloadableDocumentData(String baseName, DocumentType type, Object data)
- {
- super(baseName, type);
- this.data = data;
- }
+ @Override
+ public void writeDataToStream(OutputStream os)
+ throws IOException
+ {
+ if (data instanceof byte[]) {
+ os.write((byte[]) data);
+ } else if (data instanceof File) {
+ writeStream(os, new FileInputStream((File) data));
+ } else if (data instanceof InputStream) {
+ writeStream(os, (InputStream) data);
+ }
+ }
- @Override
- public void writeDataToStream(OutputStream os) throws IOException
- {
- if (data instanceof byte[])
- {
- os.write((byte[]) data);
- }
- else if (data instanceof File)
- {
- writeStream(os, new FileInputStream((File) data));
- }
- else if (data instanceof InputStream)
- {
- writeStream(os, (InputStream) data);
- }
+ private void writeStream(OutputStream os, InputStream is)
+ throws IOException
+
+ {
+ ReadableByteChannel in = Channels.newChannel(is);
+ WritableByteChannel out = Channels.newChannel(os);
+
+ try {
+ copyChannel(in, out);
+ } finally {
+ try {
+ in.close();
+ } catch (IOException e) {
+ // eat it
+ }
+
+ try {
+ out.close();
+ } catch (IOException e) {
+ // eat it
+ }
+ }
+ }
+
+ private void copyChannel(ReadableByteChannel in, WritableByteChannel out)
+ throws IOException
+ {
+ ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
+
+ while (in.read(buffer) != -1 || buffer.position() > 0) {
+ buffer.flip();
+ out.write(buffer);
+ buffer.compact();
+ }
+ }
+ }
- }
-
- private void writeStream(OutputStream os, InputStream is) throws IOException
- {
- while (is.available() > 0)
- {
- os.write(is.read());
- }
- }
-
- }
-
}
More information about the seam-commits
mailing list