From 40d207199006f2f1ef2f14a5a90f1f08eddeaaba Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Thu, 30 Dec 2010 16:45:32 +0000 Subject: [PATCH] Test and fix for #5356 svn changeset:16738/svn branch:6.5 --- src/com/vaadin/terminal/FileResource.java | 3 + .../resources/DownloadLargeFileResource.java | 71 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/src/com/vaadin/tests/resources/DownloadLargeFileResource.java diff --git a/src/com/vaadin/terminal/FileResource.java b/src/com/vaadin/terminal/FileResource.java index 571622b0e4..e1c866189d 100644 --- a/src/com/vaadin/terminal/FileResource.java +++ b/src/com/vaadin/terminal/FileResource.java @@ -65,6 +65,9 @@ public class FileResource implements ApplicationResource { try { final DownloadStream ds = new DownloadStream(new FileInputStream( sourceFile), getMIMEType(), getFilename()); + ds.setParameter("Content-Length", + String.valueOf(sourceFile.length())); + ds.setCacheTime(cacheTime); return ds; } catch (final FileNotFoundException e) { diff --git a/tests/src/com/vaadin/tests/resources/DownloadLargeFileResource.java b/tests/src/com/vaadin/tests/resources/DownloadLargeFileResource.java new file mode 100644 index 0000000000..3d7379aafe --- /dev/null +++ b/tests/src/com/vaadin/tests/resources/DownloadLargeFileResource.java @@ -0,0 +1,71 @@ +package com.vaadin.tests.resources; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +import com.vaadin.terminal.FileResource; +import com.vaadin.tests.components.TestBase; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; + +public class DownloadLargeFileResource extends TestBase { + + private FileResource hugeFileResource = null; + private long fileSize = (long) (1233.2 * 1024.0 * 1024.0); + + @Override + protected void setup() { + Button b = new Button( + "Download a " + + String.format("%.1f", fileSize / 1024.0 / 1024.0) + + "MB file", new Button.ClickListener() { + + public void buttonClick(ClickEvent event) { + download(); + } + }); + addComponent(b); + } + + protected void download() { + if (hugeFileResource == null) { + createFile(); + } + + getMainWindow().open(hugeFileResource); + + } + + private void createFile() { + try { + File hugeFile = File.createTempFile("huge", ".txt"); + hugeFile.deleteOnExit(); + BufferedOutputStream os = new BufferedOutputStream( + new FileOutputStream(hugeFile)); + int writeAtOnce = 1024 * 1024; + byte[] b = new byte[writeAtOnce]; + for (int i = 0; i < fileSize; i += writeAtOnce) { + os.write(b); + } + os.close(); + hugeFileResource = new FileResource(hugeFile, this); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Override + protected String getDescription() { + return "Click the button to download huge-file.txt. The file is generated on the first download."; + } + + @Override + protected Integer getTicketNumber() { + return 5356; + } + +} -- 2.39.5