Example usage for java.nio ByteBuffer position

List of usage examples for java.nio ByteBuffer position

Introduction

In this page you can find the example usage for java.nio ByteBuffer position.

Prototype

public final Buffer position(int newPosition) 

Source Link

Document

Sets the position of this buffer.

Usage

From source file:com.healthmarketscience.jackcess.impl.TableImpl.java

/**
 * Reads the null mask from the given row buffer.  Leaves limit unchanged.
 *///from   w w w  .  jav a2s .  c  o m
private NullMask getRowNullMask(ByteBuffer rowBuffer) throws IOException {
    // reset position to row start
    rowBuffer.reset();

    // Number of columns in this row
    int columnCount = ByteUtil.getUnsignedVarInt(rowBuffer, getFormat().SIZE_ROW_COLUMN_COUNT);

    // read null mask
    NullMask nullMask = new NullMask(columnCount);
    rowBuffer.position(rowBuffer.limit() - nullMask.byteSize()); //Null mask at end
    nullMask.read(rowBuffer);

    return nullMask;
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

/**
 * //from  ww  w  .j  ava2  s . com
 * @param pin
 * @param counterID
 * @return the 7-byte stream keyID || index || threshold || cursor(4 bytes).
 */
private byte[] readCounter(int pin, int counterID) {
    byte[] data = new byte[5];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    data[4] = (byte) counterID;
    ByteBuffer buf = ByteBuffer.allocate(11);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readCounter, 0, 0, 5 });
    buf.put(data);
    buf.put((byte) 7);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for readCounter: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from readCounter: " + response);
        System.out.println("With data: " + Arrays.toString(response.getData()));
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return response.getData();
        } else {
            return null;
        }
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

/**
 * //  w ww  .  j  av  a2s  .c  o  m
 * @param mode
 * @param component
 * @param groupID 
 * @param compType (0: mod, 1: order, 2: co-factor)
 * @param rootKey
 * @return
 * @throws CardException
 */
private SmartcardStatusCode setGroupComponent(int mode, byte[] component, int groupID, int compType,
        RSAKeyPair rootKey) throws CardException {
    component = removeSignBit(component);

    byte[] data = new byte[2];
    data[0] = (byte) groupID;
    data[1] = (byte) compType;
    if (mode == 1) {
        this.putData(component);
    } else {
        System.out.println("Can only use setGroupComponent in root mode");
        return SmartcardStatusCode.UNAUTHORIZED;
    }
    ByteBuffer buf = ByteBuffer.allocate(7);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.setGroupComponent, 0, 0, 2, (byte) groupID,
            (byte) compType });
    buf.position(0);
    System.out.println("Input for set Group Component: " + Arrays.toString(buf.array()));
    ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
    System.out.println("Response from setGroupComponent: " + response);
    return this.evaluateStatus(response);
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

/**
  * // w  w w  .j  av  a 2 s  .c  om
  * @param pin
  * @param credentialID
  * @return byte array containing: issuerID || size(v) [2 bytes] || size(kv) [2 bytes] || status || prescount
  */
private byte[] readCredential(int pin, int credentialID) {
    byte[] data = new byte[5];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    data[4] = (byte) credentialID;
    ByteBuffer buf = ByteBuffer.allocate(11);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readCredential, 0, 0, 5 });
    buf.put(data);
    buf.put((byte) 7);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for readCredential: " + Arrays.toString(buf.array()));
        System.out.println("Reading the on-board credential with ID=" + credentialID);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from readCredential: " + response);
        System.out.println("With the data: " + Arrays.toString(response.getData()));
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return response.getData();
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

/**
 * @param pin//from  w  ww  .  j  a  va2  s .  c  o m
 * @param issuerID
 * @return byte array containing: groupID || genID1 || genID2 || numpres || counterID
 */
private byte[] readIssuer(int pin, int issuerID) {
    if (cachedIssuerByteArray.containsKey(issuerID)) {
        byte[] cached = cachedIssuerByteArray.get(issuerID);
        System.out.println("ReadIssuer - use cached : " + (cached == null ? null : Arrays.toString(cached)));
        return cached;
    }

    byte[] data = new byte[5];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    data[4] = (byte) issuerID;
    ByteBuffer buf = ByteBuffer.allocate(11);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readIssuer, 0, 0, 5 });
    buf.put(data);
    buf.put((byte) 5);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for readIssuer: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from readIssuer: " + response);
        System.out.println("With the data: " + Arrays.toString(response.getData()));
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            cachedIssuerByteArray.put(issuerID, response.getData());
            return response.getData();
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    cachedIssuerByteArray.put(issuerID, null);
    return null;
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

