codesimian
Class Jars

java.lang.Object
  extended by codesimian.Zips
      extended by codesimian.Jars

public class Jars
extends Zips

9/07 This class is not finished. Later it will contain static functions for using, combining, breeding, and evolving JAR files.

Jars.findSelf(boolean) usually works, but sometimes not when you're not in an applet for example, and often has problems finding CodeSimian.jar in non-Windows operating-systems (has not been tested much). Needs to be fixed asap. Finding self is the most reliable way of getting the bytes of the program's internal files (jar is zip. it has internal files).

For example, find 1 CodeSimian.jar that contains a few different files from this program (an executing CodeSimian.jar) or find 2 different CodeSimian.jar's, and combine some of their different files to create a new CodeSimian.jar with different behaviors. Each software is 1 file (CodeSimian4.jar CodeSimian556.jar etc) and can modify other similar softwares.

NEED TO BUILD: a way to create and execute new JAR files IN MEMORY ONLY for if a hard-drive is not available like in an APPLET in a webpage.


Nested Class Summary
static class Jars.JarTest
           
static class Jars.SaveUpdatedSelf
          saves a new CodeSimian.jar somewhere.
static class Jars.ZipEntryAndByteArray
           
 
Method Summary
static java.io.ByteArrayInputStream breed(java.io.ByteArrayInputStream[] jars)
          Breeds 2 or more modified CodeSimian.jar's into a single CodeSimian.jar which is returned
static java.io.ByteArrayInputStream consumeAnotherProgram(java.io.ByteArrayInputStream anyOtherJarProgram)
          any JAR file contains classes and maybe other files that CodeSimian might consume by copying it into a new CodeSimian.jar

This class should search for a license text file and license at top of source-code files and announce this info to a LicenseListener object (LicenseListener interface not yet created 9/06).
static java.io.ByteArrayInputStream consumeAnyBytes(java.lang.String nameOrDescription, java.io.InputStream anything)
          Creates a new JAR file (a new version of the currently-executing CodeSimian.jar) after consuming some bytes.
 int consumeFilesInJar(java.io.ByteArrayInputStream bytesOfJarFile, java.lang.String putInThisInternalFolder, CS fileNameJudge, CS fileBytesJudge, CS booleanOverwriteP0WithP1)
          if putInThisInternalFolder is null, use the files' existing folder plus some prefix.
 int consumeFilesInJar(java.io.ByteArrayInputStream bytesOfJarFile, java.lang.String putInThisInternalFolder, java.lang.String fileNameEnd)
          example: consumeFilesInJar(bytesOfSomeJar, "cs/", ".cs")
static java.io.ByteArrayInputStream consumeSimilar(java.io.ByteArrayInputStream similarToMe, int minBytesToConsume, int maxBytesToConsume)
          Modifies this program executing now and saves it in a new CodeSimian.jar.
static java.io.ByteArrayInputStream[] findAnythingOnTheInternet(CS byteJudger, java.net.URL[] startSearchingHere, int howManyFilesDoYouWant, long maxMilliseconds, long minBytesEach, long maxBytesEach)
           
static java.io.ByteArrayInputStream[] findJarsOnTheInternet(CS jarJudger, java.net.URL[] startSearchingHere, int howManyJarsDoYouWant, long maxMilliseconds, long minBytesEachJar, long maxBytesEachJar)
          Uses jarJudger.setL(ByteArrayInputStream) then executes jarJudger.D() to judge JARs it finds on the internet, and based on how jarJudger judges, tries to find better JARs on the internet next time you call this function with the same (or similar?) jarJudger object.
static java.lang.String[] findLicenses(java.io.ByteArrayInputStream anyJar)
          should return strings like "GNU GPL" "GNU LGPL" "public domain" "new BSD" "License of Guile" etc, or whichever licenses the input program allows (can choose any, not required to use all).
static java.io.ByteArrayInputStream findSelf(boolean createApproximateSelfIfCanOnlyFindPartsOfSelf)
          returns the CodeSimian.jar that contains this program executing now, or null if could not find it.
