public boolean hasNext() { if (raf == null || !raf.getChannel().isOpen()) return false; if (flagNext == true) return true; // Déjà lue flagNext = true; try { next = new Entry(); next.timestamp = (long) raf.readInt(); next.value = raf.readFloat(); if (this.end != null && next.timestamp > this.end) { next = null; close(); return false; } return true; } catch (IOException e) { // EOF ou autre erreur d'IO if (!(e instanceof EOFException)) logger.log(Level.WARNING, e.getMessage(), e); next = null; try { close(); } catch (IOException e1) { logger.log(Level.WARNING, e.getMessage(), e); } return false; } }
public List<Entry> getLastPoints(int nb) throws IOException { File file = getFile(); RandomAccessFile raf = null; List<Entry> result = null; try { raf = new RandomAccessFile(file, "r"); long pos = raf.length() - (nb * DATA_LEN); if (pos < 0) { nb = nb + (int) (pos / DATA_LEN); pos = 0; } raf.seek(pos); result = new ArrayList<Entry>(nb); Entry next = null; for (int i = 0; i < nb; i++) { next = new Entry(); next.timestamp = (long) raf.readInt(); next.value = raf.readFloat(); result.add(0, next); } } finally { if (raf != null) raf.close(); } return result; }
public void post(long timestamp, float value) throws IOException { File file = getFile(); RandomAccessFile rdf = null; try { rdf = new RandomAccessFile(file, "rw"); // On vérifie que la longueur est cohérente long len = rdf.length(); if (len > 0) { int mod = (int) (len % DATA_LEN); if (mod != 0) { logger.warning( "Taille de fichier incoherence (" + len / DATA_LEN + "x" + DATA_LEN + ", reste " + mod + "). retour a " + (len - mod)); len = len - mod; } if (timestamp < getLast(rdf).timestamp) { logger.warning( "la nouvelle valeur anterieure a la derniere (prev:" + sdf.format(new Date((long) last.timestamp * 1000)) + " new:" + sdf.format(new Date((long) timestamp * 1000)) + ")"); } // Positionnement en fin de fichier rdf.seek(len); } // On écrit l'enregistrement if (timestamp > Integer.MAX_VALUE) { logger.warning("timestamp tronqué : " + timestamp + "/" + (int) timestamp); } if (last == null) last = new Entry(); last.timestamp = (int) timestamp; last.value = value; logger.fine("write: " + value + "(" + sdf.format(new Date(timestamp * 1000)) + ")"); rdf.writeInt((int) last.timestamp); rdf.writeFloat(last.value); } finally { if (rdf != null) rdf.close(); } }
public void addEntry( String feedback_from_name, String feedback_from_email, String feedback_body) { load(); Entry e = new Entry(); e.timestamp = getTimestamp(); e.feedback_id = "u2:" + UUID.randomUUID().toString(); e.feedback_from_name = feedback_from_name; e.feedback_from_email = feedback_from_email; e.feedback_body = feedback_body; e.package_versionCode = getPackageVersionCode(); e.build_version_sdk = getBuildVersionSdk(); entries_.add(e); save(); }
synchronized void load() { if (entries_ == null) { final String base = "feedback/"; entries_ = new ArrayList<>(); int nfeedback = pref_.getInt(base + "n", 0); // $NON-NLS-1$ for (int i = 0; i < nfeedback; i++) { Entry e = new Entry(); e.timestamp = pref_.getInt(base + i + "/timestamp", 0); e.feedback_id = pref_.getString(base + i + "/feedback_id", null); e.feedback_from_name = pref_.getString(base + i + "/feedback_from_name", null); e.feedback_from_email = pref_.getString(base + i + "/feedback_from_email", null); e.feedback_body = pref_.getString(base + i + "/feedback_body", null); e.package_versionCode = pref_.getInt(base + i + "/package_versionCode", 0); e.build_version_sdk = pref_.getInt(base + i + "/build_version_sdk", 0); entries_.add(e); } } }
/** * Fourni la dernière valeur Le curseur est positionné en fin de dernière valeur (pret à écrire si * ouvert en rw) * * @return * @throws IOException */ private Entry getLast(RandomAccessFile rdf) throws IOException { if (this.last == null) { File file = getFile(); long len = file.length(); if (len >= DATA_LEN) { // Il y a des données dans le fichier : lecture boolean closeFileFlag = false; if (rdf == null) { rdf = new RandomAccessFile(file, "r"); closeFileFlag = true; } long pos = len - DATA_LEN; long mod = pos % DATA_LEN; if (mod != 0) { logger.warning( "Taille de fichier incoherence (" + len / DATA_LEN + "x" + DATA_LEN + ", reste " + mod + "). retour a " + (len - mod)); pos -= mod; } rdf.seek(pos); last = new Entry(); last.timestamp = (long) rdf.readInt(); last.value = rdf.readFloat(); logger.fine("dernier enregistrement: " + last); if (closeFileFlag) rdf.close(); } } return this.last; }