/** Stop talking to the server */
 public void stop() {
   if (socket != null) {
     // Close all the streams and socket
     if (out != null) {
       try {
         out.close();
       } catch (IOException ioe) {
       }
       out = null;
     }
     if (in != null) {
       try {
         in.close();
       } catch (IOException ioe) {
       }
       in = null;
     }
     if (socket != null) {
       try {
         socket.close();
       } catch (IOException ioe) {
       }
       socket = null;
     }
   } else {
     // Already stopped
   }
   // Make sure the right buttons are enabled
   start_button.setEnabled(true);
   stop_button.setEnabled(false);
   setStatus(STATUS_STOPPED);
 }
 public void windowOpened(WindowEvent e) // read file on start
     {
   FileInputStream in = null;
   ObjectInputStream data = null;
   try {
     in = new FileInputStream(DB);
     data = new ObjectInputStream(in);
     p = (Person) data.readObject();
     while (p != null) {
       persons.add(p); // store Person object in ArrayList collection
       p = (Person) data.readObject(); // read the next record
     }
     data.close();
   } catch (Exception ex) {
     // IOException will always occur on the last read above
   } finally {
     if (persons.size() > 0) displayRecord();
   }
 }
Example #3
0
  /** DOCUMENT ME! */
  public void loadRaids() {
    File f = new File(REPO);

    if (!f.exists()) {
      return;
    }

    FileInputStream fIn = null;
    ObjectInputStream oIn = null;

    try {
      fIn = new FileInputStream(REPO);
      oIn = new ObjectInputStream(fIn);
      // de-serializing object
      raids = (HashMap<String, GregorianCalendar>) oIn.readObject();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } finally {
      try {
        oIn.close();
        fIn.close();
      } catch (IOException e1) {
        e1.printStackTrace();
      }
    }
  }
