1 package org.readium.r2_streamer.model.container;
3 import java.io.BufferedReader;
4 import java.io.ByteArrayInputStream;
5 import java.io.ByteArrayOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 import java.util.ArrayList;
10 import java.util.Enumeration;
11 import java.util.List;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.ExecutorService;
15 import java.util.concurrent.Executors;
16 import java.util.concurrent.Future;
17 import java.util.zip.ZipEntry;
18 import java.util.zip.ZipFile;
21 * Created by Shrikant Badwaik on 24-Jan-17.
24 public class EpubContainer implements Container {
25 private final String TAG = "EpubContainer";
26 private ZipFile zipFile;
28 public EpubContainer(String epubFilePath) throws IOException {
29 this.zipFile = new ZipFile(epubFilePath);
31 System.out.println(TAG + " Reading epub at path: " + epubFilePath);
35 public String rawData(String relativePath) throws NullPointerException {
36 System.out.println(TAG + " Reading file at path: " + relativePath);
38 ZipEntry zipEntry = zipFile.getEntry(relativePath);
39 if (zipEntry == null) {
42 InputStream is = zipFile.getInputStream(zipEntry);
43 BufferedReader br = new BufferedReader(new InputStreamReader(is));
44 StringBuilder sb = new StringBuilder();
47 while ((line = br.readLine()) != null) {
48 sb.append(line); //.append('\n');
52 } catch (IOException e) {
59 public int rawDataSize(String relativePath) {
60 ZipEntry zipEntry = zipFile.getEntry(relativePath);
61 return ((int) zipEntry.getSize());
65 public List<String> listFiles() {
66 List<String> files = new ArrayList<>();
67 Enumeration zipEntries = zipFile.entries();
68 while (zipEntries.hasMoreElements()) {
69 String fileName = ((ZipEntry) zipEntries.nextElement()).getName();
76 public InputStream rawDataInputStream(final String relativePath) throws NullPointerException {
78 //ZipEntry zipEntry = zipFile.getEntry(relativePath);
79 /*InputStream inputStream = zipFile.getInputStream(zipEntry);
80 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
82 byte[] byteArray = new byte[4069];
83 while ((bytesRead = inputStream.read(byteArray)) != -1){
84 byteArrayOutputStream.write(byteArray, 0, bytesRead);
87 byteArrayOutputStream.flush();
88 byte[] streamArray = byteArrayOutputStream.toByteArray();
89 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(streamArray);*/
91 Callable<ByteArrayInputStream> callable = new Callable<ByteArrayInputStream>() {
93 public ByteArrayInputStream call() throws Exception {
94 ZipEntry zipEntry = zipFile.getEntry(relativePath);
95 InputStream inputStream = zipFile.getInputStream(zipEntry);
96 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
97 long BUFFER_SIZE = 16 * 1024;
98 byte[] byteArray = new byte[(int) BUFFER_SIZE];
100 while ((bytesRead = inputStream.read(byteArray)) != -1) {
101 byteArrayOutputStream.write(byteArray, 0, bytesRead);
104 byteArrayOutputStream.flush();
105 byte[] streamArray = byteArrayOutputStream.toByteArray();
106 return new ByteArrayInputStream(streamArray);
110 ExecutorService executorService = Executors.newCachedThreadPool();
111 Future<ByteArrayInputStream> future = executorService.submit(callable);
113 } catch (InterruptedException | ExecutionException e) {