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