/** * Dnieprov es una librería que implementa un driver en JAVA para el DNI * electrónico y un proveedor cryptográfico compatible con la JCA de Java. * Código fuente disponible en http://github.com/lfern/dnieprov * * Copyright 2012 Luis Fernando Pardo Fincias * * Este fichero se distribuye bajo una licencia dúal: LGPL 3.0 y EUPL 1.1: * - GNU Lesser General Public License (LGPL), version 3.0 * - European Union Public Licence (EUPL), version 1.1 * ---------------------------------------------------------------------- * Si se decide por la licencia LGPL, se aplica el siguiente aviso: * * This program is free software: you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/ * * ----------------------------------------------------------------------* * Si se decide por la licencia EUPL se aplica este otro: * * Licencia con arreglo a la EUPL, Versión 1.1 exclusivamente (la "Licencia"); * Solo podrá usarse esta obra si se respeta la Licencia. * Puede obtenerse una copia de la Licencia en: * http://ec.europa.eu/idabc/eupl.html * El programa distribuido con arreglo a la Licencia se distribuye "TAL CUAL", * SIN GARANTÍAS NI CONDICIONES DE NINGÚN TIPO, ni expresas ni implícitas. * ----------------------------------------------------------------------* */ package org.dnieprov.driver; import java.util.Arrays; /** * * @author luis */ final class SecureByteBuffer { private byte[] buffer; private int freeIndex = 0; private static final int DEFAULT_CHUNK_SIZE = 1024; private static final int DEFAULT_INITIAL_CAPACITY = 1024; private int chunkSize = DEFAULT_CHUNK_SIZE; public SecureByteBuffer(int initialCapacity){ this(initialCapacity,DEFAULT_CHUNK_SIZE); } public SecureByteBuffer(){ this(DEFAULT_INITIAL_CAPACITY,DEFAULT_CHUNK_SIZE); } public SecureByteBuffer(int initialCapacity,int chunkSize){ buffer = new byte[initialCapacity]; this.chunkSize = (chunkSize < 0) ? DEFAULT_CHUNK_SIZE : chunkSize; } @Override protected void finalize() throws Throwable{ clear(); super.finalize(); } public void clearAll(){ Arrays.fill(buffer,(byte) 0x00); freeIndex = 0; } public void reset(){ freeIndex = 0; } public void clear(){ Arrays.fill(buffer, 0, freeIndex, (byte) 0x00); freeIndex = 0; } public void write(byte [] a){ write(a,0,a.length); } public void write(byte [] a,int from,int len){ int writeLen = len - from; if (writeLen > (buffer.length - freeIndex)){ int x = (writeLen) / chunkSize; int size = (x + 1) * chunkSize; byte [] bufferAux = new byte[buffer.length + size]; System.arraycopy(buffer,0,bufferAux,0,freeIndex); Arrays.fill(buffer, 0, freeIndex, (byte) 0x00); buffer = bufferAux; } System.arraycopy(a,from,buffer,freeIndex, writeLen); freeIndex += writeLen; } public byte [] getByteArray(){ return Arrays.copyOfRange(buffer, 0, freeIndex); } }