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