public class PDBFile extends Stream
Here is an example showing data being read from records in a PDBFile:
try { PDBFile pdbFile = new PDBFile("MyPDBFile.CRTR.TYPE", PDBFile.READ_WRITE); DataStream ds = new DataStream(pdbFile); int count = pdbFile.getRecordCount(); for (int i = 0; i < count; i++) { pdbFile.setRecordPos(i); String name = ds.readString(); double salary = ds.readDouble(); // ... } pdbFile.close(); pdbFile = null; // just make sure we won't use it again. } // since the exceptions are being treated the same way, we could just catch the IOException instead, // but for the purpose of this example, we'll treat them individually. catch (IllegalArgumentIOException e) { MessageBox.showException(e, false); } catch (FileNotFoundException e) { MessageBox.showException(e, false); } catch (IOException e) { MessageBox.showException(e, false); }
Note: a PDBFile cannot be opened twice. You must close one instance before opening the other or share the same instance.
In Windows, you can specify the path of the pdb file, either absolute ("\\My Documents\\Test") or relative ("..\\..\\Test"). Note that the Settings.dataPath, if set, is also used as the path to the PDBFile.
Modifier and Type | Field and Description |
---|---|
static int |
CREATE
Non-destructive create open mode.
|
static int |
CREATE_EMPTY
Destructive-create open mode.
|
static int |
DB_ATTR_APPINFODIRTY
Set if Application Info block is dirty.
|
static int |
DB_ATTR_BACKUP
Set if database should be backed up to PC if no app-specific synchronization conduit has been supplied.
|
static int |
DB_ATTR_COPY_PREVENTION
This database should not be copied to
|
static int |
DB_ATTR_OK_TO_INSTALL_NEWER
This tells the backup conduit that it's OK for it to install a newer version of this database with a different name if the current database is open.
|
static int |
DB_ATTR_READ_ONLY
Read Only database
|
static int |
DB_ATTR_RESET_AFTER_INSTALL
Device requires a reset after this database is installed.
|
static int |
DB_ATTR_STREAM
This database is used for file stream implementation.
|
static int |
READ_WRITE
Read-write open mode.
|
static byte |
REC_ATTR_DELETE
Record atribute: Deleted.
|
static byte |
REC_ATTR_DIRTY
Record atribute: Dirty (has been modified since last sync)
|
static byte |
REC_ATTR_SECRET
Record atribute: Private
|
static byte |
REC_RELEASE
Use this in order to explicitly release a record through the setRecordAttributes method.
|
skipBuffer
Constructor and Description |
---|
PDBFile(java.lang.String name,
int mode)
Opens a PDBFile with the given name and mode.
|
Modifier and Type | Method and Description |
---|---|
void |
addRecord(int size)
Adds a record to the end of the PDBFile.
|
void |
addRecord(int size,
int pos)
Insert a record to the given position of the PDBFile.
|
void |
close()
Closes the PDBFile.
|
void |
delete()
Delete and close the PDBFile.
|
void |
deleteRecord()
Deletes the current record and sets the current record position to -1.
|
protected void |
finalize() |
int |
getAttributes()
Retrieves this PDBFile's attributes.
|
java.lang.String |
getName()
Returns the name passed on the constructor.
|
byte |
getRecordAttributes(int recordPos)
Retrieves the attributes of the given record.
|
int |
getRecordCount()
Returns the number of records in the PDBFile.
|
int |
getRecordOffset()
Returns the internal record offset used to read and write data.
|
int |
getRecordPos()
Returns the current record position or -1 if there is no current record.
|
int |
getRecordSize()
Returns the size of the current record in bytes.
|
int |
inspectRecord(byte[] buf,
int recordPos,
int offsetInRec)
Inspects a record, even if its locked.
|
static java.lang.String[] |
listPDBs()
Returns the complete list of existing PDBFiles.
|
static java.lang.String[] |
listPDBs(int creatorIdWild,
int typeWild)
Returns the list of existing PDBFiles with the given creator id and/or type.
|
int |
readBytes(byte[] buf,
int start,
int count)
Reads bytes from the current record into a byte array.
|
void |
rename(java.lang.String newName)
Renames the currently open PDBFile to the given name.
|
void |
resizeRecord(int size)
Resizes a record.
|
int |
searchBytes(byte[] toSearch,
int length,
int offsetInRec)
Searches the underlying PDBFile for the given byte array.
|
void |
setAttributes(int i)
Sets this PDBFile's attributes.
|
void |
setRecordAttributes(int recordPos,
byte attr)
Sets the attributes of the given record.
|
void |
setRecordOffset(int ofs)
Set the cursor on the given position.
|
void |
setRecordPos(int pos)
Sets the current record position and locks the given record.
|
int |
skipBytes(int count)
Changes the internal read/write cursor of the current record in a number of bytes.
|
int |
writeBytes(byte[] buf,
int start,
int count)
Writes to the current record.
|
asInputStream, asOutputStream, asStream, asStream, wrapInputStream, wrapInputStreamToStream, write, writeBytes, writeBytes, writeBytes
public static final int READ_WRITE
PDBFile(String, int)
,
Constant Field Valuespublic static final int CREATE
PDBFile(String, int)
,
Constant Field Valuespublic static final int CREATE_EMPTY
PDBFile(String, int)
,
Constant Field Valuespublic static final int DB_ATTR_READ_ONLY
setAttributes(int)
,
Constant Field Valuespublic static final int DB_ATTR_APPINFODIRTY
setAttributes(int)
,
Constant Field Valuespublic static final int DB_ATTR_BACKUP
setAttributes(int)
,
Constant Field Valuespublic static final int DB_ATTR_OK_TO_INSTALL_NEWER
setAttributes(int)
,
Constant Field Valuespublic static final int DB_ATTR_RESET_AFTER_INSTALL
setAttributes(int)
,
Constant Field Valuespublic static final int DB_ATTR_COPY_PREVENTION
setAttributes(int)
,
Constant Field Valuespublic static final int DB_ATTR_STREAM
setAttributes(int)
,
Constant Field Valuespublic static final byte REC_RELEASE
int n = cat.getRecordCount(); for (int i =0; i < n; i++) { cat.setRecordPos(i)) cat.setRecordAttributes(i,REC_RELEASE); }Note that this code assume that the PDBFile was NOT being used previously and that there are no records being used. You must also close the PDBFile after and open it again. Ignoring these restrictions will likely reset the device.
setRecordAttributes(int, byte)
,
Constant Field Valuespublic static final byte REC_ATTR_DELETE
public static final byte REC_ATTR_DIRTY
public static final byte REC_ATTR_SECRET
public PDBFile(java.lang.String name, int mode) throws IllegalArgumentIOException, FileNotFoundException, IOException
A PalmOS creator id and type must be specified by appending a 4 character creator id and 4 character type to the name separated by periods. For example:
PDBFile c = new PDBFile("MyPDBFile.CRTR.TYPE", PDBFile.CREATE);Will create a PalmOS database with the name "MyPDBFile", creator id of "CRTR" and type of "TYPE".
The name of the PDBFile must be 31 characters or less, not including the creator id and type. Every time the database is modified the backup attribute is set.
Note that some file types are blocked from opening on Palm OS. These types are appl, psys and rsrc ones, and also the resource databases. Hacking the system to allow it to access these files may simply reboot the device.
name
- PDBFile name, allowing only letters, digits and '_'. Must contain the creator id and type.mode
- one of READ_WRITE, CREATE, CREATE_EMPTY.IllegalArgumentIOException
FileNotFoundException
IOException
READ_WRITE
,
CREATE
,
CREATE_EMPTY
public java.lang.String getName()
public final void rename(java.lang.String newName) throws IllegalArgumentIOException, IOException
Here is an example of use:
PDBFile c = new PDBFile("guich.Crtr.Type", PDBFile.READ_WRITE); c.rename("flor.CrTr.TyPe");
public final void addRecord(int size) throws IllegalArgumentIOException, IOException
size
- The size in bytes of the record to addIOException
- If the operation could not be completed.IllegalArgumentIOException
public final void addRecord(int size, int pos) throws IllegalArgumentIOException, IOException
Historic notes: this was the first change implemented by Guich in Waba in 06/30/2000.
size
- The size in bytes of the record to addpos
- The position where to add the record. You can specify getRecordCount() to add it to the end.
Cannot be lower than 0.IOException
- If the operation could not be completed.IllegalArgumentIOException
- If size or pos have invalid values.public final void resizeRecord(int size) throws IllegalArgumentIOException, IOException
size
- the new size of the recordIOException
- When the method fails.IllegalArgumentIOException
public void close() throws IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
IOException
- If the PDBFile was already closed.public final void delete() throws IOException
IOException
- When the method fails.public static java.lang.String[] listPDBs()
public static final java.lang.String[] listPDBs(int creatorIdWild, int typeWild)
creatorIdWild
- The creator id wild card, or 0 to recover all typestypeWild
- The type wild card, or 0 to recover all typesConvert.chars2int(String)
public final void deleteRecord() throws IOException
IOException
- When the method fails.public final int getRecordCount() throws IOException
IOException
- If the PDBFile is not open.public final int getRecordSize() throws IOException
IOException
- When the method fails.public final void setRecordPos(int pos) throws IllegalArgumentIOException, IOException
pos
- Record to be locked for use. Must be between 0 and the current number of records, or -1.IllegalArgumentIOException
- If the argument pos has an invalid value.IOException
- If the method failspublic final int readBytes(byte[] buf, int start, int count) throws IOException
readBytes
in class Stream
buf
- the byte array to read data intostart
- the start position in the buf
arraycount
- the number of bytes to readIOException
- If you request more bytes than available or if the PDBFile is closed.public int skipBytes(int count) throws IllegalArgumentIOException, IOException
skipBytes
in class Stream
count
- the number of bytes to skip.IOException
- if the stream does not support skip, or if some other I/O error occurs.IllegalArgumentIOException
public void setRecordOffset(int ofs) throws IllegalArgumentIOException, IOException
skipBytes(-getRecordOffset()+ofs)
. If the offset is
already this one, nothing is changed.ofs
- The new read/write cursor offset value.IllegalArgumentIOException
IOException
public final int writeBytes(byte[] buf, int start, int count) throws IOException
writeBytes
in class Stream
buf
- the byte array to write data fromstart
- the start position in the byte arraycount
- the number of bytes to writeIOException
public final int inspectRecord(byte[] buf, int recordPos, int offsetInRec) throws IllegalArgumentIOException, IOException
buf
- The buffer where the data will be read into.recordPos
- The record to be readoffsetInRec
- The offset in the record from where to start reading.IOException
- When the method fails.IllegalArgumentIOException
public final byte getRecordAttributes(int recordPos) throws IllegalArgumentIOException, IOException
recordPos
- the record position from where the record attributes will be retrieved.IOException
- If the method failsIllegalArgumentIOException
REC_ATTR_DELETE
,
REC_ATTR_DIRTY
,
REC_ATTR_SECRET
public final void setRecordAttributes(int recordPos, byte attr) throws IllegalArgumentIOException, IOException
recordPos
- the record position from where the record attributes will be retrieved.attr
- the new record attribute.IOException
- If the method failsIllegalArgumentIOException
REC_ATTR_DELETE
,
REC_ATTR_DIRTY
,
REC_ATTR_SECRET
public final int getAttributes() throws IOException
IOException
DB_ATTR_APPINFODIRTY
,
DB_ATTR_BACKUP
,
DB_ATTR_COPY_PREVENTION
,
DB_ATTR_OK_TO_INSTALL_NEWER
,
DB_ATTR_READ_ONLY
,
DB_ATTR_RESET_AFTER_INSTALL
,
DB_ATTR_STREAM
public final void setAttributes(int i) throws IOException
i
- The new attributes.IOException
DB_ATTR_APPINFODIRTY
,
DB_ATTR_BACKUP
,
DB_ATTR_COPY_PREVENTION
,
DB_ATTR_OK_TO_INSTALL_NEWER
,
DB_ATTR_READ_ONLY
,
DB_ATTR_RESET_AFTER_INSTALL
,
DB_ATTR_STREAM
public final int getRecordPos() throws IOException
IOException
- If the PDBFile is closedpublic int getRecordOffset() throws IOException
IOException
public final int searchBytes(byte[] toSearch, int length, int offsetInRec) throws IllegalArgumentIOException, IOException
toSearch
- The byte array used to compare with the contents of each record.length
- How many bytes will be searched inside toSearch
(may be smaller than oSearch.length
)offsetInRec
- How many bytes to skip from the record start.IOException
- If the method failsIllegalArgumentIOException
protected void finalize()
finalize
in class java.lang.Object