JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / javajs / util / ZipData.java
1 /* $RCSfile$\r
2  * $Author$\r
3  * $Date$\r
4  * $Revision$\r
5  *\r
6  * Copyright (C) 2006  The Jmol Development Team\r
7  *\r
8  * Contact: jmol-developers@lists.sf.net\r
9  *\r
10  *  This library is free software; you can redistribute it and/or\r
11  *  modify it under the terms of the GNU Lesser General Public\r
12  *  License as published by the Free Software Foundation; either\r
13  *  version 2.1 of the License, or (at your option) any later version.\r
14  *\r
15  *  This library is distributed in the hope that it will be useful,\r
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
18  *  Lesser General Public License for more details.\r
19  *\r
20  *  You should have received a copy of the GNU Lesser General Public\r
21  *  License along with this library; if not, write to the Free Software\r
22  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA\r
23  *  02110-1301, USA.\r
24  */\r
25 \r
26 package javajs.util;\r
27 \r
28 import java.io.BufferedInputStream;\r
29 \r
30 import javajs.api.GenericZipTools;\r
31 \r
32 \r
33 \r
34 \r
35 public class ZipData {\r
36   boolean isEnabled = true;\r
37   byte[] buf;\r
38   int pt;\r
39   int nBytes;\r
40   \r
41   public ZipData(int nBytes) {\r
42     this.nBytes = nBytes;\r
43   }\r
44   \r
45   public int addBytes(byte[] byteBuf, int nSectorBytes, int nBytesRemaining) {\r
46     if (pt == 0) {\r
47       if (!Rdr.isGzipB(byteBuf)) {\r
48         isEnabled = false;\r
49         return -1;\r
50       }\r
51       buf = new byte[nBytesRemaining];\r
52     }\r
53     int nToAdd = Math.min(nSectorBytes, nBytesRemaining);\r
54     System.arraycopy(byteBuf, 0, buf, pt, nToAdd);\r
55     pt += nToAdd;\r
56     return nBytesRemaining - nToAdd;\r
57   }    \r
58 \r
59   public void addTo(GenericZipTools jzt, SB data) {\r
60     data.append(getGzippedBytesAsString(jzt, buf));\r
61   }\r
62 \r
63   static String getGzippedBytesAsString(GenericZipTools jzt, byte[] bytes) {\r
64     try {\r
65       BufferedInputStream bis = jzt.getUnGzippedInputStream(bytes);\r
66       String s = ZipTools.getStreamAsString(bis);\r
67       bis.close();\r
68       return s;\r
69     } catch (Exception e) {\r
70       return "";\r
71     }\r
72   }\r
73 \r
74  \r
75 }\r
76 \r