@Override
public SmartcardStatusCode deleteBlob(int pin, URI uri) {
    byte[] uriBytes = null;
    uriBytes = this.uriToByteArr(uri);
    if (uriBytes.length > 199) {
        return SmartcardStatusCode.REQUEST_URI_TOO_LONG;
    }//from   w ww  .  j  ava2  s.  co  m
    // BLOB CACHE!
    blobCache.remove(uri);
    blobUrisCache.remove(uri);

    byte[] data = new byte[4 + uriBytes.length];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    System.arraycopy(uriBytes, 0, data, 4, uriBytes.length);
    ByteBuffer buf = ByteBuffer.allocate(9 + uriBytes.length);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.removeBlob, 0, 0, (byte) data.length });
    buf.put(data);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for removeBlob: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from removeBlob: " + response);
        return this.evaluateStatus(response);
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

public List<Byte> listCredentialIDs(int pin) {
    ByteBuffer buf = ByteBuffer.allocate(10);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.listCredentials, 0, 0, 4 });
    buf.put(this.pinToByteArr(pin));
    buf.put((byte) 0);
    buf.position(0);
    try {//from   w  w w .  jav a 2 s.  c o  m
        if (printInput)
            System.out.println("Input for listCredentials: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from listCredentials: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            List<Byte> credentialIDs = new ArrayList<Byte>();
            byte[] creds = response.getData();
            for (byte cred : creds) {
                credentialIDs.add(cred);
            }
            return credentialIDs;
        }
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }
    return null;
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

@Override
public SmartcardStatusCode deleteCredential(int pin, URI credentialId) {
    byte credID = this.getCredentialIDFromUri(pin, credentialId);
    ByteBuffer buf = ByteBuffer.allocate(10);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.removeCredential, 0, 0, 5 });
    buf.put(this.pinToByteArr(pin));
    buf.put(credID);/*from   www.j  a  v a2 s.  c om*/
    buf.position(0);
    try {
        System.out.println("Removing credential with uri: " + credentialId);
        this.deleteBlob(pin, credentialId);
        if (credentialId.toString().startsWith(UProveCryptoEngineUserImpl.UProveCredential)) {
            URI reloadURI = URI.create(credentialId.toString() + ReloadStorageManager.URI_POSTFIX);
            if (reloadURI.toString().contains(":") && !reloadURI.toString().contains("_")) {
                reloadURI = URI.create(reloadURI.toString().replaceAll(":", "_")); //change all ':' to '_'
            }
            this.deleteBlob(pin, reloadURI);
            System.out.println("deleted the reload blob of the credential: " + reloadURI);
        }
        this.removeCredentialUri(pin, credentialId);
        if (printInput)
            System.out.println("Input for removeCredential: " + Arrays.toString(buf.array()));
        System.out.println("Trying to remove on-board credential with ID=" + credID);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("response from RemoveCredential: " + response);
        return this.evaluateStatus(response);
    } catch (CardException e) {
        return SmartcardStatusCode.NOT_FOUND;
    }
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

@SuppressWarnings("unused")
private List<Byte> listCounters(int pin) {
    ByteBuffer buf = ByteBuffer.allocate(10);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.listCounters, 0, 0, 4 });
    buf.put(this.pinToByteArr(pin));
    buf.put(new byte[] { 0 });
    buf.position(0);
    try {/*www . j av a 2s .c om*/
        if (printInput)
            System.out.println("Input for listCounters: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from listCounters: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            List<Byte> counters = new ArrayList<Byte>();
            byte[] counterIDs = response.getData();
            for (byte counterID : counterIDs) {
                counters.add(counterID);
            }
            return counters;
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

@Override
public SmartcardStatusCode storeBlob(int pin, URI uri, SmartcardBlob blob) {
    //this.resetCard();

    String[] forbiddenChars = new String[] { "\u0167", ":", "*", "?", "<", ">", " ", "|" };
    if (uri.toString().contains(":") && !uri.toString().contains("_")) {
        uri = URI.create(uri.toString().replaceAll(":", "_")); //change all ':' to '_'
    } else {/*from ww  w .j a  v  a  2  s .  co m*/
        for (int i = 0; i < forbiddenChars.length; i++) {
            if (uri.toString().contains(forbiddenChars[i])) {
                throw new RuntimeException(
                        "Cannot store a blob under a URI containing the following char: " + forbiddenChars[i]);
            }
        }
    }
    byte[] uriBytes = null;
    uriBytes = this.uriToByteArr(uri);
    if (uriBytes.length > 199) {
        return SmartcardStatusCode.REQUEST_URI_TOO_LONG;
    }

    // BLOB CACHE!
    blobCache.put(uri, blob);
    blobUrisCache.add(uri);

    //first put data from blob followed by the STORE BLOB command
    this.putData(blob.blob);

    byte[] data = new byte[4 + uriBytes.length];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    System.arraycopy(uriBytes, 0, data, 4, uriBytes.length);
    ByteBuffer buf = ByteBuffer.allocate(9 + uriBytes.length);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.storeBlob, 0, 0, (byte) data.length });
    buf.put(data);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for storeBlob: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from storeBlob: " + response);
        if ((response.getSW1() != STATUS_OK) && (response.getSW1() != STATUS_BAD_PIN)) {
            throw new InsufficientStorageException("Could not store blob. Response from card: " + response);
        }
        return this.evaluateStatus(response);
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }
}