package froala.editor.video; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import java.io.RandomAccessFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import froala.editor.utils.view.VideoView; /** * 동영상 스트리밍 유틸 * @since 2021. 6. 18. * @author 임종호 *
* ----------------------- * 개정이력 * 2021. 6. 18. 임종호 : 최초작성 **/ public class StreamingUtil { private final static Logger LOGGER = LoggerFactory.getLogger(VideoView.class); public void streaming(File file, HttpServletRequest request, HttpServletResponse response, OutputStream out) throws IOException { StreamingResponse streamingResponse = new StreamingResponse(); Range range = streamingResponse.write(file, request, response); copyStreaming(file, out, range); } private void copyStreaming(File file, OutputStream output, Range range) { RandomAccessFile randomFile = null; long partSize = range.getPartSize(); try { randomFile = new RandomAccessFile(file, "r"); randomFile.seek(range.getStart()); byte[] buf = new byte[StreamingResponse.DEFAULT_BUFFER_SIZE]; do { int block = 0; if (partSize > StreamingResponse.DEFAULT_BUFFER_SIZE) { block = StreamingResponse.DEFAULT_BUFFER_SIZE; } else { block = (int) partSize; } int len = randomFile.read(buf, 0, block); output.write(buf, 0, len); output.flush(); partSize -= block; } while (partSize > 0); } catch (IOException e) { LOGGER.error("User canceled streaming request {}", e.getCause().getMessage()); } finally { IOUtils.closeQuietly(randomFile); } } }