Package java.io

Examples of java.io.ObjectInputStream$GetField


    if (logger.isLoggable(BasicLevel.DEBUG))
      logger.log(BasicLevel.DEBUG, "loadobj, after load call");

    ByteArrayInputStream bis = new ByteArrayInputStream(content);
    ObjectInputStream ois = new ObjectInputStream(bis);
    try {
      Object obj = ois.readObject();
      return obj;
    } catch (Exception e) {
      String exceptionString = e.toString();
      if (exceptionString.indexOf("KNOWN PROBLEM") == -1)
      {
          e.printStackTrace();
      }
      throw new IOException(e.getMessage());
    } finally {
      ois.close();
      bis.close();
    }
  }
View Full Code Here


  public <T> T loadAttachment(File attachmentsStore, Class<T> expected) throws IOException, ClassNotFoundException {
    if (log.isTraceEnabled()) {
      log.trace("loadAttachment, attachmentsStore=" + attachmentsStore); //$NON-NLS-1$
    }

    ObjectInputStream ois = null;
    try {
      ois = new ObjectInputStream(new FileInputStream(attachmentsStore));
      return expected.cast(ois.readObject());
    } finally {
      if (ois != null) {
        ois.close();
      }
    }
  }
View Full Code Here

      }
      int returnVal = m_FileChooser.showOpenDialog(this);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
  File selected = m_FileChooser.getSelectedFile();
  try {
    ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(selected)));
    Object obj = oi.readObject();
    oi.close();
    if (!m_ClassType.isAssignableFrom(obj.getClass())) {
      throw new Exception("Object not of type: " + m_ClassType.getName());
    }
    return obj;
  } catch (Exception ex) {
View Full Code Here

    trainStatistics = true,
    printMargins = false, printComplexityStatistics = false,
    printGraph = false, classStatistics = false, printSource = false;
    StringBuffer text = new StringBuffer();
    DataSource trainSource = null, testSource = null;
    ObjectInputStream objectInputStream = null;
    BufferedInputStream xmlInputStream = null;
    CostMatrix costMatrix = null;
    StringBuffer schemeOptionsText = null;
    long trainTimeStart = 0, trainTimeElapsed = 0,
    testTimeStart = 0, testTimeElapsed = 0;
    String xml = "";
    String[] optionsTmp = null;
    Classifier classifierBackup;
    Classifier classifierClassifications = null;
    int actualClassIndex = -1// 0-based class index
    String splitPercentageString = "";
    double splitPercentage = -1;
    boolean preserveOrder = false;
    boolean trainSetPresent = false;
    boolean testSetPresent = false;
    String thresholdFile;
    String thresholdLabel;
    StringBuffer predsBuff = null; // predictions from cross-validation
    AbstractOutput classificationOutput = null;

    // help requested?
    if (Utils.getFlag("h", options) || Utils.getFlag("help", options)) {

      // global info requested as well?
      boolean globalInfo = Utils.getFlag("synopsis", options) ||
        Utils.getFlag("info", options);

      throw new Exception("\nHelp requested."
          + makeOptionString(classifier, globalInfo));
    }

    try {
      // do we get the input from XML instead of normal parameters?
      xml = Utils.getOption("xml", options);
      if (!xml.equals(""))
        options = new XMLOptions(xml).toArray();

      // is the input model only the XML-Options, i.e. w/o built model?
      optionsTmp = new String[options.length];
      for (int i = 0; i < options.length; i++)
        optionsTmp[i] = options[i];

      String tmpO = Utils.getOption('l', optionsTmp);
      //if (Utils.getOption('l', optionsTmp).toLowerCase().endsWith(".xml")) {
      if (tmpO.endsWith(".xml")) {
        // try to load file as PMML first
        boolean success = false;
        try {
          PMMLModel pmmlModel = PMMLFactory.getPMMLModel(tmpO);
          if (pmmlModel instanceof PMMLClassifier) {
            classifier = ((PMMLClassifier)pmmlModel);
            success = true;
          }
        } catch (IllegalArgumentException ex) {
          success = false;
        }
        if (!success) {
          // load options from serialized data  ('-l' is automatically erased!)
          XMLClassifier xmlserial = new XMLClassifier();
          OptionHandler cl = (OptionHandler) xmlserial.read(Utils.getOption('l', options));

          // merge options
          optionsTmp = new String[options.length + cl.getOptions().length];
          System.arraycopy(cl.getOptions(), 0, optionsTmp, 0, cl.getOptions().length);
          System.arraycopy(options, 0, optionsTmp, cl.getOptions().length, options.length);
          options = optionsTmp;
        }
      }

      noCrossValidation = Utils.getFlag("no-cv", options);
      // Get basic options (options the same for all schemes)
      classIndexString = Utils.getOption('c', options);
      if (classIndexString.length() != 0) {
        if (classIndexString.equals("first"))
          classIndex = 1;
        else if (classIndexString.equals("last"))
          classIndex = -1;
        else
          classIndex = Integer.parseInt(classIndexString);
      }
      trainFileName = Utils.getOption('t', options);
      objectInputFileName = Utils.getOption('l', options);
      objectOutputFileName = Utils.getOption('d', options);
      testFileName = Utils.getOption('T', options);
      foldsString = Utils.getOption('x', options);
      if (foldsString.length() != 0) {
        folds = Integer.parseInt(foldsString);
      }
      seedString = Utils.getOption('s', options);
      if (seedString.length() != 0) {
        seed = Integer.parseInt(seedString);
      }
      if (trainFileName.length() == 0) {
        if (objectInputFileName.length() == 0) {
          throw new Exception("No training file and no object input file given.");
        }
        if (testFileName.length() == 0) {
          throw new Exception("No training file and no test file given.");
        }
      } else if ((objectInputFileName.length() != 0) &&
          ((!(classifier instanceof UpdateableClassifier)) ||
           (testFileName.length() == 0))) {
        throw new Exception("Classifier not incremental, or no " +
            "test file provided: can't "+
            "use both train and model file.");
      }
      try {
        if (trainFileName.length() != 0) {
          trainSetPresent = true;
          trainSource = new DataSource(trainFileName);
        }
        if (testFileName.length() != 0) {
          testSetPresent = true;
          testSource = new DataSource(testFileName);
        }
        if (objectInputFileName.length() != 0) {
          if (objectInputFileName.endsWith(".xml")) {
            // if this is the case then it means that a PMML classifier was
            // successfully loaded earlier in the code
            objectInputStream = null;
            xmlInputStream = null;
          } else {
            InputStream is = new FileInputStream(objectInputFileName);
            if (objectInputFileName.endsWith(".gz")) {
              is = new GZIPInputStream(is);
            }
            // load from KOML?
            if (!(objectInputFileName.endsWith(".koml") && KOML.isPresent()) ) {
              objectInputStream = new ObjectInputStream(is);
              xmlInputStream    = null;
            }
            else {
              objectInputStream = null;
              xmlInputStream    = new BufferedInputStream(is);
            }
          }
        }
      } catch (Exception e) {
        throw new Exception("Can't open file " + e.getMessage() + '.');
      }
      if (testSetPresent) {
        template = test = testSource.getStructure();
        if (classIndex != -1) {
          test.setClassIndex(classIndex - 1);
        } else {
          if ( (test.classIndex() == -1) || (classIndexString.length() != 0) )
            test.setClassIndex(test.numAttributes() - 1);
        }
        actualClassIndex = test.classIndex();
      }
      else {
        // percentage split
        splitPercentageString = Utils.getOption("split-percentage", options);
        if (splitPercentageString.length() != 0) {
          if (foldsString.length() != 0)
            throw new Exception(
                "Percentage split cannot be used in conjunction with "
                + "cross-validation ('-x').");
          splitPercentage = Double.parseDouble(splitPercentageString);
          if ((splitPercentage <= 0) || (splitPercentage >= 100))
            throw new Exception("Percentage split value needs be >0 and <100.");
        }
        else {
          splitPercentage = -1;
        }
        preserveOrder = Utils.getFlag("preserve-order", options);
        if (preserveOrder) {
          if (splitPercentage == -1)
            throw new Exception("Percentage split ('-percentage-split') is missing.");
        }
        // create new train/test sources
        if (splitPercentage > 0) {
          testSetPresent = true;
          Instances tmpInst = trainSource.getDataSet(actualClassIndex);
          if (!preserveOrder)
            tmpInst.randomize(new Random(seed));
          int trainSize =
            (int) Math.round(tmpInst.numInstances() * splitPercentage / 100);
          int testSize  = tmpInst.numInstances() - trainSize;
          Instances trainInst = new Instances(tmpInst, 0, trainSize);
          Instances testInst  = new Instances(tmpInst, trainSize, testSize);
          trainSource = new DataSource(trainInst);
          testSource  = new DataSource(testInst);
          template = test = testSource.getStructure();
          if (classIndex != -1) {
            test.setClassIndex(classIndex - 1);
          } else {
            if ( (test.classIndex() == -1) || (classIndexString.length() != 0) )
              test.setClassIndex(test.numAttributes() - 1);
          }
          actualClassIndex = test.classIndex();
        }
      }
      if (trainSetPresent) {
        template = train = trainSource.getStructure();
        if (classIndex != -1) {
          train.setClassIndex(classIndex - 1);
        } else {
          if ( (train.classIndex() == -1) || (classIndexString.length() != 0) )
            train.setClassIndex(train.numAttributes() - 1);
        }
        actualClassIndex = train.classIndex();
        if (!(classifier instanceof weka.classifiers.misc.InputMappedClassifier)) {
          if ((testSetPresent) && !test.equalHeaders(train)) {
            throw new IllegalArgumentException("Train and test file not compatible!\n" + test.equalHeadersMsg(train));
          }
        }
      }
      if (template == null) {
        throw new Exception("No actual dataset provided to use as template");
      }
      costMatrix = handleCostOption(
          Utils.getOption('m', options), template.numClasses());

      classStatistics = Utils.getFlag('i', options);
      noOutput = Utils.getFlag('o', options);
      trainStatistics = !Utils.getFlag('v', options);
      printComplexityStatistics = Utils.getFlag('k', options);
      printMargins = Utils.getFlag('r', options);
      printGraph = Utils.getFlag('g', options);
      sourceClass = Utils.getOption('z', options);
      printSource = (sourceClass.length() != 0);
      thresholdFile = Utils.getOption("threshold-file", options);
      thresholdLabel = Utils.getOption("threshold-label", options);

      String classifications = Utils.getOption("classifications", options);
      String classificationsOld = Utils.getOption("p", options);
      if (classifications.length() > 0) {
        noOutput = true;
        classificationOutput = AbstractOutput.fromCommandline(classifications);
        classificationOutput.setHeader(template);
      }
      // backwards compatible with old "-p range" and "-distribution" options
      else if (classificationsOld.length() > 0) {
        noOutput = true;
        classificationOutput = new PlainText();
        classificationOutput.setHeader(template);
        if (!classificationsOld.equals("0"))
          classificationOutput.setAttributes(classificationsOld);
        classificationOutput.setOutputDistribution(Utils.getFlag("distribution", options));
      }
      // -distribution flag needs -p option
      else {
        if (Utils.getFlag("distribution", options))
          throw new Exception("Cannot print distribution without '-p' option!");
      }

      // if no training file given, we don't have any priors
      if ( (!trainSetPresent) && (printComplexityStatistics) )
        throw new Exception("Cannot print complexity statistics ('-k') without training file ('-t')!");

      // If a model file is given, we can't process
      // scheme-specific options
      if (objectInputFileName.length() != 0) {
        Utils.checkForRemainingOptions(options);
      } else {

        // Set options for classifier
        if (classifier instanceof OptionHandler) {
          for (int i = 0; i < options.length; i++) {
            if (options[i].length() != 0) {
              if (schemeOptionsText == null) {
                schemeOptionsText = new StringBuffer();
              }
              if (options[i].indexOf(' ') != -1) {
                schemeOptionsText.append('"' + options[i] + "\" ");
              } else {
                schemeOptionsText.append(options[i] + " ");
              }
            }
          }
          ((OptionHandler)classifier).setOptions(options);
        }
      }

      Utils.checkForRemainingOptions(options);
    } catch (Exception e) {
      throw new Exception("\nWeka exception: " + e.getMessage()
          + makeOptionString(classifier, false));
    }

    if (objectInputFileName.length() != 0) {
      // Load classifier from file
      if (objectInputStream != null) {
        classifier = (Classifier) objectInputStream.readObject();
        // try and read a header (if present)
        Instances savedStructure = null;
        try {
          savedStructure = (Instances) objectInputStream.readObject();
        } catch (Exception ex) {
          // don't make a fuss
        }
        if (savedStructure != null) {
          // test for compatibility with template
          if (!template.equalHeaders(savedStructure)) {
            throw new Exception("training and test set are not compatible\n" + template.equalHeadersMsg(savedStructure));
          }
        }
        objectInputStream.close();
      }
      else if (xmlInputStream != null) {
        // whether KOML is available has already been checked (objectInputStream would null otherwise)!
        classifier = (Classifier) KOML.read(xmlInputStream);
        xmlInputStream.close();
View Full Code Here

   * undoes the last action
   */
  public void undo() {
    File                  tempFile;
    Instances             inst;
    ObjectInputStream     ooi;
   
    if (canUndo()) {
      // load file
      tempFile = (File) m_UndoList.get(m_UndoList.size() - 1);
      try {
        // read serialized data
        ooi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(tempFile)));
        inst = (Instances) ooi.readObject();
        ooi.close();
       
        // set instances
        setInstances(inst);
        notifyListener(new TableModelEvent(this, TableModelEvent.HEADER_ROW));
        notifyListener(new TableModelEvent(this));
View Full Code Here

   *@param  blockSize  The size of the blocks to split the array into. This must be an exact divisor of the length of the array, or some data will be lost from the main array.
   *@return            An array of arrays in which each element in the returned array will be of length <code>blockSize</code>.
   */
  private final void loadD16() {
    float d[] = null;
    ObjectInputStream in = null;

    try {
      in = new ObjectInputStream(getClass().getClassLoader().getResourceAsStream("d16.ser"));
      d = (float[]) in.readObject();
    }
    catch (Exception e) {
      System.out.println("2 couldn't load the array for the SynthesisFilter ");
      System.exit(1);
    }
    finally {
      try {
        in.close();
      }
      catch (Exception e) { }
    }
    int size = d.length / 16;
    d16 = new float[size] [];
View Full Code Here

      }
    }
  }

  private void loadTables() {
  ObjectInputStream in = null;
  int array[][]=null;

  try{
    in = new ObjectInputStream(getClass().getClassLoader().getResourceAsStream("huffman.ser"));
    ht = new HuffmanTables[HTN];
    array = (int[][])in.readObject();
    ht[0] = new HuffmanTables("0  ", 0, 0, 0, 0, -1,  array, 0);
    array = (int[][])in.readObject();
    ht[1] = new HuffmanTables("1  ", 2, 2, 0, 0, -1,   array, 7);
    array = (int[][])in.readObject();
    ht[2] = new HuffmanTables("2  ", 3, 3, 0, 0, -1,   array, 17);
    array = (int[][])in.readObject();
    ht[3] = new HuffmanTables("3  ", 3, 3, 0, 0, -1,   array, 17);
    array = (int[][])in.readObject();
    ht[4] = new HuffmanTables("4  ", 0, 0, 0, 0, -1,   array, 0);
    array = (int[][])in.readObject();
    ht[5] = new HuffmanTables("5  ", 4, 4, 0, 0, -1,   array, 31);
    array = (int[][])in.readObject();
    ht[6] = new HuffmanTables("6  ", 4, 4, 0, 0, -1,   array, 31);
    array = (int[][])in.readObject();
    ht[7] = new HuffmanTables("7  ", 6, 6, 0, 0, -1,   array, 71);
    array = (int[][])in.readObject();
    ht[8] = new HuffmanTables("8  ", 6, 6, 0, 0, -1,   array, 71);
    array = (int[][])in.readObject();
    ht[9] = new HuffmanTables("9  ", 6, 6, 0, 0, -1,   array, 71);
    array = (int[][])in.readObject();
    ht[10] = new HuffmanTables("10 ", 8, 8, 0, 0, -1,   array, 127);
    array = (int[][])in.readObject();
    ht[11] = new HuffmanTables("11 ", 8, 8, 0, 0, -1,   array, 127);
    array = (int[][])in.readObject();
    ht[12] = new HuffmanTables("12 ", 8, 8, 0, 0, -1,   array, 127);
    array = (int[][])in.readObject();
    ht[13] = new HuffmanTables("13 ", 16, 16, 0, 0, -1,   array, 511);
    array = (int[][])in.readObject();
    ht[14] = new HuffmanTables("14 ", 0, 0, 0, 0, -1,  array, 0);
    array = (int[][])in.readObject();
    ht[15] = new HuffmanTables("15 ", 16, 16, 0, 0, -1,   array, 511);
    array = (int[][])in.readObject();
    ht[16] = new HuffmanTables("16 ", 16, 16, 1, 1, -1,   array, 511);
    ht[17] = new HuffmanTables("17 ", 16, 16, 2, 3, 16,   (int[][])array.clone(), 511);
    ht[18] = new HuffmanTables("18 ", 16, 16, 3, 7, 16,   (int[][])array.clone(), 511);
    ht[19] = new HuffmanTables("19 ", 16, 16, 4, 15, 16,   (int[][])array.clone(), 511);
    ht[20] = new HuffmanTables("20 ", 16, 16, 6, 63, 16,   (int[][])array.clone(), 511);
    ht[21] = new HuffmanTables("21 ", 16, 16, 8, 255, 16,   (int[][])array.clone(), 511);
    ht[22] = new HuffmanTables("22 ", 16, 16, 10, 1023, 16,   (int[][])array.clone(), 511);
    ht[23] = new HuffmanTables("23 ", 16, 16, 13, 8191, 16,   (int[][])array.clone(), 511);
    array = (int[][])in.readObject();
    ht[24] = new HuffmanTables("24 ", 16, 16, 4, 15, -1,   array, 512);
    ht[25] = new HuffmanTables("25 ", 16, 16, 5, 31, 24,   (int[][])array.clone(), 512);
    ht[26] = new HuffmanTables("26 ", 16, 16, 6, 63, 24,   (int[][])array.clone(), 512);
    ht[27] = new HuffmanTables("27 ", 16, 16, 7, 127, 24,   (int[][])array.clone(), 512);
    ht[28] = new HuffmanTables("28 ", 16, 16, 8, 255, 24,   (int[][])array.clone(), 512);
    ht[29] = new HuffmanTables("29 ", 16, 16, 9, 511, 24,   (int[][])array.clone(), 512);
    ht[30] = new HuffmanTables("30 ", 16, 16, 11, 2047, 24,   (int[][])array.clone(), 512);
    ht[31] = new HuffmanTables("31 ", 16, 16, 13, 8191, 24,   (int[][])array.clone(), 512);
    array = (int[][])in.readObject();
    ht[32] = new HuffmanTables("32 ", 1, 16, 0, 0, -1,   array, 31);
    array = (int[][])in.readObject();
    ht[33] = new HuffmanTables("33 ", 1, 16, 0, 0, -1,   array, 31);
  }
  catch(Exception e){
    System.out.println("couldn't load the Huffman Tables");
    System.exit(1);
  }
  finally{
    try{
    in.close();
    }
    catch(Exception e){}
  }
  }
View Full Code Here

      try {
  InputStream is = new FileInputStream(selected);
  if (selected.getName().endsWith(".gz")) {
    is = new GZIPInputStream(is);
  }
  ObjectInputStream objectInputStream = new ObjectInputStream(is);
  clusterer = (Clusterer) objectInputStream.readObject();
  try { // see if we can load the header & ignored attribute info
    trainHeader = (Instances) objectInputStream.readObject();
    ignoredAtts = (int[]) objectInputStream.readObject();
  } catch (Exception e) {} // don't fuss if we can't
  objectInputStream.close();
      } catch (Exception e) {
 
  JOptionPane.showMessageDialog(null, e, "Load Failed",
              JOptionPane.ERROR_MESSAGE);
     
View Full Code Here

            // try and grab the header
            tempHeader = (Instances) v.elementAt(1);
          }
        } /* binary */ else {

          ObjectInputStream is =
            new ObjectInputStream(new BufferedInputStream(
                                                          new FileInputStream(loadFrom)));
          // try and read the model
          temp = (weka.classifiers.Classifier)is.readObject();
          // try and read the header (if present)
          try {
            tempHeader = (Instances)is.readObject();
          } catch (Exception ex) {
            //            System.err.println("No header...");
            // quietly ignore
          }
          is.close();
        }       

        // Update name and icon
        setTrainedClassifier(temp);
        // restore header
View Full Code Here

  private static GlobalPluginProgramFormatingManager mInstance;
  private GlobalPluginProgramFormating[] mAvailableProgramConfigurations;
 
  private GlobalPluginProgramFormatingManager() {
    mInstance = this;
    ObjectInputStream in=null;
   
    try {
      File programConfigFile = new File(Settings.getUserSettingsDirName(),"programConfigurations.dat");
     
      in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(programConfigFile), 0x1000));
     
      in.readInt(); // read version
     
      mAvailableProgramConfigurations = new GlobalPluginProgramFormating[in.readInt()];
     
      for(int i = 0; i < mAvailableProgramConfigurations.length; i++) {
        mAvailableProgramConfigurations[i] = GlobalPluginProgramFormating.load(in);
      }
     
    }catch(Exception e) {
      mAvailableProgramConfigurations = new GlobalPluginProgramFormating[2];
      mAvailableProgramConfigurations[0] = getDefaultConfiguration();
      new Thread("Plugin formating creation") {
        public void run() {
          try {
            Thread.sleep(100);
          } catch (InterruptedException e) {}
         
          mAvailableProgramConfigurations[1] = getTvPearlFormating();
          store();
        }
      }.start();
    } finally {
      if (in!=null) {
        try { in.close(); } catch(IOException exc) {}
      }
    }
  }
View Full Code Here

TOP

Related Classes of java.io.ObjectInputStream$GetField

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.