下面列出了java.util.jar.JarInputStream#available ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Map<String, byte[]> extractClasses(JarInputStream jarReader, NameStyle nameStyle) throws IOException {
Map<String, byte[]> classMap = new HashMap<>();
byte[] tempReadingBuffer = new byte[MAX_CLASS_BYTES];
JarEntry entry;
while (null != (entry = jarReader.getNextJarEntry())) {
String name = entry.getName();
if (name.endsWith(".class")
&& !name.equals("package-info.class")
&& !name.equals("module-info.class")) {
String internalClassName = name.replaceAll(".class$", "");
if (nameStyle.equals(NameStyle.DOT_NAME)) {
internalClassName = Utilities.internalNameToFulllyQualifiedName(internalClassName);
}
int readSize = jarReader.readNBytes(tempReadingBuffer, 0, tempReadingBuffer.length);
if (0 != jarReader.available()) {
throw new RuntimeException("Class file too big: " + name);
}
byte[] classBytes = new byte[readSize];
System.arraycopy(tempReadingBuffer, 0, classBytes, 0, readSize);
classMap.put(internalClassName, classBytes);
}
}
return classMap;
}
private void replaceManifest(InputStream is, OutputStream os, Manifest manifest) throws IOException {
JarInputStream in = new JarInputStream(is);
try {
JarOutputStream out = new JarOutputStream(os, manifest);
try {
JarEntry entry = null;
byte[] temp = new byte[32768];
while ((entry = in.getNextJarEntry()) != null) {
String name = entry.getName();
if (name.equalsIgnoreCase("META-INF/MANIFEST.MF")) { // NOI18N
continue;
}
out.putNextEntry(entry);
while (in.available() != 0) {
int read = in.read(temp);
if (read != -1) {
out.write(temp, 0, read);
}
}
out.closeEntry();
}
} finally {
out.close();
}
} finally {
in.close();
}
}
private boolean unzipJarAlt(String pathJar, String pathDir) {
try {
FileInputStream fileStream = new FileInputStream(File.separator + pathJar);
JarInputStream jarStream = new JarInputStream(fileStream);
JarEntry entry = jarStream.getNextJarEntry();
while (entry != null) {
File fileEntry = new File(File.separator + pathDir + File.separator + entry.getName());
if (entry.isDirectory()) {
fileEntry.mkdirs();
}
else {
FileOutputStream os = new FileOutputStream(fileEntry);
while (jarStream.available() > 0) {
os.write(jarStream.read());
}
os.close();
}
entry = jarStream.getNextJarEntry();
}
jarStream.close();
return true;
}
catch (IOException e) {
e.printStackTrace();
}
return false;
}
@Override
public Map<String, byte[]> getClassData() {
byte[] jarData = getFileData();
try {
ByteArrayInputStream bis = new ByteArrayInputStream(jarData);
JarInputStream jis = new JarInputStream(bis);
JarEntry entry = jis.getNextJarEntry();
Map<String, byte[]> classData = new HashMap<String, byte[]>();
while (entry != null) {
String name = entry.getName();
if (name.endsWith(".class")) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while (jis.available() > 0) {
bos.write(jis.read());
}
classData.put(DominoUtils.filePathToJavaBinaryName(name, "/"), bos.toByteArray());
}
entry = jis.getNextJarEntry();
}
jis.close();
return classData;
} catch (IOException ioe) {
DominoUtils.handleException(ioe);
return null;
}
}
@Override
public Map<String, byte[]> getClassData() {
// For this one, we'll need the note in the database
if (classData == null) {
classData = new TreeMap<String, byte[]>();
try {
byte[] jarData = getFileDataRaw("%%object%%.jar");
InputStream objInputStream = new ByteArrayInputStream(jarData);
JarInputStream jis = new JarInputStream(objInputStream);
JarEntry entry = jis.getNextJarEntry();
while (entry != null) {
String name = entry.getName();
if (name.endsWith(".class")) { //TODO our classloader should support resources also!
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while (jis.available() > 0) {
bos.write(jis.read());
}
classData.put(DominoUtils.filePathToJavaBinaryName(name, "/"), bos.toByteArray());
}
entry = jis.getNextJarEntry();
}
jis.close();
objInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return classData;
}