From 8438fe763a516368694adff65ae972511b8876cd Mon Sep 17 00:00:00 2001
From: srosse <stephane.rosse@frentix.com>
Date: Tue, 7 Apr 2020 13:43:10 +0200
Subject: [PATCH] OO-4627: respect preferred buffer size to deliver ranged
 content (video)

---
 .../generic/iframe/IFrameDeliveryMapper.java  |  4 +++-
 .../org/olat/core/gui/media/ServletUtil.java  | 24 +++++++------------
 2 files changed, 12 insertions(+), 16 deletions(-)

diff --git a/src/main/java/org/olat/core/gui/control/generic/iframe/IFrameDeliveryMapper.java b/src/main/java/org/olat/core/gui/control/generic/iframe/IFrameDeliveryMapper.java
index 3a6f007bf8a..371ee078858 100644
--- a/src/main/java/org/olat/core/gui/control/generic/iframe/IFrameDeliveryMapper.java
+++ b/src/main/java/org/olat/core/gui/control/generic/iframe/IFrameDeliveryMapper.java
@@ -229,7 +229,9 @@ public class IFrameDeliveryMapper implements Mapper {
 				String containerPath = path.substring(0, lastSlash);
 				String filename = path.substring(lastSlash + 1);
 				VFSItem parentItem = rootDir.resolve(containerPath);
-				items = ((VFSContainer)parentItem).getItems(new ByNameCaseInsensitive(filename));
+				if(parentItem != null) {
+					items = ((VFSContainer)parentItem).getItems(new ByNameCaseInsensitive(filename));
+				}
 			} else {
 				items = ((VFSContainer)rootDir).getItems(new ByNameCaseInsensitive(path));
 			}
diff --git a/src/main/java/org/olat/core/gui/media/ServletUtil.java b/src/main/java/org/olat/core/gui/media/ServletUtil.java
index 23871d8fd42..0b5dc734cb4 100644
--- a/src/main/java/org/olat/core/gui/media/ServletUtil.java
+++ b/src/main/java/org/olat/core/gui/media/ServletUtil.java
@@ -210,12 +210,9 @@ public class ServletUtil {
 						httpResp.setHeader("content-length", "" + length);
 					}
 					httpResp.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
-					try {
-						httpResp.setBufferSize(2048);
-					} catch (IllegalStateException e) {
-						// Silent catch
-					}
-					copy(out, in, range);
+
+					int bufferSize = httpResp.getBufferSize();
+					copy(out, in, range, bufferSize);
 				} else {
 					if (size != null) {
 						httpResp.setContentLengthLong(size.longValue());
@@ -247,12 +244,12 @@ public class ServletUtil {
 		}
 	}
 	
-	//fxdiff FXOLAT-118: accept range to deliver videos for iPad
-	protected static void copy(OutputStream ostream, InputStream resourceInputStream, Range range) throws IOException {
+	protected static void copy(OutputStream ostream, InputStream resourceInputStream, Range range, int bufferSize) throws IOException {
 		IOException exception = null;
 
-		InputStream istream = new BufferedInputStream(resourceInputStream, 2048);
-		exception = copyRange(istream, ostream, range.start, range.end);
+		InputStream istream = (resourceInputStream instanceof BufferedInputStream)
+				? resourceInputStream : new BufferedInputStream(resourceInputStream, bufferSize);
+		exception = copyRange(istream, ostream, range.start, range.end, bufferSize);
 
 		// Clean up the input stream
 		istream.close();
@@ -261,8 +258,7 @@ public class ServletUtil {
 		if (exception != null) throw exception;
 	}
 	
-	//fxdiff FXOLAT-118: accept range to deliver videos for iPad
-	protected static IOException copyRange(InputStream istream, OutputStream ostream, long start, long end) {
+	protected static IOException copyRange(InputStream istream, OutputStream ostream, long start, long end, int bufferSize) {
 		try {
 			istream.skip(start);
 		} catch (IOException e) {
@@ -272,7 +268,7 @@ public class ServletUtil {
 		IOException exception = null;
 		long bytesToRead = end - start + 1;
 
-		byte buffer[] = new byte[2048];
+		byte[] buffer = new byte[bufferSize];
 		int len = buffer.length;
 		while ((bytesToRead > 0) && (len >= buffer.length)) {
 			try {
@@ -294,7 +290,6 @@ public class ServletUtil {
 		return exception;
 	}
 
-	//fxdiff FXOLAT-118: accept range to deliver videos for iPad
 	protected static List<Range> parseRange(HttpServletRequest request, HttpServletResponse response, long lastModified, long fileLength)
 			throws IOException {
 		
@@ -580,7 +575,6 @@ public class ServletUtil {
 			setNoCacheHeaders(response);
 		} else {
 			long now = System.currentTimeMillis();
-			//res being the HttpServletResponse of the request
 			response.addHeader("Cache-Control", "max-age=" + duration);
 			response.setDateHeader("Expires", now + duration);
 		}
-- 
GitLab