ソースを参照

Merge remote-tracking branch 'origin/master'

James 3 年 前
コミット
54cb295142

+ 1 - 0
src/main/java/com/jfinal/plugin/redis/RedisPlugin.java

@@ -166,6 +166,7 @@ public class RedisPlugin implements IPlugin {
 	
 	public void setSerializer(ISerializer serializer) {
 		this.serializer = serializer;
+		Serializer.serializer = serializer;
 	}
 	
 	public void setKeyNamingPolicy(IKeyNamingPolicy keyNamingPolicy) {

+ 58 - 0
src/main/java/com/jfinal/plugin/redis/Serializer.java

@@ -0,0 +1,58 @@
+/**
+ * Copyright (c) 2011-2023, James Zhan 詹波 (jfinal@126.com).
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.jfinal.plugin.redis;
+
+import com.jfinal.plugin.redis.serializer.FstSerializer;
+import com.jfinal.plugin.redis.serializer.ISerializer;
+
+/**
+ * Serializer 用于 Redis.call(...)、Redis.use().call(...) 对数据进行序列化与反序列化
+ */
+public class Serializer {
+    
+    /*
+     * 与 RedisPlugin.setSerializer(...) 同步持有序列化策略类
+     */
+    static ISerializer serializer = FstSerializer.me;
+    
+    /**
+     * 序列化
+     */
+    public static byte[] to(Object value) {
+        try {
+            return serializer.valueToBytes(value);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+    
+    /**
+     * 反序列化
+     */
+    @SuppressWarnings({ "unchecked" })
+    public static <T> T from(byte[] bytes) {
+        try {
+            return (T) serializer.valueFromBytes(bytes);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+}
+
+
+
+

+ 20 - 19
src/main/java/com/jfinal/render/FileRender.java

@@ -27,7 +27,6 @@ import java.net.URLEncoder;
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import com.jfinal.kit.LogKit;
 import com.jfinal.kit.StrKit;
 
 /**
@@ -184,7 +183,7 @@ public class FileRender extends Render {
 				outputStream.write(buffer, 0, len);
 			}
 			outputStream.flush();
-			outputStream.close();
+			
 		} catch (IOException e) {	// ClientAbortException、EofException 直接或间接继承自 IOException
 			String name = e.getClass().getSimpleName();
 			if (name.equals("ClientAbortException") || name.equals("EofException")) {
@@ -194,8 +193,8 @@ public class FileRender extends Render {
 		} catch (Exception e) {
 			throw new RenderException(e);
 		} finally {
-			if (inputStream != null)
-				try {inputStream.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);}
+		    close(inputStream);
+			close(outputStream);
 		}
 	}
 	
@@ -217,8 +216,9 @@ public class FileRender extends Render {
 			long start = range[0];
 			long end = range[1];
 			inputStream = new BufferedInputStream(new FileInputStream(file));
-			if (inputStream.skip(start) != start)
-					throw new RuntimeException("File skip error");
+			if (inputStream.skip(start) != start) {
+				throw new RuntimeException("File skip error");
+			}
 			outputStream = response.getOutputStream();
 			byte[] buffer = new byte[1024];
 			long position = start;
@@ -235,21 +235,18 @@ public class FileRender extends Render {
 				}
 			}
 			outputStream.flush();
-			outputStream.close();
-		}
-		catch (IOException e) {	// ClientAbortException、EofException 直接或间接继承自 IOException
+			
+		} catch (IOException e) {	// ClientAbortException、EofException 直接或间接继承自 IOException
 			String name = e.getClass().getSimpleName();
 			if (name.equals("ClientAbortException") || name.equals("EofException")) {
 			} else {
 				throw new RenderException(e);
 			}
-		}
-		catch (Exception e) {
+		} catch (Exception e) {
 			throw new RenderException(e);
-		}
-		finally {
-			if (inputStream != null)
-				try {inputStream.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);}
+		} finally {
+		    close(inputStream);
+		    close(outputStream);
 		}
 	}
 	
@@ -263,20 +260,23 @@ public class FileRender extends Render {
 	protected void processRange(Long[] range) {
 		String rangeStr = request.getHeader("Range");
 		int index = rangeStr.indexOf(',');
-		if (index != -1)
+		if (index != -1) {
 			rangeStr = rangeStr.substring(0, index);
+		}
 		rangeStr = rangeStr.replace("bytes=", "");
 		
 		String[] arr = rangeStr.split("-", 2);
-		if (arr.length < 2)
+		if (arr.length < 2) {
 			throw new RuntimeException("Range error");
+		}
 		
 		long fileLength = file.length();
 		for (int i=0; i<range.length; i++) {
 			if (StrKit.notBlank(arr[i])) {
 				range[i] = Long.parseLong(arr[i].trim());
-				if (range[i] >= fileLength)
+				if (range[i] >= fileLength) {
 					range[i] = fileLength - 1;
+				}
 			}
 		}
 		
@@ -291,8 +291,9 @@ public class FileRender extends Render {
 		}
 		
 		// check final range
-		if (range[0] == null || range[1] == null || range[0].longValue() > range[1].longValue())
+		if (range[0] == null || range[1] == null || range[0].longValue() > range[1].longValue()) {
 			throw new RuntimeException("Range error");
+		}
 	}
 }
 

+ 15 - 0
src/main/java/com/jfinal/render/Render.java

@@ -19,6 +19,7 @@ package com.jfinal.render;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import com.jfinal.core.Const;
+import com.jfinal.log.Log;
 
 /**
  * Render.
@@ -72,4 +73,18 @@ public abstract class Render {
 	 * Render to client
 	 */
 	public abstract void render();
+	
+	/**
+	 * OutputStream、Writer 写入异常时,关闭它们,ActionHandler 中未向底层容器继续抛出 IOException,
+	 * 以防容器在意外情况下未关闭它们(虽然调试 undertow 源码得知,无论异常产生与否,都将关闭它们)
+	 */
+	protected void close(AutoCloseable autoCloseable) {
+	    if (autoCloseable != null) {
+	        try {
+                autoCloseable.close();
+            } catch (Exception e) {
+                Log.getLog(getClass()).error(e.getMessage(), e);
+            }
+	    }
+	}
 }

+ 33 - 0
src/main/java/com/jfinal/template/Template.java

@@ -206,6 +206,39 @@ public class Template {
 			throw new RuntimeException(e);
 		}
 	}
+	
+	// ---------
+	
+    private void close(boolean autoClose, AutoCloseable autoCloseable) {
+        if (autoClose && autoCloseable != null) {
+            try {
+                autoCloseable.close();
+            } catch (Exception ignored) {
+            }
+        }
+    }
+	
+	/**
+     * 渲染到 OutputStream 中去,autoCloseOutputStream 指定是否自动关闭 OutputStream
+     */
+    public void render(Map<?, ?> data, OutputStream outputStream, boolean autoCloseOutputStream) {
+        try {
+            render(data, outputStream);
+        } finally {
+            close(autoCloseOutputStream, outputStream);
+        }
+    }
+    
+    /**
+     * 渲染到 Writer 中去,autoCloseWriter 指定是否自动关闭 Writer
+     */
+    public void render(Map<?, ?> data, Writer writer, boolean autoCloseWriter) {
+        try {
+            render(data, writer);
+        } finally {
+            close(autoCloseWriter, writer);
+        }
+    }
 }