def buildLarchJar(outputStream, additionalFilesAsNameBytesPairs, filterFn=None, larchJarURL=None): """ Build a JAR from an existing Larch JAR, along with additional files to make a packaged program :param outputStream: The output stream to which the JAR is to be written :param additionalFilesAsNameBytesPairs: Additional files in the form of a sequence of tuples consisting of (path, bytes) :param filterFn: (optional) A filter function that can be used to exclude files from the existing Larch JAR; takes the form of function(name) -> boolean, should return True if file should be included :param larchJarURL: (optional) A URL at which the existing Larch JAR can be obtained. If None, it will use the JAR from which Larch was started. Raises an error if no URL provided and Larch was not started from a JAR """ if larchJarURL is None: larchJarURL = _larchJarURL if larchJarURL is None: raise RuntimeError, 'Larch was not loaded from a JAR file and no Larch JAR file was provided' jarIn = JarInputStream(larchJarURL.openStream()) manifestIn = jarIn.getManifest() manifestOut = Manifest(manifestIn) jarOut = JarOutputStream(outputStream, manifestOut) bytesBuffer = jarray.zeros(_BYTES_BUFFER_SIZE, 'b') entryIn = jarIn.getNextJarEntry() while entryIn is not None: name = entryIn.getName() if filterFn is None or filterFn(name): bufferStream = ByteArrayOutputStream() while True: bytesRead = jarIn.read(bytesBuffer, 0, _BYTES_BUFFER_SIZE) if bytesRead == -1: break bufferStream.write(bytesBuffer, 0, bytesRead) entryOut = ZipEntry(name) entryOut.setSize(bufferStream.size()) jarOut.putNextEntry(entryOut) bufferStream.writeTo(jarOut) jarOut.closeEntry() entryIn = jarIn.getNextJarEntry() for name, bytes in additionalFilesAsNameBytesPairs: size = len(bytes) entryOut = ZipEntry(name) entryOut.setSize(size) jarOut.putNextEntry(entryOut) jarOut.write(bytes, 0, size) jarOut.closeEntry() jarOut.finish()
def getStreamData(stream): baos = ByteArrayOutputStream() lenRead = 0 while lenRead >= 0: bytes = jarray.zeros(4096, 'b') lenRead = stream.read(bytes, 0, 4096) if lenRead > 0: baos.write(bytes, 0, lenRead) stream.close(); return baos.toString()
def getFileBytes(path): baos = ByteArrayOutputStream() fis = FileInputStream(File(path)) bytes = jarray.zeros(1024, 'b') while 1 == 1: readSize = fis.read(bytes) if (readSize == -1): break baos.write(bytes, 0, readSize) return baos.toByteArray()
def readBinaryFile(cls, rootFile): """ generated source for method readBinaryFile """ in_ = FileInputStream(rootFile) out = ByteArrayOutputStream() # Transfer bytes from in to out buf = [None]*1024 while in_.read(buf) > 0: out.write(buf) in_.close() return out.toByteArray()
class StringOutputStream(OutputStream): def __init__(self): self.stream = ByteArrayOutputStream() def write(self, b, off, len): self.stream.write(b, off, len) def get_string(self): output = String(self.stream.toByteArray()) if self.stream is not None: self.stream.close() return output
def buildRequest(self, payload): """ This is the main method through which an extension interacts with a IScannerInsertionPoint instance. They provide the payload through the payload parameter and we replace it in our request. If the parameter type is something that could be handled by Burp's helpers we update it in that way, otherwise we do it by modifying the byte arrays directly. Args: payload: the active scanner payload provided by the extension. """ start = self.start end = self.end if self.type == IScannerInsertionPoint.INS_PARAM_JSON: start, end, payload = self.encodeJson(start, end, payload) elif self.type == IScannerInsertionPoint.INS_HEADER: pass elif self.type in [IScannerInsertionPoint.INS_PARAM_XML, IScannerInsertionPoint.INS_PARAM_XML_ATTR]: start, end, payload = self.encodeXml(start, end, payload) else: start, end, payload = self.encodeUrl(start, end, payload) stream = ByteArrayOutputStream() stream.write(self.request[0:start]) stream.write(payload) stream.write(self.request[end:]) newRequestBytes = self.updateContentLength(stream.toByteArray()) return newRequestBytes
#print folder.compressionToString() self.cabfile.newFolder(folder) def getName(self, name): return string.join(string.split(name, '.'), '\\')+'.class' def write(self, name, file): fname = self.getName(name) entry = cab.CabFileEntry(name=fname, date=Date()) if isinstance(file, ByteArrayOutputStream): file = ByteArrayInputStream(file.toByteArray()) elif isinstance(file, type('')): file = FileInputStream(file) self.cabfile.addStream(file, entry) def close(self): self.cabfile.complete() except AttributeError: pass if __name__ == '__main__': for of in [CabOutput('c:\\jython\\test.cab')]: #DirectoryOutput('c:\\jython\\dtest'), ZipOutput('c:\\jython\\test.jar')]: of.write('org.python.core.PyInteger', 'c:\\jython\\JavaCode\\org\\python\\core\\PyInteger.class') of.write('org.python.core.PyFloat', 'c:\\jython\\JavaCode\\org\\python\\core\\PyFloat.class') bytes = ByteArrayOutputStream() bytes.write(jarray.array([10]*500, 'b')) of.write('hi.there', bytes) of.close()
def getName(self, name): return string.join(string.split(name, '.'), '\\') + '.class' def write(self, name, file): fname = self.getName(name) entry = cab.CabFileEntry(name=fname, date=Date()) if isinstance(file, ByteArrayOutputStream): file = ByteArrayInputStream(file.toByteArray()) elif isinstance(file, type('')): file = FileInputStream(file) self.cabfile.addStream(file, entry) def close(self): self.cabfile.complete() except AttributeError: pass if __name__ == '__main__': for of in [ CabOutput('c:\\jython\\test.cab') ]: #DirectoryOutput('c:\\jython\\dtest'), ZipOutput('c:\\jython\\test.jar')]: of.write('org.python.core.PyInteger', 'c:\\jython\\JavaCode\\org\\python\\core\\PyInteger.class') of.write('org.python.core.PyFloat', 'c:\\jython\\JavaCode\\org\\python\\core\\PyFloat.class') bytes = ByteArrayOutputStream() bytes.write(jarray.array([10] * 500, 'b')) of.write('hi.there', bytes) of.close()