You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by "Clere, Jean-Frederic" <jf...@fujitsu.siemens.es> on 2000/05/19 20:49:51 UTC

[PATCH] jakarta-tomcat/src/share/org/apache/tomcat/servlets/Defau ltServlet.java (EBCDIC)

Hi,

I have improved my previous patch for EBCDIC support in the default servlet.
In our EBCDIC the "text/*" files are in EBCDIC in Apache, so they cannot be
ASCII in Tomcat, (They are the same!).
My new patch does no convert the files on machines that uses ASCII as native
encoding.
Check it - Comment it (I am one week out, but I will try to answer), and
enclosed in the sources.

Cheers

Jean-Frederic

 <<DefaultServlet.patch>> 

---
cvs/jakarta-tomcat/src/share/org/apache/tomcat/servlets/DefaultServlet.java
Tue May  2 01:06:39 2000
+++ jakarta-tomcat/src/share/org/apache/tomcat/servlets/DefaultServlet.java
Mon May 15 12:37:20 2000
@@ -82,6 +82,7 @@
     private Context context;
     String docBase;
     int debug=0;
+    private int codeNative=0;
 
     public void init() throws ServletException {
 	contextF = getServletContext();
@@ -311,7 +312,10 @@
 	FileInputStream in=null;
 	try {
 	    in = new FileInputStream(file);
-	    serveStream(in, request, response);
+	    if (IsNative(mimeType))
+		serveStreamNative(in, request, response);
+	    else
+		serveStream(in, request, response);
 	} catch (FileNotFoundException e) {
 	    // Figure out what we're serving
 
@@ -374,6 +378,63 @@
 	    out.write(buf, 0, read);
 	}
     }
+
+    // Check the encoding of text files.
+    private boolean IsNative(String mimeType) {
+        // ASCII just return false
+	if (codeNative==1) return false;
+	if (codeNative==0) {
+	    String encoding = System.getProperty("file.encoding");
+            if (encoding.indexOf("8859_1")!=-1) {
+		codeNative=1;
+		return false;
+		}
+	    // here the encoding is not ASCII, may it has to improved.
+	    codeNative=2;
+	    }
+        // only text files need conversion (like .html)
+	if (mimeType.startsWith("text"))
+	    return true;
+	else
+	    return false;
+	}
+
+    // Sends the text file, when are encoded in machine encoding.
+    private void serveStreamNative(InputStream in, HttpServletRequest
request,
+        HttpServletResponse response)
+    throws IOException {
+        // like the serveStream, we try the stream and the writer. 
+
+	try {
+	    ServletOutputStream out = response.getOutputStream();
+	    serveStreamAsStreamNative(in, out);
+	} catch (IllegalStateException ise) {
+	    PrintWriter out = response.getWriter();
+            // as it uses an InputStreamReader it does the conversion.
+	    serveStreamAsWriter(in, out);
+	}
+    }
+
+    private void serveStreamAsStreamNative(InputStream in, OutputStream
out)
+    throws IOException {
+	char[] buf = new char[1024];
+	int read = 0;
+
+	// Here the input and the output have to be converted.
+        // input  default encoding output ASCII (ISO8859_1).
+        OutputStreamWriter output = null;
+	try {
+            output = new OutputStreamWriter(out,"ISO8859_1");
+	} catch(UnsupportedEncodingException e) {
+	    output = new OutputStreamWriter(out); // try without conversion.

+	}
+	InputStreamReader input = new InputStreamReader(in);
+
+	while ((read = input.read(buf)) != -1) {
+	    output.write(buf, 0, read);
+	}
+	output.flush();
+    }
 
     private boolean isFileMasked(String docBase, String requestedFile) {
         for (int i = 0; i < Constants.MASKED_DIR.length; i++) {