Java Code Examples for com.strobel.assembler.metadata.Buffer#reset()

The following examples show how to use com.strobel.assembler.metadata.Buffer#reset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UimaDecompiler.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * A special type loader that substitutes the byteArray for the given class name
 * @param classNameSlashes the name to look for to substitute
 * @param byteArray the value to substitute
 */
private void setDecompilerSettingsForByteArray(String classNameSlashes, byte[] byteArray) {
  ITypeLoader tl = new ITypeLoader() {
    
    @Override
    public boolean tryLoadType(String internalName, Buffer buffer) {
      if (classNameSlashes.equals(internalName)) {
        int length = byteArray.length;
        buffer.reset(length);
        System.arraycopy(byteArray, 0, buffer.array(), 0, length);
        return true;
      } else {
        return false;
      }
    }      
  }; 
  ITypeLoader tc = new CompositeTypeLoader(tl, getClasspathTypeLoader(), new InputTypeLoader());
  decompilerSettings.setTypeLoader(tc);
}
 
Example 2
Source File: ComposedTypeLoader.java    From Recaf with MIT License 5 votes vote down vote up
@Override
public boolean tryLoadType(String s, Buffer buffer) {
    for (ITypeLoader loader : this.loaders) {
        if (loader.tryLoadType(s, buffer)) return true;
        buffer.reset();
    }
    return false;
}
 
Example 3
Source File: ProcyonDecompiler.java    From bytecode-viewer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean tryLoadType(final String internalName,
                           final Buffer buffer) {
    for (final ITypeLoader typeLoader : _typeLoaders) {
        if (typeLoader.tryLoadType(internalName, buffer)) {
            return true;
        }

        buffer.reset();
    }

    return false;
}
 
Example 4
Source File: LuytenTypeLoader.java    From Luyten with Apache License 2.0 5 votes vote down vote up
@Override
public boolean tryLoadType(final String internalName, final Buffer buffer) {
	for (final ITypeLoader typeLoader : _typeLoaders) {
		if (typeLoader.tryLoadType(internalName, buffer)) {
			return true;
		}

		buffer.reset();
	}

	return false;
}
 
Example 5
Source File: UimaDecompiler.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private ITypeLoader getClasspathTypeLoader() {
    return new ITypeLoader() {
      
      @Override
      public boolean tryLoadType(String internalName, Buffer buffer) {
        
        // read the class as a resource, and put into temporary byte array output stream
        // because we need to know the length
//        System.out.println("debug trying to load " + internalName);
        internalName = internalName.replace('.', '/') + ".class";
        InputStream stream = classLoader.getResourceAsStream(internalName);
        if (stream == null) {
//          System.out.println("debug failed to load " + internalName);
          return false;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 16);        
        byte[] b = new byte[1024 * 16]; 
        int numberRead;
        try {
          while (0 <= (numberRead = stream.read(b))){
            baos.write(b, 0, numberRead);
          }
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
        
        // Copy result (based on length) into output buffer spot
        int length = baos.size();
        b = baos.toByteArray();
        buffer.reset(length);
        System.arraycopy(b, 0, buffer.array(), 0, length);
//        System.out.println("debug OK loading " + internalName);
        return true;
      }      
    };    
  }
 
Example 6
Source File: WindupJarTypeLoader.java    From windup with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean tryLoadType(final String internalName, final Buffer buffer)
{
    try
    {
        if (LOG.isLoggable(Level.FINE))
        {
            LOG.fine("Attempting to load type: " + internalName + "...");
        }

        final JarEntry entry = _jarFile.getJarEntry(internalName + ".class");

        if (entry == null)
        {
            final String mappedName = _knownMappings.get(internalName);

            return mappedName != null &&
                        !mappedName.equals(internalName) && tryLoadType(mappedName, buffer);
        }

        final InputStream inputStream = _jarFile.getInputStream(entry);

        int remainingBytes = inputStream.available();

        buffer.reset(remainingBytes);

        while (remainingBytes > 0)
        {
            final int bytesRead = inputStream.read(buffer.array(), buffer.position(), remainingBytes);

            if (bytesRead < 0)
            {
                break;
            }

            buffer.position(buffer.position() + bytesRead);
            remainingBytes -= bytesRead;
        }

        buffer.position(0);

        final String actualName = getInternalNameFromClassFile(buffer);

        if (actualName != null && !actualName.equals(internalName))
        {
            _knownMappings.put(actualName, internalName);
        }

        if (LOG.isLoggable(Level.FINE))
        {
            LOG.fine("Type loaded from " + _jarFile.getName() + "!" + entry.getName() + ".");
        }

        return true;
    }
    catch (IOException e)
    {
        throw ExceptionUtilities.asRuntimeException(e);
    }
}