Example #4
0
  /** @bug 4110613 */
  public void TestSerialization()
      throws ClassNotFoundException, OptionalDataException, IOException, StreamCorruptedException {
    ObjectOutputStream ostream;
    ByteArrayOutputStream obstream;
    byte[] bytes = null;

    obstream = new ByteArrayOutputStream();
    ostream = new ObjectOutputStream(obstream);

    Locale test1 = new Locale("zh", "TW", "");
    int dummy = test1.hashCode(); // fill in the cached hash-code value
    ostream.writeObject(test1);

    bytes = obstream.toByteArray();

    ObjectInputStream istream = new ObjectInputStream(new ByteArrayInputStream(bytes));

    Locale test2 = (Locale) (istream.readObject());

    if (!test1.equals(test2) || test1.hashCode() != test2.hashCode())
      errln("Locale failed to deserialize correctly.");
  }
 /**
  * Subclassed to update the internal representation of the mask after the default read operation
  * has completed.
  */
 private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
   s.defaultReadObject();
   updateMaskIfNecessary();
 }
  /**
   * Creates source for sources with two orders: firstly in RIC, secondly in time.
   *
   * @param filePath complete filename and path containing prices.
   * @param symbols set of symbols present into the file.
   * @param lineProcessor strategy to part each line.
   * @throws IOException if one symbol (at least) cannot be found.
   */
  public MultipleSymbolScanner(
      final String filePath,
      final String[] symbols,
      final LineParser lineParser,
      final SpreadTradesMgr[] spreadTradesMgr)
      throws IOException {
    this.lineParser = lineParser;
    this.spreadTradesMgr = spreadTradesMgr;
    this.filePath = filePath;
    this.symbols = symbols;
    this.mainReader = new RandomAccessFile(filePath, "r");
    this.partReader = new RandomAccessFile[symbols.length];
    this.nextMilliseconds = new long[symbols.length];
    this.nextLine = new String[symbols.length];

    boolean optimisticJumps = true;

    final ErrorControl errorControl = this.lineParser.getErrorControl();
    this.lineParser.setErrorControl(LineParser.nullErrorControl);

    final String cacheFilePath = filePath + ".cachejump";

    if (new File(cacheFilePath).exists()) {
      final ObjectInputStream cacheJumpsFileIn =
          new ObjectInputStream(new FileInputStream(cacheFilePath));

      log.info("Using stored cached jumps in file " + cacheFilePath);
      try {
        try {
          for (; ; ) {
            final String s = cacheJumpsFileIn.readUTF();
            final long pos = cacheJumpsFileIn.readLong();

            for (int i = 0; i < symbols.length; i++) {
              if (symbols[i].equals(s)) {
                partReader[i] = new RandomAccessFile(filePath, "r");
                partReader[i].seek(pos);
                log.info("Using cached information: position " + pos + " for " + s);
                break;
              }
            }
          }
        } catch (IOException ioe) {
          cacheJumpsFileIn.close();
        }
        for (int i = 0; i < partReader.length; i++) {
          if (partReader[i] == null) {
            throw new IOException("Symbol " + symbols[i] + " not placed");
          }
        }
      } catch (IOException ioe) {
        log.log(Level.SEVERE, ioe.toString(), ioe);
        log.severe("Error reading file " + cacheFilePath + ". Removing");
        new File(cacheFilePath).delete();
      }
    } else {
      final ObjectOutputStream cacheJumpsFileOut =
          new ObjectOutputStream(new FileOutputStream(cacheFilePath));
      log.fine("Caching jumps for future uses in file " + cacheFilePath);

      log.info("Scannig file to place reading pointers");

      for (int i = 0; i < symbols.length; i++) {
        long lastPosition = this.mainReader.getFilePointer();
        long foundPosition;
        log.fine("Starting to search symbol " + symbols[i] + " from position " + lastPosition);
        for (; ; ) {
          foundPosition = this.mainReader.getFilePointer();
          final String line = readNextLineCycling(lastPosition);
          if (line == null) {
            // All file's been read without finding desired symbol.
            throw new IOException(
                "Cannot find symbol '" + symbols[i] + "' in file '" + filePath + "'");
          }
          if (this.lineParser.isValid(line)
              && symbols[i].equals(this.lineParser.getSymbol(line, 0))) {
            break;
          }
        }
        log.info("Located place for reader on " + symbols[i] + ":" + foundPosition);
        cacheJumpsFileOut.writeUTF(symbols[i]);
        cacheJumpsFileOut.writeLong(foundPosition);
        partReader[i] = new RandomAccessFile(filePath, "r");
        partReader[i].seek(foundPosition);
        final int optimisticJump = (int) (2 * (foundPosition - lastPosition) / 3);
        this.mainReader.skipBytes(optimisticJump);
      }
      cacheJumpsFileOut.flush();
      cacheJumpsFileOut.close();
    }

    log.info("File scanned. Placing reading pointers");

    // Boot up
    for (int i = 0; i < symbols.length; i++) {
      for (; ; ) {
        try {
          String line = partReader[i].readLine();
          while (line != null && !this.lineParser.isValid(line)) {
            line = partReader[i].readLine();
          }

          if (!symbols[i].equals(this.lineParser.parse(line).getSymbol(0))) {
            this.nextLine[i] = null;
          } else {
            this.nextLine[i] = line;
            this.nextMilliseconds[i] =
                this.lineParser.parse(line).getUTCTimestampCopy().getTimeInMillis();
            break;
          }
        } catch (Exception e) {
          log.log(
              Level.SEVERE, "Exception reading first line " + "of file '" + this.filePath + "'", e);
          log.severe("Maybe cache file is not correct?");
        }
      }
    }
    log.info("Readers placed correctly. Source started");
    this.lineParser.setErrorControl(errorControl);
  }
    /** Background thread used to receive data from the server. */
    public void run() {
      Long id;
      Integer message_type;
      String target;
      String soap;
      SOAPMonitorData data;
      int selected;
      int row;
      boolean update_needed;
      while (socket != null) {
        try {
          // Get the data from the server
          message_type = (Integer) in.readObject();
          // Process the data depending on its type
          switch (message_type.intValue()) {
            case SOAPMonitorConstants.SOAP_MONITOR_REQUEST:
              // Get the id, target and soap info
              id = (Long) in.readObject();
              target = (String) in.readObject();
              soap = (String) in.readObject();
              // Add new request data to the table
              data = new SOAPMonitorData(id, target, soap);
              model.addData(data);
              // If "most recent" selected then update
              // the details area if needed
              selected = table.getSelectedRow();
              if ((selected == 0) && model.filterMatch(data)) {
                valueChanged(null);
              }
              break;
            case SOAPMonitorConstants.SOAP_MONITOR_RESPONSE:
              // Get the id and soap info
              id = (Long) in.readObject();
              soap = (String) in.readObject();
              data = model.findData(id);
              if (data != null) {
                update_needed = false;
                // Get the selected row
                selected = table.getSelectedRow();
                // If "most recent", then always
                // update details area
                if (selected == 0) {
                  update_needed = true;
                }
                // If the data being updated is
                // selected then update details
                row = model.findRow(data);
                if ((row != -1) && (row == selected)) {
                  update_needed = true;
                }
                // Set the response and update table
                data.setSOAPResponse(soap);
                model.updateData(data);
                // Refresh details area (if needed)
                if (update_needed) {
                  valueChanged(null);
                }
              }
              break;
          }

        } catch (Exception e) {
          // Exceptions are expected here when the
          // server communication has been terminated.
          if (stop_button.isEnabled()) {
            stop();
            setErrorStatus(STATUS_CLOSED);
          }
        }
      }
    }