static byte[] findSelfBytes(boolean createApproximateSelfIfCanOnlyFindPartsOfSelf)
           
static java.io.ByteArrayInputStream[] findSimilarToSelf()
          returns Jar files it thinks are most similar to the CodeSimian.jar currently executing.
static byte[] getUpdatedSelfBytes()
           
static java.io.ByteArrayInputStream getUpdatedSelfInputStream()
          returns the bytes for a new CodeSimian.jar, including all files in the original CodeSimian.jar (returned by findSelf()), except for files deleted by deleteInternalFile and files added or modified by setInternalFile (in InnerFiles.java).
static float GPLcompatible(java.io.ByteArrayInputStream anyJar)
          is a JAR file compatible with the GNU GPL license? All LGPL, new BSD, and many other licenses are GPL-compatible.
static float LGPLcompatible(java.io.ByteArrayInputStream anyJar)
          is a JAR file compatible with the GNU LGPL license? Unlike GPL, LGPL allows you to keep your code secret while distributing the executable.
static byte[] readBytesForCurrentEntry(java.util.zip.ZipEntry currentEntry, java.util.zip.ZipInputStream jarIn)
          jarIn.getNextJarEntry() must have returned currentEntry the last time it was called, because that positions the ByteArrayInputStream to the beginning of the entry.
static void suicide(Liquid liquidJarSuicide)
          This Jar file (like CodeSimian.jar) is the Java program that is running now.
 
Methods inherited from class codesimian.Zips
get1FileFromZip, getFileNames, unzip, zipCsArray, zipFileOrFolder, zipFileOrFolder, zipPathNameSaveToPathName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

findSelf

public static java.io.ByteArrayInputStream findSelf(boolean createApproximateSelfIfCanOnlyFindPartsOfSelf)
returns the CodeSimian.jar that contains this program executing now, or null if could not find it.

The purpose of this function is to get the bytes of CodeSimian's internal files, and to help new versions of CodeSimian.jar (containing any new files including code files) be automaticly created without the need for a Java compiler or files or the command-line. CodeSimian creates new versions of CodeSimian as a single file (a new CodeSimian.jar). But Jars.findSelf(boolean) only GETS CodeSimian.jar. It does not modify it except if it creates it from parts it finds in different places.

If your program contains Codesimian.jar and hides CodeSimian.jar inside a Jar or other place, you should modify this function to look for CodeSimian.jar wherever you put it, or if your program is a Jar and is similar to CodeSimian, and if you keep the file/folder structure in CodeSimian.jar at root level of your new Jar, it may work to make findSelf(boolean) return your own Jar instead of CodeSimian.jar.

WARNING: somebody may rename CodeSimian.jar to some other .jar name. If CodeSimian.jar is on your hard-drive, it can probably find itself by using system property "java.class.path" (finds a file whose name is similar to "CodeSimian.jar"), but if this program is in some applets, access is dened to java's classpath and only by knowing the exact text "/CodeSimian.jar" can you find CodeSimian.jar. If somebody renames CodeSimian.jar, applets will probably get an error or null from Jars.findSelf(boolean). It works for some applets, depending on your web browser and which version of Java is installed.

Use arbitrary strings to get CodeSimian.jar from an applet, if this program is now in an applet. If that doesnt work, search the CLASSPATH then find the JAR with the name (case insensitive) that ends with .jar and is most similar to "CodeSimian.jar".

This function needs to be tested many times on many different types of computers, from the hard-drive, from an applet in a webpage, and from memory directly (probably wont work here, but try to make it).

Parameters:
createApproximateSelfIfCanOnlyFindPartsOfSelf - WARNING: approximate self Jars often do not work because they are made of less than all the files inside CodeSimian.jar and some files are required. 9/06 no code exists to approximate a JAR, but this ability should be added later in case all the files cant be found, or if a JAR needs to be repaired...

suicide

public static void suicide(Liquid liquidJarSuicide)
This Jar file (like CodeSimian.jar) is the Java program that is running now. It deletes itself if liquidJarSuicide.amount() is at least 1.

TODO: should this running program also be closed? It wouldnt be a "suicide" without it. And what if you saved a new jar after the old jar is deleted? Jar suicide should be permanent. If jar suicide was not permanent, any quantum-physics-related experiments involving truly random numbers would not be accurate.

TODO: should this be called codesimianSuicide instead of jarSuicide (and liquidCodesimianSuicide)?


findSelfBytes

public static byte[] findSelfBytes(boolean createApproximateSelfIfCanOnlyFindPartsOfSelf)

findSimilarToSelf

public static java.io.ByteArrayInputStream[] findSimilarToSelf()
returns Jar files it thinks are most similar to the CodeSimian.jar currently executing. If findSelf() returns a valid ByteArrayInputStream, it will be first in the returned array. More similar at lower index. If findSelf() fails, a less similar Jar will be in index 0 or the array could be empty.

This function should be modified to search the internet and optionally your hard-drive for Jar files with names similar to CodeSimian.jar or any Jar file that contains almost the same files as CodeSimian.jar.


consumeSimilar

public static java.io.ByteArrayInputStream consumeSimilar(java.io.ByteArrayInputStream similarToMe,
                                                          int minBytesToConsume,
                                                          int maxBytesToConsume)
Modifies this program executing now and saves it in a new CodeSimian.jar. SimilarToMe is a modified CodeSimian.jar which contains some things that this program does not. Some of those things will be included (and modified?) in the returned JAR.


consumeAnotherProgram

public static java.io.ByteArrayInputStream consumeAnotherProgram(java.io.ByteArrayInputStream anyOtherJarProgram)
any JAR file contains classes and maybe other files that CodeSimian might consume by copying it into a new CodeSimian.jar

This class should search for a license text file and license at top of source-code files and announce this info to a LicenseListener object (LicenseListener interface not yet created 9/06). It is easy to get sued if you use somebody elses Java code without permission. Scan for compatible licenses like "GPL" "LGPL" "NEW BSD" "PUBLIC DOMAIN" etc in the files in anyOtherJarProgram, and put the code to do all that in a separate function or class.


findJarsOnTheInternet

public static java.io.ByteArrayInputStream[] findJarsOnTheInternet(CS jarJudger,
                                                                   java.net.URL[] startSearchingHere,
                                                                   int howManyJarsDoYouWant,
                                                                   long maxMilliseconds,
                                                                   long minBytesEachJar,
                                                                   long maxBytesEachJar)
Uses jarJudger.setL(ByteArrayInputStream) then executes jarJudger.D() to judge JARs it finds on the internet, and based on how jarJudger judges, tries to find better JARs on the internet next time you call this function with the same (or similar?) jarJudger object.

It might be better for jarJudger to take a list of bytes (bytes of the JAR file) as a parameter in addition to allowing jarJudger.setL(anyByteArrayInputStream) and jarJudger.L(ByteArrayInputStream.class). For efficiency, the list of bytes can be a subclass of PrimitiveArray.ByteArray.

This function should search for licenses like consumeAnotherProgram(ByteArrayInputStream) does, and tell the user if an incompatible license for software is in the returned ByteArrayInputStream[] array.


findAnythingOnTheInternet

public static java.io.ByteArrayInputStream[] findAnythingOnTheInternet(CS byteJudger,
                                                                       java.net.URL[] startSearchingHere,
                                                                       int howManyFilesDoYouWant,
                                                                       long maxMilliseconds,
                                                                       long minBytesEach,
                                                                       long maxBytesEach)

consumeAnyBytes

public static java.io.ByteArrayInputStream consumeAnyBytes(java.lang.String nameOrDescription,
                                                           java.io.InputStream anything)
Creates a new JAR file (a new version of the currently-executing CodeSimian.jar) after consuming some bytes. The bytes can be from anywhere: a file from your hard-drive or the internet, images, videos, Java programs, anything it can theoretically be coded to use...

Parameters:
nameOrDescription - a filename, for example. Can be null.

breed

public static java.io.ByteArrayInputStream breed(java.io.ByteArrayInputStream[] jars)
Breeds 2 or more modified CodeSimian.jar's into a single CodeSimian.jar which is returned


GPLcompatible

public static float GPLcompatible(java.io.ByteArrayInputStream anyJar)
is a JAR file compatible with the GNU GPL license? All LGPL, new BSD, and many other licenses are GPL-compatible.

CodeSimian (created by Benjamin F Rayfield, copyright 2006 and 2007) is licensed by GPL (no L).

9/06 I can not think of any way to know if a JAR is GPL-compatible except to search it for "GPL" in a sentence that does not contain "not", or find the GPL license in a text file.

Returns:
-2 to 1: between -2 to -1 if NOT GPL compatible, -1 to 0 if dont know, and 0 - 1 if it is. Compatible with CodeSimian's boolean system where positive numbers are true and 0 and negative are false.

LGPLcompatible

public static float LGPLcompatible(java.io.ByteArrayInputStream anyJar)
is a JAR file compatible with the GNU LGPL license? Unlike GPL, LGPL allows you to keep your code secret while distributing the executable. A GPL software may use an LGPL software, but not the reverse.

Returns:
-2 to 1: between -2 to -1 if NOT LGPL compatible, -1 to 0 if dont know, and 0 - 1 if it is. Compatible with CodeSimian's boolean system where positive numbers are true and 0 and negative are false.

findLicenses

public static java.lang.String[] findLicenses(java.io.ByteArrayInputStream anyJar)
should return strings like "GNU GPL" "GNU LGPL" "public domain" "new BSD" "License of Guile" etc, or whichever licenses the input program allows (can choose any, not required to use all). If multiple licenses are required, for example, you should concatenate them with " and ", and return that combination as a single license.

Your program MUST allow 1 of GNU GPL 2+ licenses, unless you have Benjamin F Rayfield's legal permission not to. Also, GNU GPL 2+ prevents you from using certain other licenses (example: "public domain"), unless you have permission.

If anyJar is null then returns the license(s) for THIS SOFTWARE.

Legally findLicenses(findSelf()) must always return at least "GNU GPL 2", or "GNU GPL 3", etc. and may not return any GPL-INcompatible licenses except by legal permission of Benjamin F Rayfield.


readBytesForCurrentEntry

public static byte[] readBytesForCurrentEntry(java.util.zip.ZipEntry currentEntry,
                                              java.util.zip.ZipInputStream jarIn)
jarIn.getNextJarEntry() must have returned currentEntry the last time it was called, because that positions the ByteArrayInputStream to the beginning of the entry. Can not use ByteArrayInputStream.available() to determine size because it always returns 0 or 1.

NEED TO MODIFY THIS FUNCTION TO WORK FOR ZipEntry and ZipInputStream also.


getUpdatedSelfBytes

public static byte[] getUpdatedSelfBytes()
                                  throws java.io.IOException
Throws:
java.io.IOException

getUpdatedSelfInputStream

public static java.io.ByteArrayInputStream getUpdatedSelfInputStream()
returns the bytes for a new CodeSimian.jar, including all files in the original CodeSimian.jar (returned by findSelf()), except for files deleted by deleteInternalFile and files added or modified by setInternalFile (in InnerFiles.java).

TODO: modify this function so it doesnt create the zip twice.


consumeFilesInJar

public int consumeFilesInJar(java.io.ByteArrayInputStream bytesOfJarFile,
                             java.lang.String putInThisInternalFolder,
                             CS fileNameJudge,
                             CS fileBytesJudge,
                             CS booleanOverwriteP0WithP1)
if putInThisInternalFolder is null, use the files' existing folder plus some prefix. If putInThisInternalFolder is not null, all selected files in the JAR are stripped of path and put there.

TODO: should this function be static?

Parameters:
booleanOverwriteP0WithP1 - Its first 2 params are overwritten by String filenames: P0 old file, P1 new file. Both are codesimian internal virtual names and paths, like "cs/SelfReplicatingWindow10.cs".

consumeFilesInJar

public int consumeFilesInJar(java.io.ByteArrayInputStream bytesOfJarFile,
                             java.lang.String putInThisInternalFolder,
                             java.lang.String fileNameEnd)
example: consumeFilesInJar(bytesOfSomeJar, "cs/", ".cs")