You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DownloadStream.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal;
  5. import java.io.InputStream;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import java.util.Map;
  9. /**
  10. * Downloadable stream.
  11. *
  12. * @author IT Mill Ltd.
  13. * @version
  14. * @VERSION@
  15. * @since 3.0
  16. */
  17. public class DownloadStream {
  18. /**
  19. * Maximum cache time.
  20. */
  21. public static final long MAX_CACHETIME = Long.MAX_VALUE;
  22. /**
  23. * Default cache time.
  24. */
  25. public static final long DEFAULT_CACHETIME = 1000 * 60 * 60 * 24;
  26. private InputStream stream;
  27. private String contentType;
  28. private String fileName;
  29. private Map params;
  30. private long cacheTime = DEFAULT_CACHETIME;
  31. private int bufferSize = 0;
  32. /**
  33. * Creates a new instance of DownloadStream.
  34. */
  35. public DownloadStream(InputStream stream, String contentType,
  36. String fileName) {
  37. setStream(stream);
  38. setContentType(contentType);
  39. setFileName(fileName);
  40. }
  41. /**
  42. * Gets downloadable stream.
  43. *
  44. * @return output stream.
  45. */
  46. public InputStream getStream() {
  47. return stream;
  48. }
  49. /**
  50. * Sets the stream.
  51. *
  52. * @param stream
  53. * The stream to set
  54. */
  55. public void setStream(InputStream stream) {
  56. this.stream = stream;
  57. }
  58. /**
  59. * Gets stream content type.
  60. *
  61. * @return type of the stream content.
  62. */
  63. public String getContentType() {
  64. return contentType;
  65. }
  66. /**
  67. * Sets stream content type.
  68. *
  69. * @param contentType
  70. * the contentType to set
  71. */
  72. public void setContentType(String contentType) {
  73. this.contentType = contentType;
  74. }
  75. /**
  76. * Returns the file name.
  77. *
  78. * @return the name of the file.
  79. */
  80. public String getFileName() {
  81. return fileName;
  82. }
  83. /**
  84. * Sets the file name.
  85. *
  86. * @param fileName
  87. * the file name to set.
  88. */
  89. public void setFileName(String fileName) {
  90. this.fileName = fileName;
  91. }
  92. /**
  93. * Sets a paramater for download stream. Parameters are optional information
  94. * about the downloadable stream and their meaning depends on the used
  95. * adapter. For example in WebAdapter they are interpreted as HTTP response
  96. * headers.
  97. *
  98. * If the parameters by this name exists, the old value is replaced.
  99. *
  100. * @param name
  101. * the Name of the parameter to set.
  102. * @param value
  103. * the Value of the parameter to set.
  104. */
  105. public void setParameter(String name, String value) {
  106. if (params == null) {
  107. params = new HashMap();
  108. }
  109. params.put(name, value);
  110. }
  111. /**
  112. * Gets a paramater for download stream. Parameters are optional information
  113. * about the downloadable stream and their meaning depends on the used
  114. * adapter. For example in WebAdapter they are interpreted as HTTP response
  115. * headers.
  116. *
  117. * @param name
  118. * the Name of the parameter to set.
  119. * @return Value of the parameter or null if the parameter does not exist.
  120. */
  121. public String getParameter(String name) {
  122. if (params != null) {
  123. return (String) params.get(name);
  124. }
  125. return null;
  126. }
  127. /**
  128. * Gets the names of the parameters.
  129. *
  130. * @return Iterator of names or null if no parameters are set.
  131. */
  132. public Iterator getParameterNames() {
  133. if (params != null) {
  134. return params.keySet().iterator();
  135. }
  136. return null;
  137. }
  138. /**
  139. * Gets length of cache expiration time. This gives the adapter the
  140. * possibility cache streams sent to the client. The caching may be made in
  141. * adapter or at the client if the client supports caching. Default is
  142. * <code>DEFAULT_CACHETIME</code>.
  143. *
  144. * @return Cache time in milliseconds
  145. */
  146. public long getCacheTime() {
  147. return cacheTime;
  148. }
  149. /**
  150. * Sets length of cache expiration time. This gives the adapter the
  151. * possibility cache streams sent to the client. The caching may be made in
  152. * adapter or at the client if the client supports caching. Zero or negavive
  153. * value disbales the caching of this stream.
  154. *
  155. * @param cacheTime
  156. * the cache time in milliseconds.
  157. */
  158. public void setCacheTime(long cacheTime) {
  159. this.cacheTime = cacheTime;
  160. }
  161. /**
  162. * Gets the size of the download buffer.
  163. *
  164. * @return int The size of the buffer in bytes.
  165. */
  166. public int getBufferSize() {
  167. return bufferSize;
  168. }
  169. /**
  170. * Sets the size of the download buffer.
  171. *
  172. * @param bufferSize
  173. * the size of the buffer in bytes.
  174. */
  175. public void setBufferSize(int bufferSize) {
  176. this.bufferSize = bufferSize;
  177. }
  178. }