[Demonstration 1]

[Buggy Method]
public boolean isAuthenticated(Message message, ClassResourceInfo classResourceInfo) {
    // extract authorization header and authenticate.
    // get the map of protocol headers
    Map protocolHeaders = (TreeMap) message.get(Message.PROTOCOL_HEADERS);
    // get the value for Authorization Header
    List authzHeaders = (ArrayList) protocolHeaders.get(SCIMConstants.AUTHORIZATION_HEADER);
    if (authzHeaders != null) {
        // get the authorization header value, if provided
        String authzHeader = (String) authzHeaders.get(0);
        // decode it and extract username and password
        byte[] decodedAuthHeader = Base64.decode(authzHeader.split(" ")[1]);
        String authHeader = new String(decodedAuthHeader);
        String userName = authHeader.split(":")[0];
        String password = authHeader.split(":")[1];
        if (userName != null && password != null) {
            String tenantDomain = MultitenantUtils.getTenantDomain(userName);
            String tenantLessUserName = MultitenantUtils.getTenantAwareUsername(userName);
            try {
                // get super tenant context and get realm service which is an osgi service
                RealmService realmService = (RealmService) PrivilegedCarbonContext.getThreadLocalCarbonContext().getOSGiService(RealmService.class);
                if (realmService != null) {
                    int tenantId = realmService.getTenantManager().getTenantId(tenantDomain);
                    if (tenantId == -1) {
                        log.error("Invalid tenant domain " + tenantDomain);
                        return false;
                    }
                    // get tenant's user realm
                    UserRealm userRealm = realmService.getTenantUserRealm(tenantId);
                    boolean authenticated = userRealm.getUserStoreManager().authenticate(tenantLessUserName, password);
                    if (authenticated) {
                        // setup thread local variable to be consumed by the provisioning
                        // framework.
                        ThreadLocalProvisioningServiceProvider serviceProvider = new ThreadLocalProvisioningServiceProvider();
                        serviceProvider.setServiceProviderName(IdentityProvisioningConstants.LOCAL_SP);
                        serviceProvider.setClaimDialect(SCIMProviderConstants.DEFAULT_SCIM_DIALECT);
                        serviceProvider.setTenantDomain(MultitenantUtils.getTenantDomain(userName));
                        IdentityApplicationManagementUtil.setThreadLocalProvisioningServiceProvider(serviceProvider);
                        // if username doesn't contain a domain, add domain to user
                        // name in order to comply with multiple user store feature.
                        if (!userName.contains(CarbonConstants.DOMAIN_SEPARATOR)) {
                            String domain = UserCoreUtil.getDomainFromThreadLocal();
                            if (domain != null) {
                                userName = domain + CarbonConstants.DOMAIN_SEPARATOR + userName;
                            }
                        }
                        // authentication success. set the username for authorization header and
                        // proceed the REST call
                        authzHeaders.set(0, userName);
                        PrivilegedCarbonContext.startTenantFlow();
                        PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
                        carbonContext.setUsername(userName);
                        carbonContext.setTenantId(tenantId);
                        carbonContext.setTenantDomain(tenantDomain);
                        return true;
                    } else {
                        UnauthorizedException unauthorizedException = new UnauthorizedException("Authentication failed for the user: " + tenantLessUserName + "@" + tenantDomain);
                        log.error(unauthorizedException.getDescription());
                        return false;
                    }
                } else {
                    log.error("Error in getting Realm Service for user: " + userName);
                    InternalServerException internalServerException = new InternalServerException("Internal server error while authenticating the user: " + tenantLessUserName + "@" + tenantDomain);
                    log.error(internalServerException.getDescription());
                    return false;
                }
            } catch (UserStoreException e) {
                InternalServerException internalServerException = new InternalServerException("Internal server error while authenticating the user.");
                log.error(internalServerException.getDescription(), e);
                return false;
            }
        } else {
            UnauthorizedException unauthorizedException = new UnauthorizedException("Authentication required for this resource. Username or password not provided.");
            log.error(unauthorizedException.getDescription());
            return false;
        }
    } else {
        UnauthorizedException unauthorizedException = new UnauthorizedException("Authentication required for this resource. Authorization header not present in the request.");
        log.error(unauthorizedException.getDescription());
        return false;
    }
}

[Buggy Hunk]
String authHeader = new String(decodedAuthHeader);

[Patch]
String authHeader = new String(decodedAuthHeader, StandardCharsets.UTF_8);

[Demonstration 2]

[Buggy Method]
public static String decodeToString(ByteBuffer shaderInfoLogBuffer, IntBuffer size) {
    int s = size.get(0);
    byte[] bs = new byte[s];
    shaderInfoLogBuffer.get(bs, 0, bs.length);
    return new String(bs);
}

[Buggy Hunk]
return new String(bs);

[Patch]
return new String(bs,CHARSET_UTF8);

[Demonstration 3]

[Buggy Method]
public byte[] toByteArray() {
    byte[] nameBytes = FSUtils.getNameWithSpaces(name).getBytes(FSConstants.CHARSET);
    byte attributes = getAttributeByte();
    byte[] result = new byte[FSConstants.FILE_HEADER_LENGTH];
    System.arraycopy(nameBytes, 0, result, 0, nameBytes.length);
    result[FILE_ATTRIBUTES_OFFSET] = attributes;
    System.arraycopy(FSUtils.intAsFourBytes(clusterNumber), 0, result, FILE_CLUSTER_OFFSET, FSConstants.BYTE_DEPTH);
    System.arraycopy(FSUtils.intAsFourBytes(size), 0, result, FILE_SIZE_OFFSET, FSConstants.BYTE_DEPTH);
    return result;
}

[Buggy Hunk]
public byte[] toByteArray() {

[Patch]
byte[] toByteArray() {