Files
scanpath-v2/src/main/java/dxt/scanpath/v2/io/JarBinFile.java
T
2026-11-14 21:14:29 +05:30

112 lines
2.7 KiB
Java

/*
* Copyright (c) DuxTechnica
*
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
* use this file except in compliance with the License. You may obtain a copy of the License by
* contacting:
*
* Dux Technica, [email protected]
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package dxt.scanpath.v2.io;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* Represents a class-file entry within a JAR archive.
*
* @author indroneel
*/
public class JarBinFile implements BinFile {
private final JarFile root;
private final JarEntry entry;
/**
* Creates a representation of a JAR class-file entry.
*
* @param root the containing JAR file
* @param entry the class-file entry
*/
public JarBinFile(JarFile root, JarEntry entry) {
this.root = root;
this.entry = entry;
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Methods of interface BinFile
/**
* Returns the fully qualified class name derived from the archive entry name.
*
* @return the fully qualified class name
*/
@Override
public String getClassName() {
String name = entry.getName();
if(name.startsWith("/")) {
name = name.substring(1, name.lastIndexOf('.')).replace('/', '.');
}
else {
name = name.substring(0, name.lastIndexOf('.')).replace('/', '.');
}
return name;
}
/**
* Returns the class name without package information.
*
* @return the simple class name
*/
@Override
public String getSimpleClassName() {
String fqcn = getClassName();
return fqcn.substring(fqcn.lastIndexOf('.') + 1);
}
/**
* Reads the class-file contents from the JAR archive.
*
* @return the class-file contents
* @throws IOException if the entry cannot be read
*/
@Override
public byte[] getContent() throws IOException {
InputStream istrm = root.getInputStream(entry);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] chunk = new byte[1024];
int amt = 0;
while(amt >=0) {
amt = istrm.read(chunk);
buffer.write(chunk, 0, Math.max(amt, 0));
}
istrm.close();
buffer.flush();
buffer.close();
return buffer.toByteArray();
}
/**
* Returns the archive path and entry path identifying this class file.
*
* @return the JAR entry location
*/
@Override
public String toString() {
return root.getName() + "!" + java.io.File.separatorChar + entry.toString();
}
}