KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > write > biff > File


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.write.biff;
21
22 import java.io.OutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import common.Logger;
26 import jxl.WorkbookSettings;
27 import jxl.biff.ByteData;
28
29 /**
30  * A file of excel data to be written out. All the excel data is held
31  * in memory, and when the close method is called a CompoundFile object
32  * is used to write the Biff oriented excel data in the CompoundFile
33  * format
34  */

35 public final class File
36 {
37   /**
38    * The logger
39    */

40   private static Logger logger = Logger.getLogger(File.class);
41
42   /**
43    * The data from the excel 97 file
44    */

45   private byte[] data;
46   /**
47    * The current position within the file
48    */

49   private int pos;
50   /**
51    * The output stream
52    */

53   private OutputStream JavaDoc outputStream;
54   /**
55    * The initial file size
56    */

57   private int initialFileSize;
58   /**
59    * The amount to increase the growable array by
60    */

61   private int arrayGrowSize;
62   /**
63    * The workbook settings
64    */

65   private WorkbookSettings workbookSettings;
66   /**
67    * The read compound file. This will only be non-null if there are macros
68    * or other property sets of that ilk which that we should be copying
69    */

70   jxl.read.biff.CompoundFile readCompoundFile;
71
72   /**
73    * Constructor
74    *
75    * @param os the output stream
76    * @param ws the configuration settings for this workbook
77    * @param rcf the rea compound file
78    */

79   File(OutputStream JavaDoc os, WorkbookSettings ws, jxl.read.biff.CompoundFile rcf)
80   {
81     initialFileSize = ws.getInitialFileSize();
82     arrayGrowSize = ws.getArrayGrowSize();
83     data = new byte[initialFileSize];
84     pos = 0;
85     outputStream = os;
86     workbookSettings = ws;
87     readCompoundFile = rcf;
88   }
89
90   /**
91    * Closes the file. In fact, this writes out all the excel data
92    * to disk using a CompoundFile object, and then frees up all the memory
93    * allocated to the workbook
94    *
95    * @exception IOException
96    * @param cs TRUE if this should close the stream, FALSE if the application
97    * closes it
98    */

99   void close(boolean cs) throws IOException JavaDoc, JxlWriteException
100   {
101     CompoundFile cf = new CompoundFile(data, pos, outputStream,
102                                        readCompoundFile);
103     cf.write();
104     
105     outputStream.flush();
106
107     if (cs)
108     {
109       outputStream.close();
110     }
111
112     // Cleanup a bit
113
data = null;
114
115     if (!workbookSettings.getGCDisabled())
116     {
117       System.gc();
118     }
119   }
120
121   /**
122    * Adds the biff record data to the memory allocated for this File
123    *
124    * @exception IOException
125    * @param record the record to add to the excel data
126    */

127   public void write(ByteData record) throws IOException JavaDoc
128   {
129     try
130     {
131     byte[] bytes = record.getBytes();
132     
133     while (pos + bytes.length > data.length)
134     {
135       // Grow the array
136
byte[] newdata = new byte[data.length + arrayGrowSize];
137       System.arraycopy(data, 0, newdata, 0, pos);
138       data = newdata;
139     }
140
141     System.arraycopy(bytes, 0, data, pos, bytes.length);
142     pos += bytes.length;
143     }
144     catch (Throwable JavaDoc t)
145     {
146       logger.debug("record is " + record.getClass().getName());
147       t.printStackTrace();
148       throw new RuntimeException JavaDoc(t);
149     }
150
151   }
152
153   /**
154    * Gets the current position within the file
155    *
156    * @return the current position
157    */

158   int getPos()
159   {
160     return pos;
161   }
162
163   /**
164    * Used to manually alter the contents of the written out data. This
165    * is used when cross-referencing cell records
166    *
167    * @param pos the position to alter
168    * @param newdata the data to modify
169    */

170   void setData(byte[] newdata, int pos)
171   {
172     System.arraycopy(newdata, 0, data, pos, newdata.length);
173   }
174
175   /**
176    * Sets a new output file. This allows the smae workbook to be
177    * written to various different output files without having to
178    * read in any templates again
179    *
180    * @param os the output stream
181    */

182   public void setOutputFile(OutputStream JavaDoc os)
183   {
184     if (data != null)
185     {
186       logger.warn("Rewriting a workbook with non-empty data");
187     }
188
189     outputStream = os;
190     data = new byte[initialFileSize];
191     pos = 0;
192   }
193 }
194
Popular Tags