Added Android code
[wl-app.git] / Android / r2-streamer / r2-parser / src / main / java / org / readium / r2_streamer / model / container / EpubContainer.java
1 package org.readium.r2_streamer.model.container;
2
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;
19
20 /**
21  * Created by Shrikant Badwaik on 24-Jan-17.
22  */
23
24 public class EpubContainer implements Container {
25     private final String TAG = "EpubContainer";
26     private ZipFile zipFile;
27
28     public EpubContainer(String epubFilePath) throws IOException {
29         this.zipFile = new ZipFile(epubFilePath);
30
31         System.out.println(TAG + " Reading epub at path: " + epubFilePath);
32     }
33
34     @Override
35     public String rawData(String relativePath) throws NullPointerException {
36         System.out.println(TAG + " Reading file at path: " + relativePath);
37         try {
38             ZipEntry zipEntry = zipFile.getEntry(relativePath);
39             if (zipEntry == null) {
40                 return "";
41             }
42             InputStream is = zipFile.getInputStream(zipEntry);
43             BufferedReader br = new BufferedReader(new InputStreamReader(is));
44             StringBuilder sb = new StringBuilder();
45             String line;
46
47             while ((line = br.readLine()) != null) {
48                 sb.append(line);        //.append('\n');
49             }
50
51             return sb.toString();
52         } catch (IOException e) {
53             e.printStackTrace();
54         }
55         return null;
56     }
57
58     @Override
59     public int rawDataSize(String relativePath) {
60         ZipEntry zipEntry = zipFile.getEntry(relativePath);
61         return ((int) zipEntry.getSize());
62     }
63
64     @Override
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();
70             files.add(fileName);
71         }
72         return files;
73     }
74
75     @Override
76     public InputStream rawDataInputStream(final String relativePath) throws NullPointerException {
77         try {
78             //ZipEntry zipEntry = zipFile.getEntry(relativePath);
79             /*InputStream inputStream = zipFile.getInputStream(zipEntry);
80             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
81             int bytesRead;
82             byte[] byteArray = new byte[4069];
83             while ((bytesRead = inputStream.read(byteArray)) != -1){
84                 byteArrayOutputStream.write(byteArray, 0, bytesRead);
85             }
86
87             byteArrayOutputStream.flush();
88             byte[] streamArray = byteArrayOutputStream.toByteArray();
89             ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(streamArray);*/
90
91             Callable<ByteArrayInputStream> callable = new Callable<ByteArrayInputStream>() {
92                 @Override
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];
99                     int bytesRead;
100                     while ((bytesRead = inputStream.read(byteArray)) != -1) {
101                         byteArrayOutputStream.write(byteArray, 0, bytesRead);
102                     }
103
104                     byteArrayOutputStream.flush();
105                     byte[] streamArray = byteArrayOutputStream.toByteArray();
106                     return new ByteArrayInputStream(streamArray);
107                 }
108             };
109
110             ExecutorService executorService = Executors.newCachedThreadPool();
111             Future<ByteArrayInputStream> future = executorService.submit(callable);
112             return future.get();
113         } catch (InterruptedException | ExecutionException e) {
114             e.printStackTrace();
115         }
116         return null;
117     }
118 }