KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > httpPresentation > CachedFilePresentation


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: CachedFilePresentation.java,v 1.3 2005/05/06 07:49:10 slobodan Exp $
23  */

24
25
26
27
28
29 package com.lutris.appserver.server.httpPresentation;
30
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.ByteArrayInputStream JavaDoc;
33 import java.io.FileNotFoundException JavaDoc;
34 import java.io.InputStream JavaDoc;
35
36 /**
37  * Presentation for file data that is cached in memory.
38  */

39 class CachedFilePresentation implements HttpPresentation {
40     private String JavaDoc mimeType;
41     private int contentLen = 0;
42     private ByteArrayInputStream JavaDoc fileData;
43
44     private static final int READ_BUFFER_SIZE = 4096;
45
46     /**
47      * Construct a new static file presentation, given an input stream.
48      */

49     protected CachedFilePresentation(ClassLoader JavaDoc classLoader,
50                                      String JavaDoc urlPath,
51                                      String JavaDoc fileMimeType)
52             throws FilePresentationException {
53         mimeType = fileMimeType;
54         InputStream JavaDoc input = classLoader.getResourceAsStream(urlPath);
55         if (input == null) {
56             throw new FilePresentationException
57                 (new FileNotFoundException JavaDoc ("File \"" + urlPath
58                                             + "\" not found on application class path"));
59         }
60         
61         ByteArrayOutputStream JavaDoc inData = new ByteArrayOutputStream JavaDoc();
62         try {
63             // Read in file.
64
try {
65                 byte [] buffer = new byte[READ_BUFFER_SIZE];
66                 while (true) {
67                     int readLen = input.read(buffer, 0, READ_BUFFER_SIZE);
68                     if (readLen < 0)
69                         break;
70                     inData.write(buffer, 0, readLen);
71                     contentLen += readLen;
72                 }
73             } finally {
74                 fileData = new ByteArrayInputStream JavaDoc(inData.toByteArray());
75                 input.close();
76             }
77         } catch (Exception JavaDoc except) {
78             throw new FilePresentationException(except);
79         }
80     }
81     
82     /**
83      * Entry point for static file presenation.
84      */

85     public void run(HttpPresentationComms comms)
86             throws FilePresentationException {
87         try {
88             comms.response.setContentLength(contentLen);
89             comms.response.setContentType(mimeType);
90
91             if (!comms.request.getMethod().equals("HEAD")) {
92                 byte [] buffer = new byte[READ_BUFFER_SIZE];
93                 HttpPresentationOutputStream output = comms.response.getOutputStream();
94                 while (true) {
95                     int readLen = fileData.read(buffer, 0, READ_BUFFER_SIZE);
96                     if (readLen < 0) {
97                         break;
98                     }
99                     output.write(buffer, 0, readLen);
100                 }
101             }
102         } catch (Exception JavaDoc except) {
103             throw new FilePresentationException(except);
104         } finally {
105             fileData.reset();
106         }
107     }
108 }
109
Popular Tags