[Waba-commits] CVS: waba/src/share/classes/waba/io Catalog.java,NONE,1.1 DataStream.java,NONE,1.1 Fi
Status: Abandoned
Brought to you by:
bornet
From: Manfred R. <mr...@us...> - 2004-07-10 14:18:15
|
Update of /cvsroot/waba/waba/src/share/classes/waba/io In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30799/src/share/classes/waba/io Added Files: Catalog.java DataStream.java File.java Registry.java SerialPort.java Socket.java Stream.java Log Message: Moved to new location --- NEW FILE: Catalog.java --- package waba.io; /* $Id: Catalog.java,v 1.1 2004/07/10 14:18:02 mriem Exp $ Copyright (c) 1998, 1999 Wabasoft All rights reserved. This software is furnished under a license and may be used only in accordance with the terms of that license. This software and documentation, and its copyrights are owned by Wabasoft and are protected by copyright law. THIS SOFTWARE AND REFERENCE MATERIALS ARE PROVIDED "AS IS" WITHOUT WARRANTY AS TO THEIR PERFORMANCE, MERCHANTABILITY, FITNESS FOR ANY PARTICULAR PURPOSE, OR AGAINST INFRINGEMENT. WABASOFT ASSUMES NO RESPONSIBILITY FOR THE USE OR INABILITY TO USE THIS SOFTWARE. WABASOFT SHALL NOT BE LIABLE FOR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OF THIS PRODUCT. WABASOFT SHALL HAVE NO LIABILITY OR RESPONSIBILITY FOR SOFTWARE ALTERED, MODIFIED, OR CONVERTED BY YOU OR A THIRD PARTY, DAMAGES RESULTING FROM ACCIDENT, ABUSE OR MISAPPLICATION, OR FOR PROBLEMS DUE TO THE MALFUNCTION OF YOUR EQUIPMENT OR SOFTWARE NOT SUPPLIED BY WABASOFT. */ import waba.util.Vector; import waba.sys.Vm; import waba.sys.VmShell; /** * Catalog is a collection of records commonly referred to as a database * on small devices. * <p> * Here is an example showing data being read from records in a catalog: * * <pre> * Catalog c = new Catalog("MyCatalog", Catalog.READ_ONLY); * if (!c.isOpen()) * return; * int count = c.getRecordCount(); * byte b[] = new byte[10]; * for (int i = 0; i < count; i++) * { * c.setRecord(i); * c.readBytes(b, 0, 10); * ... * } * c.close(); * </pre> * Notes added by Dave Slaughter @120 * Catalog now reads from and writes to .pdb files, so you can do a full test of your * database application. * The path of the database is the <current directory>\db. */ public class Catalog extends Stream { /** Read-only open mode. */ public static final int READ_ONLY = 1; /** Write-only open mode. */ public static final int WRITE_ONLY = 2; /** Read-write open mode. */ public static final int READ_WRITE = 3; // READ | WRITE /** Create open mode. Used to create a database if one does not exist. */ public static final int CREATE = 4; private boolean _isOpen; private String _name; private int _mode; private Vector _records; private int _recordPos; private int _cursor; private String _creator; // ds@120 private String _type; // ds@120 private boolean _modified; // ds@120 private static final String BASE_DIR = "db"; // ds@120 /** * Opens a catalog with the given name and mode. If mode is CREATE, the * catalog will be created if it does not exist. * <p> * For PalmOS: A PalmOS creator id and type can be specified by appending * a 4 character creator id and 4 character type to the name seperated * by periods. For example: * <pre> * Catalog c = new Catalog("MyCatalog.CRTR.TYPE", Catalog.CREATE); * </pre> * Will create a PalmOS database with the name "MyCatalog", creator id * of "CRTR" and type of "TYPE". * <p> * If no creator id and type is specified, the creator id will default * to the creator id of current waba program and the type will default * to "DATA". * <p> * You must close the catalog to write the data to pdb file on disk! * <br><b>Note</b>: since a bug in the original Waba vm, the <i>TYPE</i> * <b>must</b> be identical to <i>MyCatalog</i> and they must have 4 letters. * If you don't follow this rules and you write a conduit to talk with the waba * created database, you will never find it with the conduit! (guich@120) * <p> * Under PalmOS, the name of the catalog must be 31 characters or less, * not including the creator id and type. Windows CE supports a 32 * character catalog name but to maintain compatibility with PalmOS, * you should use 31 characters maximum for the name of the catalog. * @param name catalog name * @param mode one of READ_ONLY, WRITE_ONLY, READ_WRITE or CREATE */ public Catalog(String name, int mode) { // parse name int i, j; i = name.indexOf('.'); if (i == -1) { _name = name; _creator = "CRTR"; _type = "DATA"; } else { _name = name.substring(0, i); j = i + 1; if ((i = name.indexOf('.', j)) == -1) { _creator = name.substring(j); _type = "DATA"; } else { _creator = name.substring(j, i); _type = name.substring(++i); } } _mode = mode; if (mode == CREATE) { _records = new Vector(); if (!toPDB(File.CREATE)) { // make sure empty file is written so it gets included in the list _isOpen = false; } else { _isOpen = true; }; } else { if (!fromPDB()) { _isOpen = false; } else { _isOpen = true; } } } private int _readWriteBytes(byte buf[], int start, int count, boolean isRead) { if (_recordPos == -1 || (start < 0 || count < 0) || (start + count > buf.length)) return -1; if ((_mode == READ_ONLY && !isRead) || (_mode == WRITE_ONLY && isRead)) { return -1; } byte rec[] = (byte[])_records.get(_recordPos); if (_cursor + count > rec.length) return -1; if (isRead) // guich@120: System.arraycopy is faster than a for loop. { Vm.copyArray(rec,_cursor,buf,start,count); VmShell.println("Copied bytes:"); for (int i=0; i<count; ++i) VmShell.print(buf[start+i]+" "); } else Vm.copyArray(buf,start,rec,_cursor,count); if (!isRead) _modified = true; _cursor += count; return count; } /** * Adds a record to the end of the catalog. If this operation is successful, * the position of the new record is returned and the current position is * set to the new record. If it is unsuccessful the current position is * unset and -1 is returned. * @param size the size in bytes of the record to add */ public int addRecord(int size) { if (!_isOpen) return -1; _recordPos = _records.getCount(); _records.add(new byte[size]); _cursor = 0; return _recordPos; } /** * Adds a record to the <pos> position of the catalog. If this operation is successful, * the position of the new record is returned and the current position is * set to the new record. If it is unsuccessful the current position is * unset and -1 is returned. * implemented by guich (gu...@em...) in 06/30/2000. * @param size the size in bytes of the record to add */ public int addRecord(int size, int pos) { if (!_isOpen || size < 0) return -1; if (pos < 0 || pos > _records.getCount()) return -1; _records.insert(pos, new byte[size]); _recordPos = pos; _cursor = 0; return _recordPos; } /** * Closes the catalog: writtes all information back to the pdb file. * Returns true if the operation is successful and false otherwise. */ public boolean close() { if (!_isOpen) return false; if (_modified) { // guich@120 - write only if modified toPDB(File.READ_WRITE); } _isOpen = false; _recordPos = -1; return true; } /** * Deletes all the records of the catalog. Returns true if the operation is successful and false * otherwise. Note that this behavior is diferent from palm os, because in applets we cannot erase files at the server. */ public boolean delete() { if (!_isOpen) return false; _records.removeAll(); // erases the vector _isOpen = false; _recordPos = -1; File os = new File(getFileName(), File.WRITE_ONLY); if (os == null) return false; return true; } /** * Deletes the current record and sets the current record position to -1. * The record is immediately removed from the catalog and all subsequent * records are moved up one position. */ public boolean deleteRecord() { if (_recordPos == -1) return false; _records.del(_recordPos); _recordPos = -1; return true; } /** reads a pdb file */ private boolean fromPDB() { File file = new File(getFileName(), File.READ_ONLY); if (!file.isOpen()) { return false; } DataStream is = new DataStream(file); // DatabaseHdrType byte name[] = new byte[32]; // ps: the writted string is in c++ format, so this routine doesnt loads the name correctly (comes trash with it) byte type[] = new byte[4]; byte creator[] = new byte[4]; is.readBytes(name,0,name.length); short attributes = is.readShort(); short version = is.readShort(); int creationDate = is.readInt(); int modificationDate = is.readInt(); int lastBackupDate = is.readInt(); int modificationNumber = is.readInt(); int appInfoID = is.readInt(); int sortInfoID = is.readInt(); is.readBytes(type,0,type.length); is.readBytes(creator,0,creator.length); int uniqueIDSeed = is.readInt(); // verify if the creatorId is valid if (!_creator.equals(getString(creator)) || !_type.equals(getString(type))) { is.close(); return false; } // RecordListType int nextRecordListID = is.readInt(); short numRecords = is.readShort(); VmShell.println("# records: "+numRecords); // reads the header (meaningless) int recOffsets[] = new int[numRecords + 1]; byte recAttributes; byte recUniqueID[] = new byte[3]; for (int i = 0; i < numRecords; i++) { int nOffset = is.readInt(); // offset recOffsets[i] = nOffset; VmShell.println("Offset "+i+": "+nOffset); recAttributes = is.readByte(); VmShell.println(" attributes: "+recAttributes); VmShell.println("recUniqueID.length = "+recUniqueID.length); is.readBytes(recUniqueID,0,recUniqueID.length); for (int k=0; k<recUniqueID.length; ++k) VmShell.println(" "+recUniqueID[k]); } recOffsets[numRecords] = file.getLength(); // add the total size so we can compute the size of each record is.readShort(); // pad int offset = 80 + numRecords * 8; // the records were writted in sequence from here _records = new Vector(numRecords); int size = 0; for (int i = 0; i < numRecords; i++) { size = recOffsets[i + 1] - recOffsets[i]; byte[] bytes = new byte[size]; file.seek(recOffsets[i]); is.readBytes(bytes,0,size); _records.add(bytes); } is.close(); return true; } private byte[] getBytes(String s) { char[] chars = s.toCharArray(); byte[] bytes = new byte[chars.length]; for (int i=chars.length -1; i>=0; i--) { bytes[i] = (byte) chars[i]; } return bytes; } /** returns the file name of this catalog */ // guich@120 private String getFileName() { return BASE_DIR+"/"+_name+".PDB";//+_creator+"."+_type; } /** * Returns the number of records in the catalog or -1 if the catalog is not open. */ public int getRecordCount() { if (!_isOpen) return -1; return _records.getCount(); } /** * Returns the size of the current record in bytes or -1 if there is no * current record. */ public int getRecordSize() { if (_recordPos == -1) return -1; byte rec[] = (byte[]) _records.get(_recordPos); return rec.length; } private String getString(byte[] bytes) { char[] chars = new char[bytes.length]; for (int i=bytes.length -1; i>=0; i--) { chars[i] = (char) bytes[i]; } return new String(chars); } /** Inspects a record. use this method with careful, none of the params are checked for validity. * the cursor is not advanced, neither the current record position. this method must be used * only for a fast way of viewing the contents of a record, * like searching for a specific header or filling a grid of data. * <i>buf.length</i> bytes (at maximum) are readen from the record into <i>buf</i>. * Returns the number of bytes read (can be different of buf.length if buf.length is greater * than the record size) or -1 if an error prevented the read operation from occurring. added by guich*/ public int inspectRecord(byte buf[], int recPosition) { byte rec[] = (byte[]) _records.get(recPosition); int count; if (rec.length > buf.length) { count = buf.length; } else { count = rec.length; } waba.sys.Vm.copyArray(rec, 0, buf, 0, count); return count; } /** * Returns true if the catalog is open and false otherwise. This can * be used to check if opening or creating a catalog was successful. */ public boolean isOpen() { return _isOpen; } /** * Returns the complete list of existing catalogs. If no catalogs exist, this * method returns null. */ public static String[] listCatalogs() { File dir = new File(BASE_DIR, File.READ_ONLY); if (!dir.isDir()) { return null; } String[] files = dir.listDir(); int j = 0; for (int i = 0; i < files.length; i++) { int len = files[i].length(); if (len > 4 && waba.sys.Convert.toLowerCase(files[i].substring(len-4)).equals(".pdb")) { files[j] = files[i].substring(0,len-4); j++; } } if (j == 0) { return null; } if (j != files.length) { String[] valid = new String[j]; Vm.copyArray(files, 0, valid, 0, j); return valid; } return files; } /** * Reads bytes from the current record into a byte array. Returns the * number of bytes actually read or -1 if an error prevented the * read operation from occurring. After the read is complete, the location of * the cursor in the current record (where read and write operations start from) * is advanced the number of bytes read. * @param buf the byte array to read data into * @param start the start position in the array * @param count the number of bytes to read */ public int readBytes(byte buf[], int start, int count) { return _readWriteBytes(buf, start, count, true); } /** * Resizes a record. This method changes the size (in bytes) of the current record. * The contents of the existing record are preserved if the new size is larger * than the existing size. If the new size is less than the existing size, the * contents of the record are also preserved but truncated to the new size. * Returns true if the operation is successful and false otherwise. * @param size the new size of the record */ public boolean resizeRecord(int size) { if (_recordPos == -1) return false; byte oldRec[] = (byte[]) _records.get(_recordPos); byte newRec[] = new byte[size]; int copyLen; if (oldRec.length < newRec.length) copyLen = oldRec.length; else copyLen = newRec.length; waba.sys.Vm.copyArray(oldRec, 0, newRec, 0, copyLen); _records.set(_recordPos, newRec); //_cursor = 0; guich@120 return true; } /** * Sets the current record position and locks the given record. The value * -1 can be passed to unset and unlock the current record. If the operation * is succesful, true is returned and the read/write cursor is set to * the beginning of the record. Otherwise, false is returned. */ public boolean setRecordPos(int pos) { if (pos < 0 || pos >= _records.getCount()) { _recordPos = -1; return false; } _recordPos = pos; _cursor = 0; return true; } /** * Advances the cursor in the current record a number of bytes. The cursor * defines where read and write operations start from in the record. Returns * the number of bytes actually skipped or -1 if an error occurs. * @param count the number of bytes to skip */ public int skipBytes(int count) { if (_recordPos == -1) return -1; byte rec[] = (byte[]) _records.get(_recordPos); if (_cursor + count > rec.length) return -1; _cursor += count; return count; } /** converts the records to a pdb file. */ private boolean toPDB(int fileType) { File dir = new File(BASE_DIR, File.DONT_OPEN); if (!dir.exists()) { dir.createDir(); } File file = new File(getFileName(), fileType); if (!file.isOpen()) { return false; } byte name[] = new byte[32]; short attributes = (short) 0x8000; short version = 1; int creationDate = 0xb08823ad; int modificationDate = 0xb08823ad; int lastBackupDate = 0xb08823ad; int modificationNumber = 1; int appInfoID = 0; int sortInfoID = 0; byte type[] = getBytes(_type); byte creator[] = getBytes(_creator); int uniqueIDSeed = 0; // copies the db name to inside the array <name> byte[] bn = getBytes(_name); for (int i = 0; i < name.length; i++) name[i] = (i < bn.length) ? bn[i] : (byte)0; short numRecords = (short) _records.getCount(); DataStream os = new DataStream(file); int offset = 80 + numRecords * 8; // DatabaseHdrType os.writeBytes(name, 0, name.length); os.writeShort(attributes); os.writeShort(version); os.writeInt(creationDate); os.writeInt(modificationDate); os.writeInt(lastBackupDate); os.writeInt(modificationNumber); os.writeInt(appInfoID); os.writeInt(sortInfoID); os.writeBytes(type, 0, type.length); os.writeBytes(creator, 0, creator.length); os.writeInt(uniqueIDSeed); // RecordListType int nextRecordListID = 0; os.writeInt(nextRecordListID); os.writeShort(numRecords); for (int i = 0; i < numRecords; i++) { os.writeInt(offset); // LocalChunkID os.writeByte(0); // attributes os.writeByte((byte) (i >> 16)); // uniqueID os.writeByte((byte) (i >> 8)); // uniqueID os.writeByte((byte) (i >> 0)); // uniqueID offset += ((byte[]) _records.get(i)).length; } os.writeShort(0); // pad for (int i = 0; i < numRecords; i++) { byte[] bytes = (byte[]) _records.get(i); os.writeBytes(bytes, 0, bytes.length); } os.close(); return true; } /** * Writes to the current record. Returns the number of bytes written or -1 * if an error prevented the write operation from occurring. * After the write is complete, the location of the cursor in the current record * (where read and write operations start from) is advanced the number of bytes * written. * @param buf the byte array to write data from * @param start the start position in the byte array * @param count the number of bytes to write */ public int writeBytes(byte buf[], int start, int count) { return _readWriteBytes(buf, start, count, false); } } --- NEW FILE: DataStream.java --- /* $Id: DataStream.java,v 1.1 2004/07/10 14:18:02 mriem Exp $ */ /***************************************************************************** * Waba Extras * * Version History * Date Version Programmer * ---------- ------- ------- ------------------------------------------ * 17/10/1999 New 1.0.0 Rob Nielsen * Class created * 22/07/2000 1.0.1 Guich * Added support for Strings. * ****************************************************************************/ package waba.io; import waba.sys.*; /** * DataStream is a wrapper you can place around any Stream such as a * SerialPort, Catalog, or BufferStream which lets you read and write * standard Waba data types like ints, floats, and Strings in a simple * manner. Here's an example * <pre> * SerialPort port=new SerialPort(9600,0); * DataStream ds=new DataStream(port); * ds.writeString("Hello"); * int status=ds.readUnsignedByte(); * if (status==1) * { * ds.writeString("Pi"); * ds.writeFloat(3.14); * } * port.close(); * </pre> * <br>ps: this class was created in the Waba Extras package, but i added a few methods * for storing and retrieving strings. (gu...@em...) */ public class DataStream extends Stream { /** the underlying stream */ protected Stream stream; /** a four byte array for reading and writing numbers */ protected byte[] b=new byte[4]; /** * Constructs a new DataStream which sits upon the given stream using * big endian notation for multibyte values. * @param stream the base stream */ public DataStream(Stream stream) { this.stream=stream; } /** closes the stream */ public boolean close() { if (stream != null) return stream.close(); return true; } /** * Reads an integer from the stream as four bytes. The returned value * will range from -2147483648 to 2147483647. * @return the integer */ public int readInt() { stream.readBytes(b,0,4); return (((b[0]&0xFF) << 24) | ((b[1]&0xFF) << 16) | ((b[2]&0xFF) << 8) | (b[3]&0xFF)); } /** * Writes an integer to the stream as four bytes. * @param i the integer to write * @return the number of bytes written */ public int writeInt(int i) { for(int j=0;j<4;j++) { b[3-j]=(byte)(i&0xFF); i>>=8; } stream.writeBytes(b,0,4); return 4; } /** * Reads a short from the stream as two bytes. The returned value will * range from -32768 to 32767. * @return the short */ public short readShort() { return (short)readUnsignedShort(); } /** * Writes an short to the stream as two bytes. As there is no * short type in Waba but we often only want to only use two bytes in * storage. * an int is used but the upper two bytes are ignored. * @param i the short to write * @return the number of bytes written */ public int writeShort(int i) { for(int j=0;j<2;j++) { b[1-j]=(byte)(i&0xFF); i>>=8; } stream.writeBytes(b,0,2); return 2; } /** * Reads an unsigned short from the stream as two bytes. The returned * value will range from 0 to 65535. * @return the short */ public int readUnsignedShort() { stream.readBytes(b,0,2); return (((b[0]&0xFF) << 8) | (b[1]&0xFF)); } /** * Reads a float value from the stream as four bytes in IEEE 754 format. * @return the float value */ public float readFloat() { return Convert.toFloatBitwise(readInt()); } /** * Writes a float value to the stream as four bytes in IEEE 754 format * @param f the float to write * @return the number of bytes written */ public int writeFloat(float f) { writeInt(Convert.toIntBitwise(f)); return 4; } /** * Reads a boolean from the stream as a byte. True is returned if * the byte is not zero, false if it is. * @returns the boolean value read. */ public boolean readBoolean() { stream.readBytes(b,0,1); return b[0]!=0; } /** * Writes a boolean to the stream as a byte. True values are written as * 1 and false values as 0. * @param b the boolean to write * @return the number of bytes written */ public int writeBoolean(boolean bool) { b[0]=(bool?(byte)1:(byte)0); stream.writeBytes(b,0,1); return 1; } /** * Reads a single byte from the stream. The returned value will range * from -128 to 127. * @returns the read byte */ public byte readByte() { stream.readBytes(b,0,1); return b[0]; } /** * Writes a single byte to the stream. * @param b the byte to write * @return the number of bytes written */ public int writeByte(byte by) { b[0]=by; stream.writeBytes(b,0,1); return 1; } /** * Writes a single byte to the stream. * @param b the byte to write (only least significant byte is written) * @return the number of bytes written */ public int writeByte(int by) { b[0]=(byte)(by&0xFF); stream.writeBytes(b,0,1); return 1; } /** * Reads a single unsigned byte from the stream. The returned value will * range from 0 to 255. * @returns the read byte. */ public int readUnsignedByte() { stream.readBytes(b,0,1); return b[0]&0xFF; } /** * Skips reading the next n bytes in the stream * @param n the number of bytes to skip */ public void skip(int n) { byte[] b=new byte[n]; stream.readBytes(b,0,n); } /** * Reads bytes from the stream. Returns the * number of bytes actually read or -1 if an error prevented the * read operation from occurring. * @param buf the byte array to read data into * @param start the start position in the array * @param count the number of bytes to read */ public int readBytes(byte buf[], int start, int count) { return stream.readBytes(buf,start,count); } /** * Writes bytes to the the stream. Returns the * number of bytes actually written or -1 if an error prevented the * write operation from occurring. * @param buf the byte array to write data from * @param start the start position in the byte array * @param count the number of bytes to write */ public int writeBytes(byte buf[], int start, int count) { return stream.writeBytes(buf,start,count); } /////////////////////////////// added by guich //////////////////////////////////// static private char chars[] = new char[128]; // buffer static private byte bytes[] = new byte[128]; // buffer /** pads the stream writting n bytes. all bytes will be 0. added by guich */ public int pad(int n) { for (int i =0; i < n; i++) bytes[i] = 0; writeBytes(bytes,0,n); return n; } /** reads an string. @returns a zero or more length string. null is never returned. */ public String readString() { int len = readShort(); if (len == 0) return ""; if (chars.length < len) chars = new char[len+16]; // grows the buffer if necessary readBytes(bytes,0,len); for (int i=0; i < len; i++) chars[i] = (char) bytes[i]; return new String(chars,0,len); } /** reads an array of string. @returns an zero or more length array. null is never returned. */ public String[] readStringArray() { int size = (int)readShort(); String []a = new String[size]; for (int i =0; i < size; i++) a[i] = readString(); return a; } /** writes the string array into the stream */ public int writeStringArray(String []v) { int n = 0; if (v == null || v.length == 0) n += writeShort((short)0); else { n += writeShort((short)v.length); for (int i=0; i < v.length; i++) n += writeString(v[i]); } return n; } /** writes the string into the stream */ public int writeString(String s) { int n = 0; if (s != null && s.length() > 0) { char[] c = s.toCharArray(); if (bytes.length < c.length) bytes =new byte[c.length+16]; for (int i=0; i < c.length; i++) bytes[i] = (byte) c[i]; n += writeShort((short)c.length); n += writeBytes(bytes,0,c.length); } else n += writeShort(0); return n; } } --- NEW FILE: File.java --- /* $Id: File.java,v 1.1 2004/07/10 14:18:02 mriem Exp $ Copyright (c) 1998, 1999 Wabasoft All rights reserved. This software is furnished under a license and may be used only in accordance with the terms of that license. This software and documentation, and its copyrights are owned by Wabasoft and are protected by copyright law. THIS SOFTWARE AND REFERENCE MATERIALS ARE PROVIDED "AS IS" WITHOUT WARRANTY AS TO THEIR PERFORMANCE, MERCHANTABILITY, FITNESS FOR ANY PARTICULAR PURPOSE, OR AGAINST INFRINGEMENT. WABASOFT ASSUMES NO RESPONSIBILITY FOR THE USE OR INABILITY TO USE THIS SOFTWARE. WABASOFT SHALL NOT BE LIABLE FOR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OF THIS PRODUCT. WABASOFT SHALL HAVE NO LIABILITY OR RESPONSIBILITY FOR SOFTWARE ALTERED, MODIFIED, OR CONVERTED BY YOU OR A THIRD PARTY, DAMAGES RESULTING FROM ACCIDENT, ABUSE OR MISAPPLICATION, OR FOR PROBLEMS DUE TO THE MALFUNCTION OF YOUR EQUIPMENT OR SOFTWARE NOT SUPPLIED BY WABASOFT. */ package waba.io; /** * File is a file or directory. * <p> * The File class will not work under the PalmPilot since it does not * contain a filesystem. * <p> * Here is an example showing data being read from a file: * * <pre> * File file = new File("/temp/tempfile", File.READ_ONLY); * if (!file.isOpen()) * return; * byte b[] = new byte[10]; * file.readBytes(b, 0, 10); * file.close(); * file = new File("/temp/tempfile", File.DONT_OPEN); * file.delete(); * </pre> */ public class File extends Stream { /** Don't open the file mode. */ public static final int DONT_OPEN = 0; /** Read-only open mode. */ public static final int READ_ONLY = 1; /** Write-only open mode. */ public static final int WRITE_ONLY = 2; /** Read-write open mode. */ public static final int READ_WRITE = 3; // READ | WRITE /** Create open mode. Used to create a file if one does not exist. */ public static final int CREATE = 4; String path; int mode; /** * Opens a file with the given name and mode. If mode is CREATE, the * file will be created if it does not exist. The DONT_OPEN mode * allows the exists(), rename(), delete(), listDir(), createDir() * and isDir() methods to be called without requiring the file to be * open for reading or writing. * @param path the file's path * @param mode one of DONT_OPEN, READ_ONLY, WRITE_ONLY, READ_WRITE or CREATE */ public File(String path, int mode) { this.path = path; this.mode = mode; _nativeCreate(); } private native void _nativeCreate(); /** * Closes the file. Returns true if the operation is successful and false * otherwise. */ public native boolean close(); /** * Returns true if the file is open for reading or writing and * false otherwise. This can be used to check if opening or * creating a file was successful. */ public native boolean isOpen(); /** * Creates a directory. Returns true if the operation is successful and false * otherwise. */ public native boolean createDir(); /** * Deletes the file or directory. Returns true if the operation is * successful and false otherwise. */ public native boolean delete(); /** Returns true if the file exists and false otherwise. */ public native boolean exists(); /** Returns the length of the file in bytes. If the file is not open * 0 will be returned. */ public native int getLength(); /** Return the file's path. */ public String getPath() { return path; } /** Returns true if the file is a directory and false otherwise. */ public native boolean isDir(); /** * Lists the files contained in a directory. The strings returned are the * names of the files and directories contained within this directory. * This method returns null if the directory can't be read or if the * operation fails. */ public native String []listDir(); /** * Reads bytes from the file into a byte array. Returns the * number of bytes actually read or -1 if an error prevented the * read operation from occurring. After the read is complete, the location of * the file pointer (where read and write operations start from) * is advanced the number of bytes read. * @param buf the byte array to read data into * @param start the start position in the array * @param count the number of bytes to read */ public native int readBytes(byte b[], int off, int len); /** * Writes to the file. Returns the number of bytes written or -1 * if an error prevented the write operation from occurring. * After the write is complete, the file pointer (where read and * write operations start from) is advanced the number of bytes * written. * @param buf the byte array to write data from * @param start the start position in the byte array * @param count the number of bytes to write */ public native int writeBytes(byte b[], int off, int len); /** * Renames the file. If the path given is in a different directory, the * existing file will be moved to the directory in the specified path. * Returns true if the renaming was successful and false otherwise. */ public native boolean rename(String path); /** * Sets the file pointer for read and write operations to the given * position. The position passed is an absolute position, in bytes, * from the beginning of the file. To set the position to just after * the end of the file, you can call: * <pre> * file.seek(file.getLength()); * </pre> * True is returned if the operation is successful and false otherwise. */ public native boolean seek(int pos); } --- NEW FILE: Registry.java --- /* $Id: Registry.java,v 1.1 2004/07/10 14:18:02 mriem Exp $ Copyright (c) 2001 SmartData This software is furnished under a license and may be used only in accordance with the terms of that license. This software and documentation, and its copyrights are owned by SmartData and are protected by copyright law. THIS SOFTWARE AND REFERENCE MATERIALS ARE PROVIDED "AS IS" WITHOUT WARRANTY AS TO THEIR PERFORMANCE, MERCHANTABILITY, FITNESS FOR ANY PARTICULAR PURPOSE, OR AGAINST INFRINGEMENT. SMARTDATA ASSUMES NO RESPONSIBILITY FOR THE USE OR INABILITY TO USE THIS SOFTWARE. SMARTDATA SHALL NOT BE LIABLE FOR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OF THIS PRODUCT. SMARTDATA SHALL HAVE NO LIABILITY OR RESPONSIBILITY FOR SOFTWARE ALTERED, MODIFIED, OR CONVERTED BY YOU OR A THIRD PARTY, DAMAGES RESULTING FROM ACCIDENT, ABUSE OR MISAPPLICATION, OR FOR PROBLEMS DUE TO THE MALFUNCTION OF YOUR EQUIPMENT OR SOFTWARE NOT SUPPLIED BY SMARTDATA. */ package waba.io; /** Registry <P> Allows to save and read application-specific data in non-volatile storage. <P> A Registry is a stream that, once obtained, can be read from or written to. It is also possible to skip bytes, using a positive argument to skip forward and a negative argument to skip back to a previous position. <P> It is also important to close the Registry when data has been written to it, as its contents is not guaranteed to be saved in non-volatile storage until the Registry is closed. <P> Deleting the Registry eliminates all data currently contained in it. Reading from or writing to the Registry after it has been deleted has undefined results. After deleting the Registry, it should be re-obtained using <TT>getRegistry ()</TT>. <P> Registry data is associated with the Waba main class name, so as to be application-specific. Accessing the Registry simultaneously by multiple instances of the same application can yield unexpected results. <P> Here is an example of Registry usage: <PRE> (...) Registry r = Registry.getRegistry (); byte [] data = {0x1F, 0x07, 'A', 'B'}; r.writeBytes (data, 0, 4); r.close (); (...) r = Registry.getRegistry (); r.readBytes (data, 0, 4); (...) </PRE> */ public class Registry extends Stream { private static Registry registry = null; private Registry () { _nativeCreate (); } private native boolean _nativeCreate (); /** Obtain the Registry object associated with this application. */ public static Registry getRegistry () { if (registry == null) { registry = new Registry (); } return registry; } /** Return the space available in the Registry object associated with this application. <P> Depending on the specific underlying implementation of Registry objects, the total size returned by this method might not be available at the next write, as it may have been claimed by another application in the mean time. @return the space available in bytes */ public static synchronized native int spaceAvailable (); /** Read bytes from the Registry at the current position. @param buf buffer into which the data should be stored @param start position in the buffer to start storing data @param count number of bytes to read @return the number of bytes actually read, -1 indicating an error */ public native int readBytes (byte [] buf, int start, int count); /** Write bytes into the Registry at the current position. @param buf buffer from which the data should be taken @param start position in the buffer to start taking data @param count number of bytes to write @return the number of bytes actually written, -1 indicating an error */ public synchronized native int writeBytes (byte [] buf, int start, int count); /** Skip bytes. @param count the number of bytes to skip; the result of skipping past data that has been previously written is implementation-dependent @return the new position in the Registry, or -1 if an error occurred */ public native int skipBytes (int count); /** Close the Registry and save its contents into non-volatile storage. @return <TT>false</TT> if an error occurred, <TT>true</TT> otherwise */ public synchronized boolean close () { boolean retValue = _nativeClose (); registry = null; return retValue; } private native boolean _nativeClose (); /** Delete the Registry and all its contents from non-volatile storage. @return <TT>false</TT> if an error occurred, <TT>true</TT> otherwise */ public boolean delete () { boolean retValue = _nativeDelete (); registry = null; return retValue; } private native boolean _nativeDelete (); } --- NEW FILE: SerialPort.java --- /* $Id: SerialPort.java,v 1.1 2004/07/10 14:18:02 mriem Exp $ Copyright (c) 1998, 1999 Wabasoft All rights reserved. This software is furnished under a license and may be used only in accordance with the terms of that license. This software and documentation, and its copyrights are owned by Wabasoft and are protected by copyright law. THIS SOFTWARE AND REFERENCE MATERIALS ARE PROVIDED "AS IS" WITHOUT WARRANTY AS TO THEIR PERFORMANCE, MERCHANTABILITY, FITNESS FOR ANY PARTICULAR PURPOSE, OR AGAINST INFRINGEMENT. WABASOFT ASSUMES NO RESPONSIBILITY FOR THE USE OR INABILITY TO USE THIS SOFTWARE. WABASOFT SHALL NOT BE LIABLE FOR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OF THIS PRODUCT. WABASOFT SHALL HAVE NO LIABILITY OR RESPONSIBILITY FOR SOFTWARE ALTERED, MODIFIED, OR CONVERTED BY YOU OR A THIRD PARTY, DAMAGES RESULTING FROM ACCIDENT, ABUSE OR MISAPPLICATION, OR FOR PROBLEMS DUE TO THE MALFUNCTION OF YOUR EQUIPMENT OR SOFTWARE NOT SUPPLIED BY WABASOFT. */ package waba.io; /** * SerialPort accesses a device's serial port. * <p> * Serial port access is only available when running under a native WabaVM, it * is not supported when running under Java. * <p> * When a serial port is created, an attempt is made to open the port. * If the open attempt is successful, a call to isOpen() * will return true and the port will remain open until close() is called. * If close() is never called, the port will be closed when the object * is garbage collected. * <p> * * Here is an example showing data being written and read from a serial port: * * <pre> * SerialPort port = new SerialPort(0, 9600); * if (!port.isOpen()) * return; * byte buf[] = new byte[10]; * buf[0] = 3; * buf[1] = 7; * port.writeBytes(buf, 0, 2); * int count = port.readBytes(buf, 0, 10); * if (count == 10) * ... * port.close(); * </pre> */ public class SerialPort extends Stream { /** * Opens a serial port. The number passed is the number of the * serial port for devices with multiple serial ports. Port number * 0 defines the "default port" of a given type. For Windows CE * and PalmPilot devices, you should pass 0 as the port number to * access the device's single serial port. * <p> * On Windows devices, port numbers map to COM port numbers. * For example, serial port 2 maps to "COM2:". * <p> * Here is an example showing how to open the serial port of a * PalmPilot device at 9600 baud with settings of 8 bits, * no partity and one stop bit (8/N/1): * <pre> * SerialPort port = new SerialPort(0, 9600, 8, false, 1); * </pre> * Here is an example of opening serial port COM2: on a Windows device: * <pre> * SerialPort port = new SerialPort(2, 57600, 8, false, 1); * </pre> * No serial XON/XOFF flow control (commonly called software flow control) * is used and RTS/CTS flow control (commonly called hardware flow control) * is turn on by default on all platforms but Windows CE. The parity setting * is a boolean. If false, no parity is used. If true, "even" parity is used. * * @param number port number * @param baudRate baud rate * @param bits bits per char [5 to 8] * @param parity true for even parity, false for no parity * @param stopBits number of stop bits * @see #setFlowControl */ public SerialPort(int number, int baudRate, int bits, boolean parity, int stopBits) { _nativeCreate(number, baudRate, bits, parity, stopBits); } /** * Open a serial port with settings of 8 bits, no parity and 1 stop bit. * These are the most commonly used serial port settings. */ public SerialPort(int number, int baudRate) { this(number, baudRate, 8, false, 1); } private native void _nativeCreate(int number, int baudRate, int bits, boolean parity, int stopBits); /** * Closes the port. Returns true if the operation is successful * and false otherwise. */ public native boolean close(); /** * Returns true if the port is open and false otherwise. This can * be used to check if opening the serial port was successful. */ public native boolean isOpen(); /** * Turns RTS/CTS flow control (hardware flow control) on or off. * @param on pass true to set flow control on and false to set it off */ public native boolean setFlowControl(boolean on); /** * Sets the timeout value for read operations. The value specifies * the number of milliseconds to wait from the time of last activity * before timing out a read operation. Passing a value of 0 sets * no timeout causing any read operation to return immediately with * or without data. The default timeout is 100 milliseconds. This * method returns true if successful and false if the value passed * is negative or the port is not open. * @param millis timeout in milliseconds */ public native boolean setReadTimeout(int millis); /** * Reads bytes from the port into a byte array. Returns the * number of bytes actually read or -1 if an error prevented the read * operation from occurring. The read will timeout if no activity * takes place within the timeout value for the port. * @param buf the byte array to read data into * @param start the start position in the byte array * @param count the number of bytes to read * @see #setReadTimeout */ public native int readBytes(byte buf[], int start, int count); /** * Returns the number of bytes currently available to be read from the * serial port's queue. This method only works under PalmOS and not WinCE * due to limitations in the Win32 CE API. Under Win32 and Java, * this method will always return -1. */ public native int readCheck(); /** * Writes to the port. Returns the number of bytes written or -1 * if an error prevented the write operation from occurring. If data * can't be written to the port and flow control is on, the write * operation will time out and fail after approximately 2 seconds. * @param buf the byte array to write data from * @param start the start position in the byte array * @param count the number of bytes to write */ public native int writeBytes(byte buf[], int start, int count); } --- NEW FILE: Socket.java --- /* $Id: Socket.java,v 1.1 2004/07/10 14:18:02 mriem Exp $ Copyright (c) 1998, 1999 Wabasoft All rights reserved. This software is furnished under a license and may be used only in accordance with the terms of that license. This software and documentation, and its copyrights are owned by Wabasoft and are protected by copyright law. THIS SOFTWARE AND REFERENCE MATERIALS ARE PROVIDED "AS IS" WITHOUT WARRANTY AS TO THEIR PERFORMANCE, MERCHANTABILITY, FITNESS FOR ANY PARTICULAR PURPOSE, OR AGAINST INFRINGEMENT. WABASOFT ASSUMES NO RESPONSIBILITY FOR THE USE OR INABILITY TO USE THIS SOFTWARE. WABASOFT SHALL NOT BE LIABLE FOR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OF THIS PRODUCT. WABASOFT SHALL HAVE NO LIABILITY OR RESPONSIBILITY FOR SOFTWARE ALTERED, MODIFIED, OR CONVERTED BY YOU OR A THIRD PARTY, DAMAGES RESULTING FROM ACCIDENT, ABUSE OR MISAPPLICATION, OR FOR PROBLEMS DUE TO THE MALFUNCTION OF YOUR EQUIPMENT OR SOFTWARE NOT SUPPLIED BY WABASOFT. */ package waba.io; /** * Socket is a TCP/IP network socket. * <p> * Under Java and Windows CE, if no network is present, the socket * constructor may hang for an extended period of time due to the * implementation of sockets in the underlying OS. This is a known * problem. * <p> * Here is an example showing data being written and read from a socket: * * <pre> * Socket socket = new Socket("www.yahoo.com", 80); * if (!socket.isOpen()) * return; * byte buf[] = new byte[10]; * buf[0] = 3; * buf[1] = 7; * socket.writeBytes(buf, 0, 2); * int count = socket.readBytes(buf, 0, 10); * if (count == 10) * ... * socket.close(); * </pre> */ public class Socket extends Stream { /** * Opens a socket. This method establishes a socket connection by * looking up the given host and performing the 3 way TCP/IP handshake. * @param host the host name or IP address to connect to * @param port the port number to connect to */ public Socket(String host, int port) { _nativeCreate(host, port); } private native void _nativeCreate(String host, int port); /** * Closes the socket. Returns true if the operation is successful * and false otherwise. */ public native boolean close(); /** * Returns true if the socket is open and false otherwise. This can * be used to check if opening the socket was successful. */ public native boolean isOpen(); /** * Sets the timeout value for read operations. The value specifies * the number of milliseconds to wait from the time of last activity * before timing out a read operation. Passing a value of 0 sets * no timeout causing any read operation to return immediately with * or without data. The default timeout is 1500 milliseconds. This * method returns true if successful and false if the value passed * is negative or the socket is not open. Calling this method * currently has no effect under Win32 or WindowsCE. The * read timeout under those platforms will remain the system default. * @param millis timeout in milliseconds */ public native boolean setReadTimeout(int millis); /** * Reads bytes from the socket into a byte array. Returns the * number of bytes actually read or -1 if the server closed * the connection or an error prevented the read operation from * occurring. * @param buf the byte array to read data into * @param start the start position in the byte array * @param count the number of bytes to read */ public native int readBytes(byte buf[], int start, int count); /** * Writes to the socket. Returns the number of bytes written or -1 * if an error prevented the write operation from occurring. If data * can't be written to the socket for approximately 2 seconds, the * write operation will time out. * @param buf the byte array to write data from * @param start the start position in the byte array * @param count the number of bytes to write */ public native int writeBytes(byte buf[], int start, int count); } --- NEW FILE: Stream.java --- /* $Id: Stream.java,v 1.1 2004/07/10 14:18:02 mriem Exp $ Copyright (c) 1998, 1999 Wabasoft All rights reserved. This software is furnished under a license and may be used only in accordance with the terms of that license. This software and documentation, and its copyrights are owned by Wabasoft and are protected by copyright law. THIS SOFTWARE AND REFERENCE MATERIALS ARE PROVIDED "AS IS" WITHOUT WARRANTY AS TO THEIR PERFORMANCE, MERCHANTABILITY, FITNESS FOR ANY PARTICULAR PURPOSE, OR AGAINST INFRINGEMENT. WABASOFT ASSUMES NO RESPONSIBILITY FOR THE USE OR INABILITY TO USE THIS SOFTWARE. WABASOFT SHALL NOT BE LIABLE FOR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OF THIS PRODUCT. WABASOFT SHALL HAVE NO LIABILITY OR RESPONSIBILITY FOR SOFTWARE ALTERED, MODIFIED, OR CONVERTED BY YOU OR A THIRD PARTY, DAMAGES RESULTING FROM ACCIDENT, ABUSE OR MISAPPLICATION, OR FOR PROBLEMS DUE TO THE MALFUNCTION OF YOUR EQUIPMENT OR SOFTWARE NOT SUPPLIED BY WABASOFT. */ package waba.io; /** * Stream is the base class for all stream-based I/O classes. */ public abstract class Stream { /** * Reads bytes from the stream. Returns the * number of bytes actually read or -1 if an error prevented the * read operation from occurring. * @param buf the byte array to read data into * @param start the start position in the array * @param count the number of bytes to read */ public abstract int readBytes(byte buf[], int start, int count); /** * Writes bytes to the the stream. Returns the * number of bytes actually written or -1 if an error prevented the * write operation from occurring. * @param buf the byte array to write data from * @param start the start position in the byte array * @param count the number of bytes to write */ public abstract int writeBytes(byte buf[], int start, int count); /** * Closes the stream. Returns true if the operation is successful * and false otherwise. */ public abstract boolean close(); } |