Guest User

StreamUtils

a guest
May 26th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1.  
  2. public static String getContentDisposition(Response response, String filename) throws Exception {
  3.     String disposition = "attachment; ";
  4.     if (asciiEncoder.canEncode(filename))
  5.         disposition += "filename=\"" + filename + "\"";
  6.     else {
  7.         String encodedFilename = urlEncoder.encode(filename, response.encoding);
  8.         disposition += "filename*=" + response.encoding + "''" + encodedFilename + "; filename=\"" + encodedFilename + "\"";
  9.     }
  10.  
  11.     return disposition;
  12. }
  13.  
  14. public static void streamFileToResponse(Response response, File file, String filename, String mimeType, int bufferSize, int maxBytesPerSecond) throws Exception {
  15.     FileInputStream fis = null;
  16.     BufferedInputStream bs = null;
  17.     try {
  18.         fis = new FileInputStream(file);
  19.         bs = new BufferedInputStream(fis, bufferSize);
  20.         prepareResponseStreamForFile(response, filename, file.length(), mimeType);
  21.         channelStreamToResponse(response, bs, bufferSize, maxBytesPerSecond);
  22.     } finally {
  23.         if (bs != null)
  24.             bs.close();
  25.         if (fis != null)
  26.             fis.close();
  27.     }
  28. }
  29.  
  30. public static void prepareResponseStreamForFile(Response response, String filename, long length, String mimeType) throws Exception {
  31.     response.out.reset();
  32.     response.contentType = mimeType;
  33.     response.setHeader("Content-Disposition", getContentDisposition(response, filename));
  34.     response.setHeader("Content-Length", String.valueOf(length));
  35. }
  36.  
  37. public static void channelStreamToResponse(Response response, InputStream inputStream, int bufferSize, int maxBytesPerSecond) throws Exception {
  38.     InputStream is = null;
  39.     try {
  40.         is = new ThrottledInputStream(inputStream, maxBytesPerSecond);
  41.         byte[] buf = new byte[bufferSize];
  42.         int readBytesLen = 0;
  43.         response.out.reset();
  44.         int flushCnt = 0;
  45.  
  46.         while ((readBytesLen = is.read(buf)) > 0) {
  47.             if (readBytesLen != bufferSize)
  48.                 response.writeChunk(Arrays.copyOf(buf, readBytesLen));
  49.             else
  50.                 response.writeChunk(buf);
  51.  
  52.             if (flushCnt++ > 7 || readBytesLen <= 0) {
  53.                 response.out.flush();
  54.                 flushCnt = 0;
  55.             }
  56.         }
  57.        
  58.         response.out.flush();
  59.     } catch (Exception ex) {
  60.         response.out = null;
  61.         System.gc();
  62.         throw ex;
  63.     } finally {
  64.         if (is != null)
  65.             is.close();
  66.         if (inputStream != null)
  67.             inputStream.close();
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment