KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > read > biff > Record


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.read.biff;
21
22 import java.util.ArrayList JavaDoc;
23
24 import common.Logger;
25
26 import jxl.biff.Type;
27 import jxl.biff.IntegerHelper;
28
29 /**
30  * A container for the raw record data within a biff file
31  */

32 public final class Record
33 {
34   /**
35    * The logger
36    */

37   private static final Logger logger = Logger.getLogger(Record.class);
38
39   /**
40    * The excel biff code
41    */

42   private int code;
43   /**
44    * The data type
45    */

46   private Type type;
47   /**
48    * The length of this record
49    */

50   private int length;
51   /**
52    * A pointer to the beginning of the actual data
53    */

54   private int dataPos;
55   /**
56    * A handle to the excel 97 file
57    */

58   private File file;
59   /**
60    * The raw data within this record
61    */

62   private byte[] data;
63
64   /**
65    * Any continue records
66    */

67   private ArrayList JavaDoc continueRecords;
68
69   /**
70    * Constructor
71    *
72    * @param offset the offset in the raw file
73    * @param f the excel 97 biff file
74    * @param d the data record
75    */

76   Record(byte[] d, int offset, File f)
77   {
78     code = IntegerHelper.getInt(d[offset], d[offset + 1]);
79     length = IntegerHelper.getInt(d[offset + 2], d[offset + 3]);
80     file = f;
81     file.skip(4);
82     dataPos = f.getPos();
83     file.skip(length);
84     type = Type.getType(code);
85   }
86
87   /**
88    * Gets the biff type
89    *
90    * @return the biff type
91    */

92   public Type getType()
93   {
94     return type;
95   }
96
97   /**
98    * Gets the length of the record
99    *
100    * @return the length of the record
101    */

102   public int getLength()
103   {
104     return length;
105   }
106
107   /**
108    * Gets the data portion of the record
109    *
110    * @return the data portion of the record
111    */

112   public byte[] getData()
113   {
114     if (data == null)
115     {
116       data = file.read(dataPos, length);
117     }
118
119     // copy in any data from the continue records
120
if (continueRecords != null)
121     {
122       int size = 0;
123       byte[][] contData = new byte[continueRecords.size()][];
124       for (int i = 0; i < continueRecords.size(); i++)
125       {
126         Record r = (Record) continueRecords.get(i);
127         contData[i] = r.getData();
128         byte[] d2 = contData[i];
129         size += d2.length;
130       }
131
132       byte[] d3 = new byte[data.length + size];
133       System.arraycopy(data, 0, d3, 0, data.length);
134       int pos = data.length;
135       for (int i = 0; i < contData.length; i++)
136       {
137         byte[] d2 = contData[i];
138         System.arraycopy(d2, 0, d3, pos, d2.length);
139         pos += d2.length;
140       }
141
142       data = d3;
143     }
144
145     return data;
146   }
147
148   /**
149    * The excel 97 code
150    *
151    * @return the excel code
152    */

153   public int getCode()
154   {
155     return code;
156   }
157
158   /**
159    * In the case of dodgy records, this method may be called to forcibly
160    * set the type in order to continue processing
161    *
162    * @param t the forcibly overridden type
163    */

164   void setType(Type t)
165   {
166     type = t;
167   }
168
169   /**
170    * Adds a continue record to this data
171    *
172    * @param d the continue record
173    */

174   public void addContinueRecord(Record d)
175   {
176     if (continueRecords == null)
177     {
178       continueRecords = new ArrayList JavaDoc();
179     }
180
181     continueRecords.add(d);
182   }
183 }
184
Popular Tags