Esempio n. 1
0
 /**
  * A static convience method for decoding an object from a String.
  *
  * <p>All exceptions are logged using LogMgr internally and then rethrown as GlueException with
  * the same message as written to the log.
  *
  * @param title The name to be given to the object when decoded.
  * @param bytes The Glue formated data to be decoded.
  * @throws GlueException If unable to decode the string.
  */
 public static Object decodeBytes(String title, byte bytes[]) throws GlueException {
   try {
     GlueDecoderImpl gd = new GlueDecoderImpl();
     InputStream in = null;
     try {
       in = new ByteArrayInputStream(bytes);
       GlueParser parser = new GlueParser(in);
       return parser.Decode(gd, gd.getState());
     } catch (ParseException ex) {
       throw new GlueException(ex);
     } catch (TokenMgrError ex) {
       throw new GlueException(ex);
     } finally {
       in.close();
     }
   } catch (GlueException ex) {
     String msg = ("Unable to Glue decode: " + title + "\n" + "  " + ex.getMessage());
     LogMgr.getInstance().log(LogMgr.Kind.Glu, LogMgr.Level.Severe, msg);
     throw new GlueException(msg);
   } catch (Exception ex) {
     String msg = Exceptions.getFullMessage("INTERNAL ERROR:", ex, true, true);
     LogMgr.getInstance().log(LogMgr.Kind.Glu, LogMgr.Level.Severe, msg);
     throw new GlueException(msg);
   }
 }
Esempio n. 2
0
  /**
   * A static convience method for decoding an object from the given file.
   *
   * <p>All exceptions are logged using LogMgr internally and then rethrown as GlueException with
   * the same message as written to the log.
   *
   * @param title The name to be given to the object when decoded.
   * @param file The Glue format file to be decoded.
   * @throws GlueException If unable to decode the string.
   */
  public static Object decodeFile(String title, File file) throws GlueException {
    LogMgr.getInstance()
        .log(LogMgr.Kind.Glu, LogMgr.Level.Finest, "Reading " + title + ": " + file);

    try {
      InputStream in = null;
      try {
        in = new BufferedInputStream(new FileInputStream(file));
      } catch (IOException ex) {
        String msg =
            ("I/O ERROR: \n"
                + "  Unable to open file ("
                + file
                + ") to decode: "
                + title
                + "\n"
                + "    "
                + ex.getMessage());
        LogMgr.getInstance().log(LogMgr.Kind.Glu, LogMgr.Level.Severe, msg);
        throw new GlueException(msg);
      }

      try {
        GlueDecoderImpl gd = new GlueDecoderImpl();
        GlueParser parser = new GlueParser(in, "UTF-8");
        return parser.Decode(gd, gd.getState());
      } catch (ParseException ex) {
        throw new GlueException(ex);
      } catch (TokenMgrError ex) {
        throw new GlueException(ex);
      } finally {
        in.close();
      }
    } catch (IOException ex) {
      String msg =
          ("I/O ERROR: \n"
              + "  While reading from file ("
              + file
              + ") during Glue decoding of: "
              + title
              + "\n"
              + "    "
              + ex.getMessage());
      LogMgr.getInstance().log(LogMgr.Kind.Glu, LogMgr.Level.Severe, msg);
      throw new GlueException(msg);
    } catch (GlueException ex) {
      String msg =
          ("While reading from file ("
              + file
              + "), unable to Glue decode: "
              + title
              + "\n"
              + "  "
              + ex.getMessage());
      LogMgr.getInstance().log(LogMgr.Kind.Glu, LogMgr.Level.Severe, msg);
      throw new GlueException(msg);
    } catch (Exception ex) {
      String msg = Exceptions.getFullMessage("INTERNAL ERROR:", ex, true, true);
      LogMgr.getInstance().log(LogMgr.Kind.Glu, LogMgr.Level.Severe, msg);
      throw new GlueException(msg);
    }
  }