/**
  * Serializes the passed object to a byte array, returning a zero byte array if the passed object
  * is null, or fails serialization
  *
  * @param arg The object to serialize
  * @return the serialized object
  */
 public static byte[] serializeNoCompress(Object arg) {
   if (arg == null) return new byte[] {};
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   ObjectOutputStream oos = null;
   try {
     oos = new ObjectOutputStream(baos);
     oos.writeObject(arg);
     oos.flush();
     baos.flush();
     return baos.toByteArray();
   } catch (Exception ex) {
     return new byte[] {};
   } finally {
     try {
       baos.close();
     } catch (Exception ex) {
       /* No Op */
     }
     if (oos != null)
       try {
         oos.close();
       } catch (Exception ex) {
         /* No Op */
       }
   }
 }
예제 #2
0
파일: Q2.java 프로젝트: rlishtaba/jPOS
  protected Document encrypt(Document doc) throws GeneralSecurityException, IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(os);
    XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
    out.output(doc, writer);
    writer.close();

    byte[] crypt = dodes(os.toByteArray(), Cipher.ENCRYPT_MODE);

    Document secureDoc = new Document();
    Element root = new Element(PROTECTED_QBEAN);
    secureDoc.setRootElement(root);
    Element secureData = new Element("data");
    root.addContent(secureData);

    secureData.setText(ISOUtil.hexString(crypt));
    return secureDoc;
  }
 /**
  * Serializes an array of invocation arguments to a byte array
  *
  * @param args The arguments to marshall
  * @return a byte array
  */
 public static byte[] getOutput(Object... args) {
   if (args == null || args.length == 0) return new byte[] {};
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   GZIPOutputStream gzos = null;
   ObjectOutputStream oos = null;
   try {
     gzos = new GZIPOutputStream(baos);
     oos = new ObjectOutputStream(gzos);
     oos.writeObject(args);
     oos.flush();
     gzos.finish();
     gzos.flush();
     baos.flush();
     return baos.toByteArray();
   } catch (Exception ex) {
     throw new RuntimeException(
         "Failed to encode MBeanServerConnection Invocation arguments " + Arrays.toString(args),
         ex);
   } finally {
     try {
       baos.close();
     } catch (Exception ex) {
       /* No Op */
     }
     if (oos != null)
       try {
         oos.close();
       } catch (Exception ex) {
         /* No Op */
       }
     if (gzos != null)
       try {
         gzos.close();
       } catch (Exception ex) {
         /* No Op */
       }
   }
 }