public virtual void TestExistingManifest() { FilePath dir = new FilePath(Runtime.GetProperty("test.build.dir", "target/test-dir" ), typeof(TestJarFinder).FullName + "-testExistingManifest"); Delete(dir); dir.Mkdirs(); FilePath metaInfDir = new FilePath(dir, "META-INF"); metaInfDir.Mkdirs(); FilePath manifestFile = new FilePath(metaInfDir, "MANIFEST.MF"); Manifest manifest = new Manifest(); OutputStream os = new FileOutputStream(manifestFile); manifest.Write(os); os.Close(); FilePath propsFile = new FilePath(dir, "props.properties"); TextWriter writer = new FileWriter(propsFile); new Properties().Store(writer, string.Empty); writer.Close(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); JarOutputStream zos = new JarOutputStream(baos); JarFinder.JarDir(dir, string.Empty, zos); JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.ToByteArray ())); NUnit.Framework.Assert.IsNotNull(jis.GetManifest()); jis.Close(); }
/// <exception cref="System.IO.IOException"/> /// <exception cref="Sharpen.URISyntaxException"/> internal static LocalResource CreateJarFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis) { byte[] bytes = new byte[len]; r.NextBytes(bytes); FilePath archiveFile = new FilePath(p.ToUri().GetPath() + ".jar"); archiveFile.CreateNewFile(); JarOutputStream @out = new JarOutputStream(new FileOutputStream(archiveFile)); @out.PutNextEntry(new JarEntry(p.GetName())); @out.Write(bytes); @out.CloseEntry(); @out.Close(); LocalResource ret = recordFactory.NewRecordInstance <LocalResource>(); ret.SetResource(ConverterUtils.GetYarnUrlFromPath(new Path(p.ToString() + ".jar") )); ret.SetSize(len); ret.SetType(LocalResourceType.Archive); ret.SetVisibility(vis); ret.SetTimestamp(files.GetFileStatus(new Path(p.ToString() + ".jar")).GetModificationTime ()); return(ret); }
/// <exception cref="System.IO.FileNotFoundException"/> /// <exception cref="System.IO.IOException"/> private void CreateAndAddJarToJar(JarOutputStream jos, FilePath jarFile) { FileOutputStream fos2 = new FileOutputStream(jarFile); JarOutputStream jos2 = new JarOutputStream(fos2); // Have to have at least one entry or it will complain ZipEntry ze = new ZipEntry("lib1.inside"); jos2.PutNextEntry(ze); jos2.CloseEntry(); jos2.Close(); ze = new ZipEntry("lib/" + jarFile.GetName()); jos.PutNextEntry(ze); FileInputStream @in = new FileInputStream(jarFile); byte[] buf = new byte[1024]; int numRead; do { numRead = @in.Read(buf); if (numRead >= 0) { jos.Write(buf, 0, numRead); } }while (numRead != -1); @in.Close(); jos.CloseEntry(); jarFile.Delete(); }
private static void Write(string name, byte[] value, JarOutputStream target) { name = name.Replace(".", "/") + ".class"; JarEntry entry = new JarEntry(name); entry.Time = System.CurrentTimeMillis(); target.PutNextEntry(entry); target.Write(value, 0, value.Length); target.CloseEntry(); }
/// <summary> /// Construct a jar with two files in it in our /// test dir. /// </summary> /// <exception cref="System.IO.IOException"/> private void MakeTestJar() { FilePath jarFile = new FilePath(TestRootDir, TestJarName); JarOutputStream jstream = new JarOutputStream(new FileOutputStream(jarFile)); jstream.PutNextEntry(new ZipEntry("foobar.txt")); jstream.CloseEntry(); jstream.PutNextEntry(new ZipEntry("foobaz.txt")); jstream.CloseEntry(); jstream.Close(); }
/// <exception cref="System.IO.FileNotFoundException"/> /// <exception cref="System.IO.IOException"/> private Path MakeJar(Path p, int index) { FileOutputStream fos = new FileOutputStream(new FilePath(p.ToString())); JarOutputStream jos = new JarOutputStream(fos); ZipEntry ze = new ZipEntry("distributed.jar.inside" + index); jos.PutNextEntry(ze); jos.Write(Sharpen.Runtime.GetBytesForString(("inside the jar!" + index))); jos.CloseEntry(); jos.Close(); return(p); }
/// <exception cref="System.IO.IOException"/> private FilePath MakeTestJar() { FilePath jarFile = new FilePath(testDir, "test.jar"); JarOutputStream @out = new JarOutputStream(new FileOutputStream(jarFile)); ZipEntry entry = new ZipEntry("resource.txt"); @out.PutNextEntry(entry); @out.Write(Runtime.GetBytesForString("hello")); @out.CloseEntry(); @out.Close(); return(jarFile); }
/// <exception cref="System.IO.FileNotFoundException"/> /// <exception cref="System.IO.IOException"/> private Path MakeJobJarWithLib(string testDir) { Path jobJarPath = new Path(testDir, "thejob.jar"); FileOutputStream fos = new FileOutputStream(new FilePath(jobJarPath.ToUri().GetPath ())); JarOutputStream jos = new JarOutputStream(fos); // Have to put in real jar files or it will complain CreateAndAddJarToJar(jos, new FilePath(new Path(testDir, "lib1.jar").ToUri().GetPath ())); CreateAndAddJarToJar(jos, new FilePath(new Path(testDir, "lib2.jar").ToUri().GetPath ())); jos.Close(); localFs.SetPermission(jobJarPath, new FsPermission("700")); return(jobJarPath); }
/// <exception cref="System.IO.IOException"/> private static void CreateJar(FilePath dir, FilePath jarFile) { Preconditions.CheckNotNull(dir, "dir"); Preconditions.CheckNotNull(jarFile, "jarFile"); FilePath jarDir = jarFile.GetParentFile(); if (!jarDir.Exists()) { if (!jarDir.Mkdirs()) { throw new IOException(MessageFormat.Format("could not create dir [{0}]", jarDir)); } } JarOutputStream zos = new JarOutputStream(new FileOutputStream(jarFile)); JarDir(dir, string.Empty, zos); }
/// <summary> /// Write the compiled to a jar file. Overwrites the existing jar file. /// </summary> /// <param name="compiled">compiled</param> /// <param name="file">the target file</param> /// <throws>IOException when the write failed</throws> public static void Write(EPCompiled compiled, FileInfo file) { Manifest manifest = new Manifest(); manifest.MainAttributes.Put(Attributes.Name.MANIFEST_VERSION, "1.0"); manifest.MainAttributes.Put(new Attributes.Name(MANIFEST_COMPILER_VERSION), compiled.Manifest.CompilerVersion); manifest.MainAttributes.Put(new Attributes.Name(MANIFEST_MODULEPROVIDERCLASSNAME), compiled.Manifest.ModuleProviderClassName); manifest.MainAttributes.Put(new Attributes.Name(MANIFEST_QUERYPROVIDERCLASSNAME), compiled.Manifest.QueryProviderClassName); JarOutputStream target = new JarOutputStream(new FileOutputStream(file), manifest); try { foreach (KeyValuePair<string, byte[]> entry in compiled.Classes) { Write(entry.Key, entry.Value, target); } } finally { target.Close(); } }
/// <exception cref="System.IO.IOException"/> private FilePath MakeTestJar() { FilePath jarFile = new FilePath(TestRootDir, TestJarName); JarOutputStream jstream = new JarOutputStream(new FileOutputStream(jarFile)); InputStream entryInputStream = this.GetType().GetResourceAsStream(ClassName); ZipEntry entry = new ZipEntry("org/apache/hadoop/util/" + ClassName); jstream.PutNextEntry(entry); BufferedInputStream bufInputStream = new BufferedInputStream(entryInputStream, 2048 ); int count; byte[] data = new byte[2048]; while ((count = bufInputStream.Read(data, 0, 2048)) != -1) { jstream.Write(data, 0, count); } jstream.CloseEntry(); jstream.Close(); return(jarFile); }
/// <exception cref="System.IO.IOException"/> internal static LocalResource CreateJar(FileContext files, Path p, LocalResourceVisibility vis) { Log.Info("Create jar file " + p); FilePath jarFile = new FilePath((files.MakeQualified(p)).ToUri()); FileOutputStream stream = new FileOutputStream(jarFile); Log.Info("Create jar out stream "); JarOutputStream @out = new JarOutputStream(stream, new Manifest()); Log.Info("Done writing jar stream "); @out.Close(); LocalResource ret = recordFactory.NewRecordInstance <LocalResource>(); ret.SetResource(ConverterUtils.GetYarnUrlFromPath(p)); FileStatus status = files.GetFileStatus(p); ret.SetSize(status.GetLen()); ret.SetTimestamp(status.GetModificationTime()); ret.SetType(LocalResourceType.Pattern); ret.SetVisibility(vis); ret.SetPattern("classes/.*"); return(ret); }
// it should not throw an exception /// <exception cref="System.IO.IOException"/> private FilePath MakeClassLoaderTestJar(params string[] clsNames) { FilePath jarFile = new FilePath(TestRootDir, TestJar2Name); JarOutputStream jstream = new JarOutputStream(new FileOutputStream(jarFile)); foreach (string clsName in clsNames) { string name = clsName.Replace('.', '/') + ".class"; InputStream entryInputStream = this.GetType().GetResourceAsStream("/" + name); ZipEntry entry = new ZipEntry(name); jstream.PutNextEntry(entry); BufferedInputStream bufInputStream = new BufferedInputStream(entryInputStream, 2048 ); int count; byte[] data = new byte[2048]; while ((count = bufInputStream.Read(data, 0, 2048)) != -1) { jstream.Write(data, 0, count); } jstream.CloseEntry(); } jstream.Close(); return(jarFile); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public void generate(java.util.List<Node> instructions, String outputfile) throws java.io.IOException public virtual void generate(IList <Node> instructions, string outputfile) { ClassWriter cw = new ClassWriter(0); cw.visit(V1_1, ACC_PUBLIC, outputfile, null, "java/lang/Object", null); // creates a MethodWriter for the (implicit) constructor MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); // pushes the 'this' variable mw.visitVarInsn(ALOAD, 0); // invokes the super class constructor mw.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mw.visitInsn(RETURN); // this code uses a maximum of one stack element and one local variable mw.visitMaxs(1, 1); mw.visitEnd(); // creates a MethodWriter for the 'main' method mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); // pushes the 'out' field (of type PrintStream) of the System class mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); // pushes the "Hello Juliar Future" String constant mw.visitLdcInsn("Now Calling generated Juliar Methods!"); // invokes the 'println' method (defined in the PrintStream class) mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); //mw.visitInsn(RETURN); // this code uses a maximum of two stack elements and two local // variables //mw.visitVarInsn(ALOAD,0); mw.visitMethodInsn(INVOKESTATIC, outputfile, "juliarMethod", "()V", false); mw.visitInsn(RETURN); mw.visitMaxs(2, 2); mw.visitEnd(); mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "juliarMethod", "()V", null, null); int?stackSize = 0; GeneratorAdapter ga = new GeneratorAdapter(mw, ACC_PUBLIC + ACC_STATIC, "juliarMethod", "()V"); evaluateExpressions(instructions, mw, ga, stackSize); mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mw.visitLdcInsn("Instructions:" + instructions); mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); mw.visitInsn(RETURN); mw.visitMaxs(16, 6); mw.visitEnd(); /*MethodVisitor foo = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "foo", "()V", null, null); * * GeneratorAdapter ga1 = new GeneratorAdapter(foo, ACC_PUBLIC + ACC_STATIC, "foo", "()V"); * foo.visitInsn(RETURN); * foo.visitEnd();*/ // gets the bytecode of the Example class, and loads it dynamically sbyte[] code = cw.toByteArray(); //Create JAR output FileOutputStream fout = new FileOutputStream(outputfile + ".jar"); Manifest manifest = new Manifest(); manifest.MainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); manifest.MainAttributes.put(Attributes.Name.MAIN_CLASS, outputfile); JarOutputStream jarOut = new JarOutputStream(fout, manifest); try { jarOut.putNextEntry(new ZipEntry("com/juliar/pal/Primitives.class")); InputStream primitiveStream = typeof(Primitives).getResourceAsStream("Primitives.class"); jarOut.write(getBytes(primitiveStream)); jarOut.closeEntry(); jarOut.putNextEntry(new ZipEntry(outputfile + ".class")); jarOut.write(code); jarOut.closeEntry(); } catch (Exception e) { JuliarLogger.log(e); } jarOut.close(); fout.close(); /* * List<String> Dependencies = SomeClass.getDependencies(); * FileOutputStream fout = new FileOutputStream(outputfile+".jar"); * * JarOutputStream jarOut = new JarOutputStream(fout); * * //jarOut.putNextEntry(new ZipEntry("com/juliar/pal")); // Folders must end with "/". * //jarOut.putNextEntry(new ZipEntry("com/juliar/pal/Primitives.class")); * //jarOut.write(getBytes("com/juliar/pal/Primitives.class")); * //jarOut.closeEntry(); * * jarOut.putNextEntry(new ZipEntry(outputfile+".class")); * jarOut.write(getBytes(outputfile+".class")); * jarOut.closeEntry(); * * for(String dependency : Dependencies){ * int index = dependency.lastIndexOf( '/' ); * jarOut.putNextEntry(new ZipEntry(dependency.substring( index ))); // Folders must end with "/". * jarOut.putNextEntry(new ZipEntry(dependency)); * jarOut.write(getBytes(dependency)); * jarOut.closeEntry(); * } * * * jarOut.close(); * * fout.close();*/ }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: public java.net.URL createJarFor(java.io.File f, Class... classesToInclude) throws java.io.IOException public virtual URL CreateJarFor(File f, params Type[] classesToInclude) { using (FileStream fout = new FileStream(f, FileMode.Create, FileAccess.Write), JarOutputStream jarOut = new JarOutputStream(fout)) { foreach (Type target in classesToInclude) { //JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method: string fileName = target.FullName.Replace(".", "/") + ".class"; jarOut.putNextEntry(new ZipEntry(fileName)); jarOut.write(ClassCompiledBytes(fileName)); jarOut.closeEntry(); } } return(f.toURI().toURL()); }