private void load() throws RecordStoreException { RecordStore recordStore = null; RecordEnumeration recordEnumeration = null; try { recordStore = RecordStore.openRecordStore(_recordStoreName, true); recordEnumeration = recordStore.enumerateRecords(null, null, false); while (recordEnumeration.hasNextElement()) { byte[] raw = recordEnumeration.nextRecord(); String entry = new String(raw); /* Parse name and value for a preference entry. */ int index = entry.indexOf('='); String name = entry.substring(0, index); String value = entry.substring(index + 1); put(name, value); } } finally { if (recordEnumeration != null) { recordEnumeration.destroy(); } if (recordStore != null) { recordStore.closeRecordStore(); } } }
/** Prida nebo edituje vzorec */ public void addEdit(boolean edit) { try { // prevod dat na stream bytu ByteArrayOutputStream buffer = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(buffer); dos.writeUTF(gui.get_tfPatternName().getString()); dos.writeUTF(gui.get_tfEditPatternLattitude().getString().toUpperCase()); dos.writeUTF(gui.get_tfEditPatternLongitude().getString().toUpperCase()); byte[] bytes = buffer.toByteArray(); // nejdriv zjistit zda uz dane pismeno v databazi je if (edit) { RecordEnumeration rc = recordStore.enumerateRecords(this, this, true); rc.rebuild(); int id = 0; for (int i = 0; i < gui.get_lstPatterns().getSelectedIndex() + 1; i++) { id = rc.nextRecordId(); } recordStore.setRecord(id, bytes, 0, bytes.length); } else { recordStore.addRecord(bytes, 0, bytes.length); } viewAll(); } catch (Exception ex) { gui.showError("addEditPattern", ex.toString(), ""); } }
public void showAllRecordForm() { fmAllRecords = new Form("All records"); try { RecordEnumeration recEnum = rs.enumerateRecords(null, null, false); while (recEnum.hasNextElement()) { try { // System.out.println(new String(recEnum.nextRecord())); String name = "Nenacetl jsem"; int number = 0; try { byte[] record = recEnum.nextRecord(); ByteArrayInputStream buffer = new ByteArrayInputStream(record); DataInputStream dis = new DataInputStream(buffer); name = dis.readUTF(); number = dis.readInt(); dis.close(); } catch (Exception e) { } fmAllRecords.append(name + " " + String.valueOf(number) + "\n"); } catch (Exception e) { System.out.println(e.getMessage()); } // } } catch (Exception ex) { System.out.println(ex.getMessage()); } fmAllRecords.addCommand(cmdMenu); fmAllRecords.setCommandListener(this); dsp.setCurrent(fmAllRecords); }
/** * Saves current settings to the Record Management Store. * * @throws RecordStoreException if error occured while accessing the Record Management Store. */ public void save() throws RecordStoreException { RecordStore recordStore = null; RecordEnumeration recordEnumeration = null; try { recordStore = RecordStore.openRecordStore(_recordStoreName, true); recordEnumeration = recordStore.enumerateRecords(null, null, false); /* Remove all records first. */ while (recordEnumeration.hasNextElement()) { int id = recordEnumeration.nextRecordId(); recordStore.deleteRecord(id); } /* Now save the preferences. */ IStringEnumeration keys = _hashtable.keys(); while (keys.hasMoreElements()) { String key = keys.nextElement(); String value = get(key); String entry = key + "=" + value; byte[] raw = entry.getBytes(); recordStore.addRecord(raw, 0, raw.length); } } finally { if (recordEnumeration != null) { recordEnumeration.destroy(); } if (recordStore != null) { recordStore.closeRecordStore(); } } }
/** * This will populate the imageInfo hashtable with the ImageInfo object, referenced by label name * and populate the imageTable hashtable with Image objects referenced by the RMS record Id * * @throws PersistenceMechanismException */ public MediaData[] loadMediaDataFromRMS(String recordName) throws PersistenceMechanismException, InvalidImageDataException { Vector mediaVector = new Vector(); try { String infoStoreName = info_label + recordName; RecordStore infoStore = RecordStore.openRecordStore(infoStoreName, false); RecordEnumeration isEnum = infoStore.enumerateRecords(null, null, false); while (isEnum.hasNextElement()) { // Get next record int currentId = isEnum.nextRecordId(); byte[] data = infoStore.getRecord(currentId); // Convert the data from a byte array into our ImageData // (metadata) object MediaData iiObject = getMediaFromBytes(data); // Add the info to the metadata hashtable String label = iiObject.getMediaLabel(); mediaVector.addElement(iiObject); getMediaInfoTable().put(label, iiObject); } infoStore.closeRecordStore(); } catch (RecordStoreException rse) { throw new PersistenceMechanismException(rse); } // Re-copy the contents into a smaller array MediaData[] labelArray = new MediaData[mediaVector.size()]; mediaVector.copyInto(labelArray); return labelArray; }
public void save() throws RecordStoreException { RecordStore rs = null; RecordEnumeration re = null; try { rs = RecordStore.openRecordStore(mRecordStoreName, true); re = rs.enumerateRecords(null, null, false); // First remove all records, a little clumsy. while (re.hasNextElement()) { int id = re.nextRecordId(); rs.deleteRecord(id); } // Now save the preferences records. Enumeration keys = mHashtable.keys(); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); String value = get(key); String pref = key + "|" + value; byte[] raw = pref.getBytes(); rs.addRecord(raw, 0, raw.length); } } finally { if (re != null) re.destroy(); if (rs != null) rs.closeRecordStore(); } }
/** Smaze jeden vzorecek */ public void delete() { try { RecordEnumeration rc = recordStore.enumerateRecords(this, this, true); rc.rebuild(); int id = 0; for (int i = 0; i < gui.get_lstPatterns().getSelectedIndex() + 1; i++) { id = rc.nextRecordId(); } recordStore.deleteRecord(id); } catch (Exception e) { gui.showError("deletePattern", e.toString(), ""); } }
/** Zobrazi vsechny vzorce */ public void viewAll() { try { gui.get_lstPatterns().deleteAll(); RecordEnumeration rc = recordStore.enumerateRecords(this, this, true); rc.rebuild(); for (int i = 0; i < rc.numRecords(); i++) { DataInputStream dis = new DataInputStream(new ByteArrayInputStream(recordStore.getRecord(rc.nextRecordId()))); gui.get_lstPatterns().append(dis.readUTF(), null); } } catch (Exception ex) { gui.showError("viewPatterns", ex.toString(), ""); } }
/** * Load image with specified name * * @param recordStore is the name of the record store. * @param resourceName is the name of the image to load * @return the loaded Image or null. */ public static Image loadPngFromRMS(String recordStore, String resourceName) { RecordStore imagesRS = null; Image img = null; try { imagesRS = RecordStore.openRecordStore(recordStore, true); RecordEnumeration re = imagesRS.enumerateRecords(null, null, true); int numRecs = re.numRecords(); // For each record for (int i = 0; i < numRecs; i++) { // Get the next record's ID int recId = re.nextRecordId(); // throws // InvalidRecordIDException // Get the record byte[] rec = imagesRS.getRecord(recId); // ByteArrayInputStream bin = new ByteArrayInputStream(rec); DataInputStream din = new DataInputStream(bin); String name = din.readUTF(); // If this is the image we are looking for, load it. if (name.equals(resourceName) == false) continue; int width = din.readInt(); int height = din.readInt(); long timestamp = din.readLong(); int length = din.readInt(); int[] rawImg = new int[width * height]; // Serialize the image raw data for (i = 0; i < length; i++) { rawImg[i] = din.readInt(); } img = Image.createRGBImage(rawImg, width, height, false); din.close(); bin.close(); } } catch (InvalidRecordIDException ignore) { // End of enumeration, ignore } catch (Exception e) { Log.error("Exception while retrieving Image: " + e.getMessage()); } finally { try { // Close the Record Store if (imagesRS != null) imagesRS.closeRecordStore(); } catch (Exception ignore) { // Ignore } } return img; }
/** * Removes expired images from the cache * * @param recordStore is the name of the record store. * @param number of seconds for the expiration * @return the number of images expired. */ public static int clearExpiredImages(String recordStore, int expireNumSeconds) { RecordStore imagesRS = null; int deletedRecs = 0; try { imagesRS = RecordStore.openRecordStore(recordStore, true); RecordEnumeration re = imagesRS.enumerateRecords(null, null, true); int numRecs = re.numRecords(); // For each record for (int i = 0; i < numRecs; i++) { // Get the next record's ID int recId = re.nextRecordId(); // throws // InvalidRecordIDException // Get the record byte[] rec = imagesRS.getRecord(recId); ByteArrayInputStream bin = new ByteArrayInputStream(rec); DataInputStream din = new DataInputStream(bin); String name = din.readUTF(); int width = din.readInt(); int height = din.readInt(); long timestamp = din.readLong(); // calculate expiration, and remove record if expired long now = System.currentTimeMillis(); long delta = now - timestamp; // delta in milliseconds // Remove this record if it has expired. // Convert delta mills to seconds prior to test. if ((delta / 1000) > expireNumSeconds) { imagesRS.deleteRecord(recId); deletedRecs++; } din.close(); bin.close(); } } catch (InvalidRecordIDException ignore) { // End of enumeration, ignore } catch (Exception e) { // Log the Exception } finally { try { // Close the Record Store if (imagesRS != null) imagesRS.closeRecordStore(); } catch (Exception ignore) { // Ignore } } return deletedRecs; }
public RMSOutputStream(String storename) throws IOException { super(); rsname = storename; try { rs = RecordStore.openRecordStore(rsname, true); // clear up any old content! RecordEnumeration records = rs.enumerateRecords(null, null, false); while (records.hasNextElement()) { rs.deleteRecord(records.nextRecordId()); } records.destroy(); } catch (Exception e) { throw new IOException(e.toString()); } }
/** Zobrazi vzorec */ public void view() { try { RecordEnumeration rc = recordStore.enumerateRecords(this, this, true); rc.rebuild(); int id = 0; for (int i = 0; i < gui.get_lstPatterns().getSelectedIndex() + 1; i++) { id = rc.nextRecordId(); } DataInputStream dis = new DataInputStream(new ByteArrayInputStream(recordStore.getRecord(id))); gui.get_tfPatternName().setString(dis.readUTF()); gui.get_tfEditPatternLattitude().setString(dis.readUTF()); gui.get_tfEditPatternLongitude().setString(dis.readUTF()); } catch (Exception e) { gui.showError("viewPattern", e.toString(), ""); } }
/** * Return a list of all the images stored in this RMS * * @param recordStore */ public static Vector getImageList(String recordStore) { Log.debug("Getting Image list store name=[" + recordStore + "]"); RecordStore imagesRS = null; Vector v = new Vector(); try { imagesRS = RecordStore.openRecordStore(recordStore, true); RecordEnumeration re = imagesRS.enumerateRecords(null, null, true); if (re != null) { Log.debug("ImageRMSUtils: Record Enumeration was not null "); int numRecs = re.numRecords(); // For each record for (int i = 0; i < numRecs; i++) { // Get the next record's ID int recId = re.nextRecordId(); // throws // InvalidRecordIDException // Get the record Log.debug("Got a record"); byte[] rec = imagesRS.getRecord(recId); ByteArrayInputStream bin = new ByteArrayInputStream(rec); DataInputStream din = new DataInputStream(bin); v.addElement(din.readUTF()); bin.close(); } } else { Log.error("ImageRMSUtils Exception Record Enumeration was null "); } Log.debug("Retrieved " + v.size() + " images from RMS"); } catch (InvalidRecordIDException ignore) { Log.error("RecordId Exception " + ignore.getMessage()); } catch (Exception e) { Log.error("ImageRMSUtils Exception " + e.getMessage()); } finally { try { // Close the Record Store if (imagesRS != null) imagesRS.closeRecordStore(); } catch (Exception ignore) { // Ignore } } return v; }
private void load() throws RecordStoreException { RecordStore rs = null; RecordEnumeration re = null; try { rs = RecordStore.openRecordStore(mRecordStoreName, true); re = rs.enumerateRecords(null, null, false); while (re.hasNextElement()) { byte[] raw = re.nextRecord(); String pref = new String(raw); // Parse out the name. int index = pref.indexOf('|'); String name = pref.substring(0, index); String value = pref.substring(index + 1); put(name, value); } } finally { if (re != null) re.destroy(); if (rs != null) rs.closeRecordStore(); } }
public int postViaHttpConnection(String url) { // throws IOException, // RecordStoreException { HttpConnection c = null; InputStream is = null; OutputStream os = null; int rc = -1; int recID = 0; RecordEnumeration recIter = null; SigSeg sigSeg = null; this.log("Test1"); try { try { c = (HttpConnection) Connector.open(url, Connector.READ_WRITE); } catch (IllegalArgumentException e) { this.alertError("post:open IllegalArgument: parameter is invalid. " + e.getMessage()); throw e; } catch (ConnectionNotFoundException e) { this.alertError( "post:open ConnectionNotFound: target not found, or protocol not supported. " + e.getMessage()); throw e; } catch (IOException e) { this.alertError("post:open IOException: " + e.getMessage()); throw e; } catch (SecurityException e) { this.alertError("post:open SecurityException: " + e.getMessage()); throw e; } this.log("Test2"); try { c.setRequestMethod(HttpConnection.POST); } catch (IOException e) { this.alertError( "post:setReqMethod IOException: the method cannot be reset or if the requested method isn't valid for HTTP:" + e.getMessage()); throw e; } this.log("Test3"); try { c.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0"); c.setRequestProperty("Accept", "text/plain"); c.setRequestProperty("Accept-Encoding", "identity"); } catch (IOException e) { this.alertError( "post:setReqProperty IOException: connection is in connected state." + e.getMessage()); throw e; } this.log("Test4"); try { os = c.openOutputStream(); } catch (IOException e) { this.alertError( "post:openOutputStream IOException: maybe output stream has been closed?" + e.getMessage()); throw e; } this.log("Test5"); StringBuffer postBuf = new StringBuffer(); postBuf.append("email=adparker%40gmail.com"); postBuf.append("&pw=ecopda"); postBuf.append("&type=xml"); postBuf.append("&project_id=43"); postBuf.append("&tableName=test2"); postBuf.append("&data_string="); try { recIter = this.recordStore.enumerateRecords(null, null, true); } catch (RecordStoreNotOpenException e) { this.alertError("post:enumrateRecords RecordStoreNotOpenException" + e.getMessage()); throw e; } this.log("Test6"); try { recID = recIter.nextRecordId(); } catch (InvalidRecordIDException e) { this.alertError("post:nextRecordId: no more records." + e.getMessage()); throw e; } this.log("Test7"); try { sigSeg = new SigSeg(this.recordStore, recID); } catch (RecordStoreNotOpenException e) { alertError("post:SigSeg RecordStoreNotOpen "); // + // e.getMessage()); throw e; } catch (InvalidRecordIDException e) { alertError("post:SigSeg InvalidIDException "); // + // e.getMessage()); throw e; } catch (RecordStoreException e) { alertError("post:SigSeg RecordStoreException"); // ";//+ // e.getMessage());; throw e; } catch (IOException e) { alertError("post:SigSeg IOException " + e.getMessage()); throw e; } this.log("Test8"); postBuf.append(URLEncode.encode("<table>")); postBuf.append(URLEncode.encode("<row>")); // <field name="user">ASDFASDF</field> postBuf.append(URLEncode.encode("<field name=\"user\">")); String _userName = this.midlet.strItem_userName.getString(); postBuf.append(URLEncode.encode(_userName)); postBuf.append(URLEncode.encode("</field>")); postBuf.append(URLEncode.encode(sigSeg.toXML())); // postBuf.append(sigSeg.toXML()); // sigSeg.toXML(); postBuf.append(URLEncode.encode("</row>")); postBuf.append(URLEncode.encode("</table>")); // URL encode! try { // String urlenc = URLEncode.encode(postBuf.toString()); // String urlenc = postBuf.toString(); os.write(postBuf.toString().getBytes()); } catch (IOException e) { alertError("post:os.write IOException " + e.getMessage()); throw e; } this.log("Test9"); try { os.flush(); } catch (IOException e) { alertError("post:os.flush IOException " + e.getMessage()); throw e; } this.log("Test10"); try { rc = c.getResponseCode(); } catch (IOException e) { alertError("post:c.getResponseCode IOException" + e.getMessage()); throw e; } this.log("Test11"); if (rc != HttpConnection.HTTP_OK) { this.alertError("HTTP response code: " + String.valueOf(rc)); } else { ++this.int_recordsSent; } this.popRecord(recID); this.updateView(); } catch (Exception e) { } finally { if (is != null) try { is.close(); } catch (IOException e) { e.printStackTrace(); } if (os != null) try { os.close(); } catch (IOException e) { e.printStackTrace(); } if (c != null) try { c.close(); } catch (IOException e) { e.printStackTrace(); } } return rc; }