private void readCentralDirectory() throws IOException{ InputStream in = readData(tableOffset, tableLength); byte[] header = new byte[tableLength]; // Fix for "PK12 bug in town.zip": sometimes // not entire byte array will be read with InputStream.read() // (especially for big headers) fillByteArray(header, in); // in.read(header); in.close(); entries = new HashMap<String, ZipEntry2>(numEntries); int offset = 0; for (int i = 0; i < numEntries; i++){ offset = readTableEntry(header, offset); } }
public InputStream openStream(String name) throws IOException{ ZipEntry2 entry = entries.get(name); if (entry == null) throw new RuntimeException("Entry not found: "+name); return openStream(entry); }
private InputStream openStream(ZipEntry2 entry) throws IOException{ InputStream in = readData(entry.offset, entry.compSize); if (entry.deflate){ return new InflaterInputStream(in, new Inflater(true)); } return in; }
private int readTableEntry(byte[] table, int offset) throws IOException{ if (get32(table, offset) != ZipEntry.CENSIG){ throw new IOException("Central directory error, expected 'PK12'"); int nameLen = get16(table, offset + ZipEntry.CENNAM); int extraLen = get16(table, offset + ZipEntry.CENEXT); int commentLen = get16(table, offset + ZipEntry.CENCOM); int newOffset = offset + ZipEntry.CENHDR + nameLen + extraLen + commentLen; int flags = get16(table, offset + ZipEntry.CENFLG); if ((flags & 1) == 1){ int method = get16(table, offset + ZipEntry.CENHOW); if (method != ZipEntry.DEFLATED && method != ZipEntry.STORED){ String name = getUTF8String(table, offset + ZipEntry.CENHDR, nameLen); if (name.charAt(name.length()-1) == '/'){ ZipEntry2 entry = new ZipEntry2(); entry.name = name; entry.deflate = (method == ZipEntry.DEFLATED); entry.crc = getu32(table, offset + ZipEntry.CENCRC); entry.length = get32(table, offset + ZipEntry.CENLEN); entry.compSize = get32(table, offset + ZipEntry.CENSIZ); entry.offset = get32(table, offset + ZipEntry.CENOFF);
InputStream in = readData(Integer.MAX_VALUE, 200); byte[] header = new byte[200]; fillByteArray(header, in); in.close(); for (int i = 200 - 22; i >= 0; i--){ if (header[i] == (byte) (ZipEntry.ENDSIG & 0xff) && get32(header, i) == ZipEntry.ENDSIG){ throw new IOException("Cannot find Zip End Header in file!"); numEntries = get16(header, offset + ZipEntry.ENDTOT); tableLength = get32(header, offset + ZipEntry.ENDSIZ); tableOffset = get32(header, offset + ZipEntry.ENDOFF);
public void load(URL url) throws IOException { if (!url.getProtocol().equals("http") && !url.getProtocol().equals("https")) throw new UnsupportedOperationException("HttpZipLocator only supports HTTP(S) URLs"); zipUrl = url; readEndHeader(); readCentralDirectory(); }
public AssetInfo locate(AssetManager manager, AssetKey key) { String name = key.getName(); if(name.startsWith("/"))name=name.substring(1); ZipEntry entry = zipfile.getEntry(name); if (entry == null) return null; return new JarAssetInfo(manager, key, entry); }
public void setRootPath(String path){ if (!rootPath.equals(path)){ rootPath = path; try { load(new URL(path)); } catch (IOException ex) { logger.log(Level.WARNING, "Failed to set root path "+path, ex); } } }
public AssetInfo locate(AssetManager manager, AssetKey key) { String name = key.getName(); File file = new File(root, name); if (file.exists() && file.isFile()){ try { // Now, check asset name requirements String canonical = file.getCanonicalPath(); String absolute = file.getAbsolutePath(); if (!canonical.endsWith(absolute)){ throw new AssetNotFoundException("Asset name doesn't match requirements.\n"+ "\"" + canonical + "\" doesn't match \"" + absolute + "\""); } } catch (IOException ex) { throw new AssetLoadException("Failed to get file canonical path " + file, ex); } return new AssetInfoFile(manager, key, file); }else{ return null; } }
public AssetInfo locate(AssetManager manager, AssetKey key) { String name = key.getName(); try{ //TODO: remove workaround for SDK // URL url = new URL(root, name); if(name.startsWith("/")){ name = name.substring(1); } URL url = new URL(root.toExternalForm() + name); return UrlAssetInfo.create(manager, key, url); }catch (FileNotFoundException e){ return null; }catch (IOException ex){ logger.log(Level.WARNING, "Error while locating " + name, ex); return null; } }
public static UrlAssetInfo create(AssetManager assetManager, AssetKey key, URL url) throws IOException { // Check if URL can be reached. This will throw // IOException which calling code will handle. URLConnection conn = url.openConnection(); conn.setUseCaches(false); InputStream in = conn.getInputStream(); // For some reason url cannot be reached? if (in == null){ return null; }else{ return new UrlAssetInfo(assetManager, key, url, in); } }
InputStream in = androidResources.getAssets().open(assetPath); if (in != null){ return new AndroidAssetInfo(manager, key, assetPath, in, 0); String resourceName = getResourceName(assetPath); int resourceId = androidResources.getIdentifier(resourceName, "drawable", androidPackageName); if (in != null){ return new AndroidAssetInfo(manager, key, assetPath, in, resourceId);
private static AudioStream loadStream(AssetInfo assetInfo) throws IOException { AndroidAssetInfo aai = (AndroidAssetInfo) assetInfo; AssetFileDescriptor afd = null; NativeVorbisFile file = null; boolean success = false; try { afd = aai.openFileDescriptor(); int fd = afd.getParcelFileDescriptor().getFd(); file = new NativeVorbisFile(fd, afd.getStartOffset(), afd.getLength()); AudioStream stream = new AudioStream(); stream.setupFormat(file.channels, 16, file.sampleRate); stream.updateData(new VorbisInputStream(afd, file), file.duration); success = true; return stream; } finally { if (!success) { if (file != null) { file.close(); } if (afd != null) { afd.close(); } } } }
private InputStream openStream(ZipEntry2 entry) throws IOException{ InputStream in = readData(entry.offset, entry.compSize); if (entry.deflate){ return new InflaterInputStream(in, new Inflater(true)); } return in; }
public InputStream openStream(String name) throws IOException{ ZipEntry2 entry = entries.get(name); if (entry == null) throw new RuntimeException("Entry not found: "+name); return openStream(entry); }
public AssetInfo locate(AssetManager manager, AssetKey key) { String name = key.getName(); ZipEntry entry = zipfile.getEntry(name); if (entry == null) return null; return new JarAssetInfo(manager, key, entry); }
return UrlAssetInfo.create(manager, key, url); }catch (IOException ex){
private static AudioBuffer loadBuffer(AssetInfo assetInfo) throws IOException { AndroidAssetInfo aai = (AndroidAssetInfo) assetInfo; AssetFileDescriptor afd = null; NativeVorbisFile file = null; try { afd = aai.openFileDescriptor(); int fd = afd.getParcelFileDescriptor().getFd(); file = new NativeVorbisFile(fd, afd.getStartOffset(), afd.getLength()); ByteBuffer data = BufferUtils.createByteBuffer(file.totalBytes); file.readFully(data); AudioBuffer ab = new AudioBuffer(); ab.setupFormat(file.channels, 16, file.sampleRate); ab.updateData(data); return ab; } finally { if (file != null) { file.close(); } if (afd != null) { afd.close(); } } }
private InputStream openStream(ZipEntry2 entry) throws IOException{ InputStream in = readData(entry.offset, entry.compSize); if (entry.deflate){ return new InflaterInputStream(in, new Inflater(true)); } return in; }
public InputStream openStream(String name) throws IOException{ ZipEntry2 entry = entries.get(name); if (entry == null) throw new RuntimeException("Entry not found: "+name); return openStream(entry); }