示例#1
0
  public void saveObject(String key, Object object) {
    ByteArrayOutputStream bos = null;
    ObjectOutputStream oos = null;
    try {
      bos = new ByteArrayOutputStream();
      // 创建对象输出流,并封装字节流
      oos = new ObjectOutputStream(bos);
      // 将对象写入字节流
      oos.writeObject(object);

      // 将字节流编码成base64的字符窜
      byte[] bytes = bos.toByteArray();
      String value = Base64.encode(bytes);
      putString(key, value);
    } catch (StreamCorruptedException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (bos != null) bos.close();
        if (oos != null) oos.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
示例#2
0
 /*
  * recovery a class from path's file
  */
 @SuppressWarnings("finally")
 public Object readSerializableData(String path) {
   Object yyc = null;
   try {
     FileInputStream fis = new FileInputStream(path);
     ObjectInputStream ois = new ObjectInputStream(fis);
     yyc = (Object) ois.readObject();
     ois.close();
     return yyc;
   } catch (FileNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (StreamCorruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (ClassNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } finally {
     return yyc;
   }
 }
  public static Car instantiateCar(String object) {
    if (object == null) return null;

    ObjectInputStream ois = null;
    try {
      Base64InputStream b64 =
          new Base64InputStream(new ByteArrayInputStream(object.getBytes()), Base64.DEFAULT);
      ois = new ObjectInputStream(b64);
      Car car = (Car) ois.readObject();
      return car;
    } catch (StreamCorruptedException e) {
      logger.warn(e.getMessage(), e);
    } catch (IOException e) {
      logger.warn(e.getMessage(), e);
    } catch (ClassNotFoundException e) {
      logger.warn(e.getMessage(), e);
    } finally {
      if (ois != null)
        try {
          ois.close();
        } catch (IOException e) {
          logger.warn(e.getMessage(), e);
        }
    }
    return null;
  }
  private void loadFromCache() {
    // TODO Auto-generated method stub
    File f = new File(cacheArticlesPath);
    if (!f.exists()) {
      loadArticles();
      return;
    }

    try {
      ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
      Object obj = ois.readObject();
      List<Article> articles = (List<Article>) obj;
      mAdapter.getList().clear();
      mAdapter.addAll(articles);
      ois.close();
    } catch (StreamCorruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
示例#5
0
 // Helper function to load the day log from a file
 private boolean loadDayLog() {
   boolean res = false;
   String fullfilename = dayLogFileNamePrefix + dayLogFileNameSuffix;
   try {
     FileInputStream fis = this.openFileInput(fullfilename);
     ObjectInputStream is = new ObjectInputStream(fis);
     daylog = (DayLog) is.readObject();
     is.close();
     fis.close();
     res = true;
     // Log.e("Main Load", "Existing log for this date found: " + fullfilename);
   } catch (ClassNotFoundException e) {
     e.printStackTrace();
     this.deleteFile(dayLogFileNamePrefix + dayLogFileNameSuffix);
     Toast.makeText(MainActivity.this, "Broken file selected, deleting it...", Toast.LENGTH_LONG)
         .show();
   } catch (OptionalDataException e) {
     e.printStackTrace();
   } catch (FileNotFoundException e) {
     e.printStackTrace();
     Log.e("MAIN", "Logload, No log for this date found " + fullfilename);
   } catch (StreamCorruptedException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
   return res;
 }
  public static List<String> getdata(List<String> list, String data) {

    byte[] bite = Base64.decodeBase64(data.getBytes());
    System.out.println("begin----------->" + bite.toString());
    ByteArrayInputStream in = new ByteArrayInputStream(bite);
    ObjectInputStream bos;

    try {
      bos = new ObjectInputStream(in);
      try {
        System.out.println("return---------------end");
        return (List<String>) bos.readObject();
      } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        // return listnull;
        e.printStackTrace();
      }
    } catch (StreamCorruptedException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    return null;
  }
示例#7
0
 // TODO Der kommer en EOF exception når der forsøges at readObject.
 // TODO Brug sharedpref til at gemme og læse tamagotchi.
 private synchronized void loadTamagotchiFromFile() {
   FileInputStream fis = null;
   try {
     available.acquire();
     Log.d(TAMAGOTCHI, "Loading the tamagotchi from: " + fileName);
     fis = this.openFileInput(fileName);
     ObjectInputStream is = new ObjectInputStream(fis);
     // is.reset();
     // TODO Bug when reading a tama from filesystem
     // Steps to reproduce: Kill with the app context menu thingie, start app again.
     tama = (Tamagotchi) is.readObject();
     if (tama == null) return;
     is.close();
     fis.close();
     initializePlayingfield();
     Log.d(TAMAGOTCHI, "Done loading the tama");
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   } catch (ClassNotFoundException e) {
     e.printStackTrace();
   } catch (OptionalDataException e) {
     e.printStackTrace();
   } catch (StreamCorruptedException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } catch (InterruptedException e) {
     e.printStackTrace();
   } finally {
     available.release();
   }
 }
示例#8
0
  public static void pushPipelineB() {
    PushPipe<CharTransfer> pipe1 = new PushPipe<>();
    CharacterSource filter1 = new CharacterSource(pipe1);

    PushPipe<WordTransfer> pipe2 = new PushPipe<>();
    CharToWordFilter filter2 = new CharToWordFilter(pipe2);

    PushPipe<LineWithLineNumber> pipe3 = new PushPipe<>();
    WordToLineFilter filter3 = new WordToLineFilter(pipe3, len, align);

    PushPipe<LinkedList<String>> pipe4 = new PushPipe<>();
    LineToListFilter filter4 = new LineToListFilter(pipe4);

    PrintSink filter5 = new PrintSink(outputPath);

    pipe1.setSuccessorFilter(filter2);
    pipe2.setSuccessorFilter(filter3);
    pipe3.setSuccessorFilter(filter4);
    pipe4.setSuccessorFilter(filter5);

    try {
      filter1.write(new File("").getAbsolutePath() + inputPath);
    } catch (StreamCorruptedException e) {
      e.printStackTrace();
    }
  }
示例#9
0
  public static void pullPipelineA() {

    PullPipe<LinkedList<String>> pipe1 = new PullPipe<>();
    PrintSink filter1 = new PrintSink(pipe1, outputPath);

    PullPipe<LinkedList<LineWithLineNumber>> pipe2 = new PullPipe<>();
    CreateIndexFilter filter2 = new CreateIndexFilter(pipe2);

    PullPipe<WordArray> pipe3 = new PullPipe<>();
    ShiftFilter filter3 = new ShiftFilter(pipe3);

    PullPipe<LineWithLineNumber> pipe4 = new PullPipe<>();
    LineToWordFilter filter4 = new LineToWordFilter(pipe4);

    ReadLineFromRessourceSource filter5 =
        new ReadLineFromRessourceSource(new File("").getAbsolutePath() + inputPath);

    pipe1.setPredecessorFilter(filter2);
    pipe2.setPredecessorFilter(filter3);
    pipe3.setPredecessorFilter(filter4);
    pipe4.setPredecessorFilter(filter5);

    try {
      filter1.read();
    } catch (StreamCorruptedException e) {
      e.printStackTrace();
    }
  }
示例#10
0
  public Object readObject(String location) {
    try {
      Log.d("UseInternalStorage", location);
      FileInputStream fis = rootActivity.openFileInput(location);

      ObjectInputStream is = new ObjectInputStream(fis);

      Object ob = is.readObject();
      is.close();

      Log.d(TAG, "odczytano polik " + location);

      return ob;
    } catch (StreamCorruptedException e) {
      e.printStackTrace();
      return null;
    } catch (IOException e) {
      e.printStackTrace();
      return null;
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
      return null;
    } catch (NullPointerException e) {
      e.printStackTrace();
      return null;
    }
  }
示例#11
0
  public static Results initResults() {

    Results results;
    String state = Environment.getExternalStorageState();

    if (state.equals(Environment.MEDIA_MOUNTED)) { // TODO: MEDIA_READONLY
      // is exists folders
      File file = new File(Environment.getExternalStorageDirectory(), "morning-trainer");

      if (!file.isDirectory())
        Log.e(
            "mirinda1",
            file.delete()
                ? "delete"
                : "cannot delete file :" + file.getPath()); // TODO and what? if can't delete
      if (file.mkdir()) Log.i("mirinda1", "create directory: " + file.getName());
      appPath = file.getPath();

      file = new File(appPath, user);
      if (!file.isDirectory())
        Log.e("mirinda1", file.delete() ? "delete" : "cannot delete file :" + file.getPath());
      if (file.mkdir()) Log.i("mirinda1", "create directory: " + file.getName());
      userPath = file.getPath();

      String[] lst = file.list();
      Log.i("mirinda1", Arrays.toString(lst));
      // load data
      try {
        file = new File(userPath, SERIALIZED);
        if (!file.exists()) throw new IOException("not exist");
        if (!file.isFile()) throw new IOException("not a file"); // TODO my exceptions

        FileInputStream inputStream = new FileInputStream(file);
        ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
        results = (Results) objectInputStream.readObject();
        Log.i("mirinda1", results.toString());
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
        results = null;
      } catch (OptionalDataException e) {
        e.printStackTrace();
        results = null;
      } catch (FileNotFoundException e) {
        Log.e("mirinda1", "File not found");
        results = null;
      } catch (StreamCorruptedException e) {
        e.printStackTrace();
        results = null;
      } catch (IOException e) {
        Log.e("mirinda1", "" + e.getMessage());
        results = new Results(user);
      }
    } else {
      Log.e("mirinda1", "SD card didn't mounted");
      results = null;
    }
    return results;
  }
示例#12
0
 public short[] decode_frame(byte[] bytes) {
   try {
     _spxd.processData(bytes, 0, bytes.length);
   } catch (StreamCorruptedException ex) {
     Log.error("Speex Decoder error " + ex.getMessage());
   }
   short audioOut[] = new short[_aframesz];
   int decsize = _spxd.getProcessedData(audioOut, 0);
   return audioOut;
 }
示例#13
0
  /**
   * @param path
   * @return byte or null
   */
  public byte[] getFile(String path) {
    Log.d(TAG, "get file " + path); // NON-NLS

    File f = new File(getFullCachePath(path));
    if (!f.exists()) {
      Log.w(TAG, "file: " + f.toString() + " not exists"); // NON-NLS
      return null;
    }

    try {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      FileInputStream fileInputStream = new FileInputStream(f);
      byte[] buf = new byte[1024];
      int numRead = 0;
      while ((numRead = fileInputStream.read(buf)) != -1) out.write(buf, 0, numRead);

      fileInputStream.close();

      return out.toByteArray();

    } catch (FileNotFoundException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (StreamCorruptedException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (IOException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (IllegalArgumentException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (RuntimeException e) {
      // не позволим кэшу убить нашу программу
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (Exception e) {
      // не позволим кэшу убить нашу программу
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    }
  }
 @Override
 public void onImage(ImageEvent e) {
   erodeFilter = new ErodeFilter(new ImageEventReadable<ImageEvent>(e), radius);
   ImageEvent result = null;
   input = e;
   try {
     result = erodeFilter.read();
   } catch (StreamCorruptedException e1) {
     e1.printStackTrace();
   }
   notifyAllListener(result);
 }
示例#15
0
  public void run() {
    T input = null;
    try {
      do {

        input = readInput();
        if (input != null) {
          writeOutput(input);
        }

      } while (input != null);
      sendEndSignal();
    } catch (StreamCorruptedException e) {
      e.printStackTrace();
    }
  }
示例#16
0
  @Override
  @TargetDescriptor
  public void onImageEvent(ImageEvent imageEvent) {
    try {
      _lastImageEvent = imageEvent;

      OpeningFilter openingFilter = new OpeningFilter(new SupplierPipe<>(imageEvent), _kernel);

      ImageEvent result = openingFilter.read();

      notifyAllListeners(result);
    } catch (StreamCorruptedException e) {
      e.printStackTrace();
      notifyAllListeners(null);
    }
  }
示例#17
0
  @SuppressWarnings("unchecked")
  private Object getObject(String key, String description, Activity callingActivity) {
    FileInputStream file = null;
    ObjectInputStream input = null;
    try {
      file = callingActivity.openFileInput(key);
      input = new ObjectInputStream(file);
      if (key == COUNT) {
        return (MyCountDownTimerWrapper) input.readObject();
      } else if (key == LIST) {
        return (Stack<Exercise>) input.readObject();
      } else if (key == STACK) {
        return (Stack<Exercise>) input.readObject();
      } else if (key == KEYS) {
        return (ArrayList<String>) input.readObject();
      } else {
        return (ArrayList<Exercise>) input.readObject();
      }
    } catch (StreamCorruptedException e) {
      Log.e(MainMenuActivity.LOG_TAG, "Could not get " + description + " from file");
      e.printStackTrace();
    } catch (OptionalDataException e) {
      Log.e(MainMenuActivity.LOG_TAG, "Could not get " + description + " from file");
      e.printStackTrace();
    } catch (IOException e) {
      Log.e(MainMenuActivity.LOG_TAG, "Could not get " + description + " from file");
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      Log.e(MainMenuActivity.LOG_TAG, "Could not get " + description + " from file");
      e.printStackTrace();
    } finally {

      try {
        if (file != null && input != null) {
          file.close();
          input.close();
        }
      } catch (IOException e) {
        Log.e(
            MainMenuActivity.LOG_TAG,
            "Could not close files while getting " + description + " from file");
        e.printStackTrace();
      }
    }
    return null;
  }
 /** This method loads the fingerprint template if it exists. */
 private void loadEnrolledTemplateIfExists() {
   File enrolledFile = getFileStreamPath(ENROLL_FILENAME);
   if (enrolledFile.exists()) {
     try {
       FileInputStream enrollStream = openFileInput(ENROLL_FILENAME);
       ObjectInputStream ois = new ObjectInputStream(enrollStream);
       mEnrolledTemplate = (FingerprintTemplate) ois.readObject();
     } catch (FileNotFoundException e) {
       Log.e(TAG, e.getMessage());
     } catch (StreamCorruptedException e) {
       Log.e(TAG, e.getMessage());
     } catch (IOException e) {
       Log.e(TAG, e.getMessage());
     } catch (ClassNotFoundException e) {
       Log.e(TAG, e.getMessage());
     }
   }
 }
示例#19
0
  public static LocationSettings loadFromSavedFile(Context context) {
    LocationSettings loadedLocationSettings = null;

    try {
      FileInputStream fis = context.openFileInput(SAVE_FILE_NAME);
      ObjectInputStream ois = new ObjectInputStream(fis);
      loadedLocationSettings = (LocationSettings) ois.readObject();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (StreamCorruptedException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }

    return loadedLocationSettings;
  }
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   // user cancelled out of list
   if (data == null) {
     return;
   }
   switch (requestCode) {
     case LOAD_GAME_RESULT:
       String filename = data.getStringExtra("filename");
       try {
         ObjectInputStream ois = new ObjectInputStream(openFileInput(filename));
         Game newGame = (Game) ois.readObject();
         ois.close();
         if (newGame == null) {
           break;
         }
         this.game = newGame;
         Tossup currTossup = this.game.currTossup();
         this.updateBonusBoxes(currTossup);
         this.updatePointsSelector(currTossup);
         this.updateGlobalScore();
         this.updateTossupNum();
         this.updatePlayerSpinner();
       } catch (StreamCorruptedException e) {
         e.printStackTrace();
         throw new RuntimeException();
       } catch (FileNotFoundException e) {
         e.printStackTrace();
         throw new RuntimeException();
       } catch (IOException e) {
         e.printStackTrace();
         throw new RuntimeException();
       } catch (ClassNotFoundException e) {
         e.printStackTrace();
         throw new RuntimeException();
       }
       break;
     default:
       super.onActivityResult(requestCode, resultCode, data);
       break;
   }
 }
示例#21
0
  /*
   * Read a sample from the current position of the cursor. TODO: Needs to be
   * updated when fields update.
   */
  private Sample fillSample(Cursor cursor) {
    Sample s = null;

    byte[] sampleB = cursor.getBlob(cursor.getColumnIndex(CaratSampleDB.COLUMN_SAMPLE));
    if (sampleB != null) {
      ObjectInputStream oi;
      try {
        oi = new ObjectInputStream(new ByteArrayInputStream(sampleB));
        Object o = oi.readObject();
        if (o != null) s = SampleReader.readSample(o);
      } catch (StreamCorruptedException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      }
    }
    return s;
  }
示例#22
0
  @Override
  public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    context = getApplicationContext();
    shopCartManager = new ShopCartManager(getApplicationContext());
    shopCartList = new ArrayList<Object>();
    try {
      shopCartList = shopCartManager.readShopCart();
    } catch (StreamCorruptedException e) {
      shopCartManager.dele();
      e.printStackTrace();
    } catch (IOException e) {
      shopCartManager.dele();
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      shopCartManager.dele();
      e.printStackTrace();
    }
    /*
     * 初始化SharedPreferences
     */
    sp = getSharedPreferences("hrht", MODE_PRIVATE);
    /*
     * 初始化LayoutInflater
     */
    Inflater = LayoutInflater.from(getApplicationContext());
    ed = sp.edit();

    resources = getResources();
    /*
     * 初始化Volley框架的Http工具类
     */
    client = MyHttpClient.getInstance(MyApplication.this.getApplicationContext());
    /*
     * 实例化缓存管理器
     */
    mcCacheManager = new CacheManager(getApplicationContext());
    // city=new City();
  }
 private Codebook readCodebook(String speakerDirPath) {
   Codebook codebook = null;
   try {
     speakerDirPath += File.separator + Constants.CODEBOOK_FILE_NAME;
     // Log.d("Trying to read codebook from %s",
     // shortenPath(speakerDirPath));
     ObjectInputStream ois = new ObjectInputStream(new FileInputStream(speakerDirPath));
     codebook = (Codebook) ois.readObject();
   } catch (StreamCorruptedException e) {
     Log.e("Error reading codebook from path %s", speakerDirPath);
     e.printStackTrace();
   } catch (FileNotFoundException e) {
     Log.e("Error reading codebook from path %s", speakerDirPath);
     e.printStackTrace();
   } catch (IOException e) {
     Log.e("Error reading codebook from path %s", speakerDirPath);
     e.printStackTrace();
   } catch (ClassNotFoundException e) {
     Log.e("Error reading codebook from path %s", speakerDirPath);
     e.printStackTrace();
   }
   return codebook;
 }
示例#24
0
  public static void pullPipelineAandB() {

    PullPipe<LinkedList<String>> pipe1 = new PullPipe<>();
    PrintSink filter1 = new PrintSink(pipe1, outputPath);

    PullPipe<LinkedList<LineWithLineNumber>> pipe2 = new PullPipe<>();
    CreateIndexFilter filter2 = new CreateIndexFilter(pipe2);

    PullPipe<WordArray> pipe3 = new PullPipe<>();
    ShiftFilter filter3 = new ShiftFilter(pipe3);

    PullPipe<LineWithLineNumber> pipe4 = new PullPipe<>();
    LineToWordFilter filter4 = new LineToWordFilter(pipe4);

    PullPipe<WordTransfer> pipe5 = new PullPipe<>();
    // Allignment wird hier nicht durchgeführt, da der LineToWordFilter alle Leerzeichen wieder
    // entfernt
    WordToLineFilter filter5 = new WordToLineFilter(pipe5, len, align);

    PullPipe<CharTransfer> pipe6 = new PullPipe<>();
    CharToWordFilter filter6 = new CharToWordFilter(pipe6);

    CharacterSource filter7 = new CharacterSource(new File("").getAbsolutePath() + inputPath);

    pipe1.setPredecessorFilter(filter2);
    pipe2.setPredecessorFilter(filter3);
    pipe3.setPredecessorFilter(filter4);
    pipe4.setPredecessorFilter(filter5);
    pipe5.setPredecessorFilter(filter6);
    pipe6.setPredecessorFilter(filter7);

    try {
      filter1.read();
    } catch (StreamCorruptedException e) {
      e.printStackTrace();
    }
  }
示例#25
0
  public Object readObject(String key, Object o) {
    Object object = null;

    ByteArrayInputStream bis = null;
    ObjectInputStream ois;
    try {
      String value = getString(key, "");
      byte[] bytes = Base64.decode(value);

      if (bytes.length > 0) {
        // 封装到字节流
        bis = new ByteArrayInputStream(bytes);
        // 再次封装
        ois = new ObjectInputStream(bis);
        // 读取对象
        object = ois.readObject();
      } else {
        object = o;
      }
    } catch (EOFException e) {
      e.printStackTrace();
    } catch (StreamCorruptedException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } finally {
      try {
        if (bis != null) bis.close();
        if (bis != null) bis.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return object;
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_workout);
    Workout loadedWorkout = null;

    try {
      FileInputStream fis = openFileInput("WorkoutPage");
      ObjectInputStream is = new ObjectInputStream(fis);
      loadedWorkout = (Workout) is.readObject();
      is.close();
      fis.close();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (OptionalDataException e) {
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (StreamCorruptedException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    String s = loadedWorkout.getName();
    setTitle(s);

    /*
    Intent intent = getIntent();
    String message = intent.getStringExtra("asdf");
    name = message;
    //setTitle(name);
    */

  }
  /**
   * recursion happens here -- for each subOrgName subordinate, a new servlet in the target agent
   * will be executed
   */
  protected HierarchyData fetchForSubordinate(
      HttpServletRequest request,
      SimpleServletSupport support,
      String subOrgName,
      boolean allRelationships,
      Set visitedOrgs) {
    HierarchyData hd = null;
    InputStream is = null;
    ObjectInputStream ois = null;

    try {
      // build URL for remote connection
      StringBuffer buf = new StringBuffer();
      buf.append("http://");
      buf.append(request.getServerName());
      buf.append(":");
      buf.append(request.getServerPort());
      buf.append("/$");
      buf.append(subOrgName);
      buf.append(support.getPath());
      buf.append("?data=true&recurse=true&visitedOrgs=");
      for (Iterator iter = visitedOrgs.iterator(); iter.hasNext(); ) {
        buf.append(iter.next());
        if (iter.hasNext()) buf.append(",");
      }
      if (allRelationships) buf.append("&allRelationships=true");

      String url = buf.toString();

      if (VERBOSE) {
        System.out.println(
            "At "
                + new Date()
                + " - in "
                + support.getAgentIdentifier()
                + ", fetch hierarchy from "
                + subOrgName
                + ", URL:\n"
                + url);
      }

      // open connection
      URL myURL = new URL(url);
      URLConnection myConnection = myURL.openConnection();
      is = myConnection.getInputStream();
      ois = new ObjectInputStream(is);

      // read single HierarchyData Object from subordinate
      hd = (HierarchyData) ois.readObject();

      if (VERBOSE) {
        System.out.println(
            "In "
                + support.getAgentIdentifier()
                + ", got "
                + ((hd != null) ? ("hierarchy[" + hd.numOrgs() + "]") : ("null"))
                + " from "
                + subOrgName);
      }

    } catch (StreamCorruptedException sce) {
      if (VERBOSE) {
        System.err.println("In " + support.getAgentIdentifier() + ", got exception : ");
        sce.printStackTrace();
      }
    } catch (FileNotFoundException fnf) {
      if (!allRelationships || true) {
        System.err.println("In " + support.getAgentIdentifier() + ", got exception : ");
        fnf.printStackTrace();
      }
    } catch (Exception e) {
      System.err.println("In " + support.getAgentIdentifier() + ", got exception : ");
      e.printStackTrace();
    } finally {
      try {
        if (ois != null) ois.close();
        if (is != null) is.close();
      } catch (Exception e) {
      }
    }

    return hd;
  }
示例#28
0
  public <T> T get(String path, Class<T> c) {
    Log.d(TAG, "get " + path); // NON-NLS

    File f = new File(getFullCachePath(path));
    if (!f.exists()) {
      Log.w(TAG, "file: " + f.toString() + " not exists"); // NON-NLS
      return null;
    }

    T object;
    try {
      StringBuffer fileData = new StringBuffer();
      BufferedReader reader = new BufferedReader(new FileReader(f));
      char[] buf = new char[1024];
      int numRead = 0;
      while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
      }
      reader.close();
      String json = fileData.toString();
      Log.d(TAG, "cache: " + json); // NON-NLS

      object = getGson().fromJson(json, c);
    } catch (FileNotFoundException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (StreamCorruptedException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (IOException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (JsonSyntaxException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (IllegalArgumentException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (RuntimeException e) {
      // не позволим кэшу убить нашу программу
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (Exception e) {
      // не позволим кэшу убить нашу программу
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    }

    if (object.getClass().toString().equals(c.toString())) return object;
    else return null;
  }
示例#29
0
  /**
   * @param path
   * @param t
   * @return
   */
  public List<?> getList(String path, java.lang.reflect.Type t) {
    Log.d(TAG, "get list " + path); // NON-NLS

    File f = new File(getFullCachePath(path));
    if (!f.exists()) {
      Log.w(TAG, "file: " + f.toString() + " not exists"); // NON-NLS
      return null;
    }

    List<?> list;
    try {
      StringBuffer fileData = new StringBuffer();
      BufferedReader reader = new BufferedReader(new FileReader(f));
      char[] buf = new char[1024];
      int numRead = 0;
      while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
      }
      reader.close();
      String json = fileData.toString();
      Log.d(TAG, "cache: " + json); // NON-NLS

      list = gson.fromJson(json, t);
    } catch (FileNotFoundException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (StreamCorruptedException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (IOException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (JsonSyntaxException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (IllegalArgumentException e) {
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (RuntimeException e) {
      // не позволим кэшу убить нашу программу
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    } catch (Exception e) {
      // не позволим кэшу убить нашу программу
      Log.w(TAG, e.getMessage());
      e.printStackTrace();
      absoluteDelete(path);
      return null;
    }

    return list;
  }