Search in sources :

Example 16 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class UtilReflection method getMethod.

/**
 * Get method and call its return value with parameters.
 *
 * @param <T> The object type.
 * @param object The object caller (must not be <code>null</code>).
 * @param name The method name (must not be <code>null</code>).
 * @param params The method parameters (must not be <code>null</code>).
 * @return The value returned.
 * @throws LionEngineException If invalid parameters.
 */
public static <T> T getMethod(Object object, String name, Object... params) {
    Check.notNull(object);
    Check.notNull(name);
    Check.notNull(params);
    try {
        final Class<?> clazz = getClass(object);
        final Method method = clazz.getDeclaredMethod(name, getParamTypes(params));
        setAccessible(method, true);
        @SuppressWarnings("unchecked") final T value = (T) method.invoke(object, params);
        return value;
    } catch (final NoSuchMethodException | InvocationTargetException | IllegalAccessException exception) {
        throw new LionEngineException(exception, ERROR_METHOD + name);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 17 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class UtilStream method getCopy.

/**
 * Get of full copy of the input stream stored in a temporary file.
 *
 * @param name The file name reference, to have a similar temporary file name (must not be <code>null</code>).
 * @param input The input stream reference (must not be <code>null</code>).
 * @return The temporary file created with copied content from stream.
 * @throws LionEngineException If invalid arguments or invalid stream.
 */
public static File getCopy(String name, InputStream input) {
    Check.notNull(name);
    Check.notNull(input);
    final String prefix;
    final String suffix;
    final int minimumPrefix = 3;
    final int i = name.lastIndexOf(Constant.DOT);
    if (i > minimumPrefix) {
        prefix = name.substring(0, i);
        suffix = name.substring(i);
    } else {
        if (name.length() > minimumPrefix) {
            prefix = name;
        } else {
            prefix = PREFIX_TEMP;
        }
        suffix = null;
    }
    try {
        final File temp = File.createTempFile(prefix, suffix);
        try (OutputStream output = new BufferedOutputStream(new FileOutputStream(temp))) {
            copy(input, output);
        }
        return temp;
    } catch (final IOException exception) {
        throw new LionEngineException(exception, ERROR_TEMP_FILE + name);
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 18 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class UtilZip method getEntriesByExtension.

/**
 * Get all entries existing in the path considering the extension.
 *
 * @param jar The JAR to check (must not be <code>null</code>).
 * @param path The path to check (must not be <code>null</code>).
 * @param extension The extension without dot; eg: xml (must not be <code>null</code>).
 * @return The entries list.
 * @throws LionEngineException If invalid arguments or unable to open ZIP.
 */
public static Collection<ZipEntry> getEntriesByExtension(File jar, String path, String extension) {
    Check.notNull(jar);
    Check.notNull(path);
    Check.notNull(extension);
    try (ZipFile zip = new ZipFile(jar)) {
        return checkEntries(zip, path, extension);
    } catch (final IOException exception) {
        throw new LionEngineException(exception, ERROR_OPEN_ZIP + jar.getAbsolutePath());
    }
}
Also used : ZipFile(java.util.zip.ZipFile) LionEngineException(com.b3dgs.lionengine.LionEngineException) IOException(java.io.IOException)

Example 19 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class ExtractorModelTest method testEnumFail.

/**
 * Test with enum fail.
 *
 * @throws NoSuchFieldException If error.
 * @throws IllegalArgumentException If error.
 * @throws IllegalAccessException If error.
 */
@Test
public void testEnumFail() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    final ExtractorModel extractor = new ExtractorModel(services);
    final Field field = extractor.getClass().getDeclaredField("state");
    UtilReflection.setAccessible(field, true);
    field.set(extractor, ExtractorState.values()[5]);
    try {
        extractor.update(1.0);
        Assert.fail();
    } catch (final LionEngineException exception) {
        Assert.assertEquals("Unknown enum: FAIL", exception.getMessage());
    }
}
Also used : Field(java.lang.reflect.Field) LionEngineException(com.b3dgs.lionengine.LionEngineException) Test(org.junit.Test)

Example 20 with LionEngineException

use of com.b3dgs.lionengine.LionEngineException in project lionengine by b3dgs.

the class ClientImpl method connect.

/*
     * Client
     */
@Override
public void connect(String ip, int port) {
    Check.notNull(ip);
    Check.superiorOrEqual(port, 0);
    Check.inferiorOrEqual(port, Constant.MAX_PORT);
    try {
        socket = new Socket(InetAddress.getByName(ip), port);
        out = new ObjectOutputStream(socket.getOutputStream());
        in = new ObjectInputStream(socket.getInputStream());
        connected = true;
        clientId = -1;
        pingRequestTimer.start();
        bandwidthTimer.start();
    } catch (final IOException exception) {
        throw new LionEngineException(exception, "Cannot connect to the server !");
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Socket(java.net.Socket) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

LionEngineException (com.b3dgs.lionengine.LionEngineException)75 IOException (java.io.IOException)13 File (java.io.File)8 OutputStream (java.io.OutputStream)7 ArrayList (java.util.ArrayList)7 XmlReader (com.b3dgs.lionengine.XmlReader)6 Field (java.lang.reflect.Field)6 Test (org.junit.Test)6 Media (com.b3dgs.lionengine.Media)5 Configurer (com.b3dgs.lionengine.game.Configurer)4 UtilFile (com.b3dgs.lionengine.UtilFile)3 Graphic (com.b3dgs.lionengine.graphic.Graphic)3 UtilFile (com.b3dgs.lionengine.util.UtilFile)3 DisplayMode (java.awt.DisplayMode)3 BufferedImage (java.awt.image.BufferedImage)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 AfterAll (org.junit.jupiter.api.AfterAll)3 Animation (com.b3dgs.lionengine.Animation)2 AnimationConfig (com.b3dgs.lionengine.game.AnimationConfig)2 Feature (com.b3dgs.lionengine.game.Feature)2