LibreOffice Module xmerge (master)  1
PdbEncoder.java
Go to the documentation of this file.
1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  * Licensed to the Apache Software Foundation (ASF) under one or more
11  * contributor license agreements. See the NOTICE file distributed
12  * with this work for additional information regarding copyright
13  * ownership. The ASF licenses this file to you under the Apache
14  * License, Version 2.0 (the "License"); you may not use this file
15  * except in compliance with the License. You may obtain a copy of
16  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 package org.openoffice.xmerge.converter.palm;
20 
21 import java.io.OutputStream;
22 import java.io.BufferedOutputStream;
23 import java.io.DataOutputStream;
24 import java.io.IOException;
25 import java.util.Date;
26 
44 public final class PdbEncoder {
45 
47  private final PdbHeader header;
48 
50  private final PalmDB db;
51 
53  private static final int START_UNIQUE_ID = 0x00BABE;
54 
55 
61  public PdbEncoder(PalmDB db) {
62 
63  header = new PdbHeader();
64  header.version = db.getVersion();
65 
66  header.attribute = db.getAttribute();
67 
68  this.db = db;
69 
70  header.pdbName = db.getPDBNameBytes();
71  header.creatorID = db.getCreatorID();
72  header.typeID = db.getTypeID();
73 
74  // set the following dates to current date
75  Date date = new Date();
76  header.creationDate = (date.getTime() / 1000) + PdbUtil.TIME_DIFF;
77  header.modificationDate = header.creationDate;
78 
79  header.numRecords = db.getRecordCount();
80  }
81 
106  public void write(OutputStream os) throws IOException {
107 
108  BufferedOutputStream bos = new BufferedOutputStream(os);
109  DataOutputStream dos = new DataOutputStream(bos);
110 
111  // write out the PDB header
112  header.write(dos);
113 
114  if (header.numRecords > 0) {
115 
116  // compute for recOffset[]
117  int recOffset[] = new int[header.numRecords];
118  byte recAttr[] = new byte[header.numRecords];
119 
120  // first recOffset will be at PdbUtil.HEADER_SIZE + all the
121  // record indices (@ 8 bytes each)
122  recOffset[0] = PdbUtil.HEADER_SIZE + (header.numRecords * 8);
123 
124  int lastIndex = header.numRecords - 1;
125  for (int i = 0; i < lastIndex; i++) {
126 
127  Record rec = db.getRecord(i);
128  int size = rec.getSize();
129  recAttr[i] = rec.getAttributes();
130 
131  recOffset[i+1] = recOffset[i] + size;
132  }
133 
134  // grab the last record's attribute.
135  Record lastRec = db.getRecord(lastIndex);
136  recAttr[lastIndex] = lastRec.getAttributes();
137 
138  int uid = START_UNIQUE_ID;
139  for (int i = 0; i < header.numRecords; i++) {
140 
141  // write out each record offset
142  dos.writeInt(recOffset[i]);
143 
144  // write out record attribute (recAttr) and
145  // unique ID (uid) in 4 bytes (int) chunk.
146  // unique ID's have to be unique, thus
147  // increment each time.
148  int attr = (recAttr[i] << 24 );
149  attr |= uid;
150  dos.writeInt(attr);
151  uid++;
152  }
153 
154  // write out the raw records
155  for (int i = 0; i < header.numRecords; i++) {
156 
157  Record rec = db.getRecord(i);
158  byte bytes[] = rec.getBytes();
159  dos.write(bytes);
160  }
161  } else {
162  // placeholder bytes if there are no records in the list.
163  dos.writeShort(0);
164  }
165 
166  dos.flush();
167  }
168 }
static final int START_UNIQUE_ID
The pattern for unique_id=0x00BABE(start).
Definition: PdbEncoder.java:53
void write(OutputStream os)
Write out a PDB into the given.
std::vector< sal_uInt8 > bytes
short getAttribute()
Returns attribute flag.
Definition: PalmDB.java:234
final PalmDB db
the PalmDB object.
Definition: PdbEncoder.java:50
byte[] getPDBNameBytes()
Return the PDB name associated with this object in.
Definition: PalmDB.java:294
exports com.sun.star. java
Provides functionality to encode a.
Definition: PdbEncoder.java:44
int i
Contains common static methods and constants for use within the package.
Definition: PdbUtil.java:24
int getCreatorID()
Returns creator ID.
Definition: PalmDB.java:216
size
Record getRecord(int index)
Return the specific.
Definition: PalmDB.java:266
byte[] getBytes()
This method returns the contents of this.
Definition: Record.java:92
unsigned char byte
int getRecordCount()
Return the number of Records contained in this PDB.
Definition: PalmDB.java:252
int getSize()
This method returns the number of bytes in this object.
Definition: Record.java:83
This class contains data for a single Palm database for use during a conversion process.
Definition: PalmDB.java:59
Contains the raw bytes for a.
Definition: Record.java:35