Java tutorial
//package com.java2s; /* Copyright (C) 2011 [Gobierno de Espana] * This file is part of "Cliente @Firma". * "Cliente @Firma" is free software; you can redistribute it and/or modify it under the terms of: * - the GNU General Public License as published by the Free Software Foundation; * either version 2 of the License, or (at your option) any later version. * - or The European Software License; either version 1.1 or (at your option) any later version. * Date: 11/01/11 * You may contact the copyright holder at: soporte.afirma5@mpt.es */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.util.logging.Logger; public class Main { private static final Logger LOGGER = Logger.getLogger("es.gob.afirma"); /** Carga una librería nativa del sistema. * @param path Ruta a la libreria de sistema. * @throws IOException Si ocurre algún problema durante la carga */ public static void loadNativeLibrary(final String path) throws IOException { if (path == null) { LOGGER.warning("No se puede cargar una biblioteca nula"); //$NON-NLS-1$ return; } final int pos = path.lastIndexOf('.'); final File file = new File(path); final File tempLibrary = File.createTempFile( pos < 1 ? file.getName() : file.getName().substring(0, file.getName().indexOf('.')), pos < 1 || pos == path.length() - 1 ? null : path.substring(pos)); // Copiamos el fichero copyFile(file, tempLibrary); // Pedimos borrar los temporales cuando se cierre la JVM if (tempLibrary != null) { tempLibrary.deleteOnExit(); } LOGGER.info("Cargamos " + (tempLibrary == null ? path : tempLibrary.getAbsolutePath())); //$NON-NLS-1$ System.load(tempLibrary != null ? tempLibrary.getAbsolutePath() : path); } /** Copia un fichero. * @param source Fichero origen con el contenido que queremos copiar. * @param dest Fichero destino de los datos. * @throws IOException SI ocurre algun problema durante la copia */ public static void copyFile(final File source, final File dest) throws IOException { if (source == null || dest == null) { throw new IllegalArgumentException("Ni origen ni destino de la copia pueden ser nulos"); //$NON-NLS-1$ } final FileInputStream is = new FileInputStream(source); final FileOutputStream os = new FileOutputStream(dest); final FileChannel in = is.getChannel(); final FileChannel out = os.getChannel(); final MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size()); out.write(buf); in.close(); out.close(); is.close(); os.close(); } }