Search in sources :

Example 1 with InputMismatchException

use of java.util.InputMismatchException in project jvm-serializers by eishay.

the class Image method unmarshal.

/**
	 * Deserializes the object.
	 * @param buf the data source.
	 * @param offset the initial index for {@code buf}, inclusive.
	 * @return the final index for {@code buf}, exclusive.
	 * @throws BufferUnderflowException when {@code buf} is incomplete. (EOF)
	 * @throws SecurityException on an upper limit breach defined by either {@link #colferSizeMax} or {@link #colferListMax}.
	 * @throws InputMismatchException when the data does not match this object's schema.
	 */
public int unmarshal(byte[] buf, int offset) {
    int i = offset;
    try {
        byte header = buf[i++];
        if (header == (byte) 0) {
            int size = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                size |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            if (size > colferSizeMax)
                throw new SecurityException(format("colfer: field serializers/colfer/media.image.uri size %d exceeds %d UTF-8 bytes", size, colferSizeMax));
            int start = i;
            i += size;
            this.uri = new String(buf, start, size, this._utf8);
            header = buf[i++];
        }
        if (header == (byte) 1) {
            int size = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                size |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            if (size > colferSizeMax)
                throw new SecurityException(format("colfer: field serializers/colfer/media.image.title size %d exceeds %d UTF-8 bytes", size, colferSizeMax));
            int start = i;
            i += size;
            this.title = new String(buf, start, size, this._utf8);
            header = buf[i++];
        }
        if (header == (byte) 2) {
            int x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                x |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            this.width = x;
            header = buf[i++];
        } else if (header == (byte) (2 | 0x80)) {
            int x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                x |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            this.width = -x;
            header = buf[i++];
        }
        if (header == (byte) 3) {
            int x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                x |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            this.height = x;
            header = buf[i++];
        } else if (header == (byte) (3 | 0x80)) {
            int x = 0;
            for (int shift = 0; true; shift += 7) {
                byte b = buf[i++];
                x |= (b & 0x7f) << shift;
                if (shift == 28 || b >= 0)
                    break;
            }
            this.height = -x;
            header = buf[i++];
        }
        if (header == (byte) 4) {
            this.small = true;
            header = buf[i++];
        }
        if (header == (byte) 5) {
            this.large = true;
            header = buf[i++];
        }
        if (header != (byte) 0x7f)
            throw new InputMismatchException(format("colfer: unknown header at byte %d", i - 1));
    } catch (IndexOutOfBoundsException e) {
        if (i - offset > colferSizeMax)
            throw new SecurityException(format("colfer: serial exceeds %d bytes", colferSizeMax));
        if (i >= buf.length)
            throw new BufferUnderflowException();
        throw new RuntimeException("colfer: bug", e);
    }
    if (i - offset > colferSizeMax)
        throw new SecurityException(format("colfer: serial exceeds %d bytes", colferSizeMax));
    return i;
}
Also used : InputMismatchException(java.util.InputMismatchException) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 2 with InputMismatchException

use of java.util.InputMismatchException in project hibernate-orm by hibernate.

the class TestConsole method start.

private void start() {
    Scanner scanner = new Scanner(System.in);
    PrintStream out = System.out;
    while (true) {
        out.println("-----------------------------------------------");
        out.println("1 - list persons             5 - list addresses");
        out.println("2 - list person history      6 - list addresses history");
        out.println("3 - new person               7 - new address");
        out.println("4 - modify person            8 - modify address");
        out.println("9 - get person at revision  10 - get address at revision");
        out.println("                             0 - end");
        try {
            int choice = scanner.nextInt();
            scanner.nextLine();
            entityManager.getTransaction().begin();
            StringBuilder sb;
            int personId;
            int addressId;
            int revision;
            switch(choice) {
                case 1:
                    sb = new StringBuilder();
                    printPersons(sb);
                    out.println(sb.toString());
                    break;
                case 2:
                    out.print("Person id: ");
                    personId = scanner.nextInt();
                    scanner.nextLine();
                    sb = new StringBuilder();
                    printPersonHistory(sb, personId);
                    out.println(sb.toString());
                    break;
                case 3:
                    Person p = readNewPerson(out, scanner);
                    entityManager.persist(p);
                    break;
                case 4:
                    out.print("Person id: ");
                    personId = scanner.nextInt();
                    scanner.nextLine();
                    readModifyPerson(out, scanner, personId);
                    break;
                case 5:
                    sb = new StringBuilder();
                    printAddresses(sb);
                    out.println(sb.toString());
                    break;
                case 6:
                    out.print("Address id: ");
                    addressId = scanner.nextInt();
                    scanner.nextLine();
                    sb = new StringBuilder();
                    printAddressHistory(sb, addressId);
                    out.println(sb.toString());
                    break;
                case 7:
                    Address a = readNewAddress(out, scanner);
                    entityManager.persist(a);
                    break;
                case 8:
                    out.print("Address id: ");
                    addressId = scanner.nextInt();
                    scanner.nextLine();
                    readModifyAddress(out, scanner, addressId);
                    break;
                case 9:
                    out.print("Person id: ");
                    personId = scanner.nextInt();
                    scanner.nextLine();
                    out.print("Revision number: ");
                    revision = scanner.nextInt();
                    scanner.nextLine();
                    if (revision <= 0) {
                        System.out.println("Revision must be greater then 0!");
                        continue;
                    }
                    sb = new StringBuilder();
                    printPersonAtRevision(sb, personId, revision);
                    out.println(sb.toString());
                    break;
                case 10:
                    out.print("Address id: ");
                    addressId = scanner.nextInt();
                    scanner.nextLine();
                    out.print("Revision number: ");
                    revision = scanner.nextInt();
                    scanner.nextLine();
                    if (revision <= 0) {
                        System.out.println("Revision must be greater then 0!");
                        continue;
                    }
                    sb = new StringBuilder();
                    printAddressAtRevision(sb, addressId, revision);
                    out.println(sb.toString());
                    break;
                case 0:
                    return;
            }
        } catch (InputMismatchException e) {
        // continuing
        } finally {
            entityManager.getTransaction().commit();
        }
    }
}
Also used : Scanner(java.util.Scanner) PrintStream(java.io.PrintStream) InputMismatchException(java.util.InputMismatchException)

Example 3 with InputMismatchException

use of java.util.InputMismatchException in project GDSC-SMLM by aherbert.

the class PeakResultsReader method createPeakResultDeviationsV1.

private PeakResult createPeakResultDeviationsV1(String line) {
    try {
        float[] params = new float[7];
        float[] paramsStdDev = new float[7];
        if (isUseScanner()) {
            // Code using a Scanner
            Scanner scanner = new Scanner(line);
            scanner.useDelimiter(tabPattern);
            scanner.useLocale(Locale.US);
            int id = 0, endPeak = 0;
            if (readId)
                id = scanner.nextInt();
            int peak = scanner.nextInt();
            if (readEndFrame)
                endPeak = scanner.nextInt();
            int origX = scanner.nextInt();
            int origY = scanner.nextInt();
            float origValue = scanner.nextFloat();
            double chiSquared = scanner.nextDouble();
            float noise = scanner.nextFloat();
            // Ignored but must be read
            float signal = scanner.nextFloat();
            for (int i = 0; i < params.length; i++) {
                params[i] = scanner.nextFloat();
                paramsStdDev[i] = scanner.nextFloat();
            }
            params[Gaussian2DFunction.SIGNAL] = signal;
            scanner.close();
            if (readId || readEndFrame)
                return new ExtendedPeakResult(peak, origX, origY, origValue, chiSquared, noise, params, paramsStdDev, endPeak, id);
            else
                return new PeakResult(peak, origX, origY, origValue, chiSquared, noise, params, paramsStdDev);
        } else {
            // JUnit test shows this is faster than the scanner
            // Code using split and parse
            String[] fields = tabPattern.split(line);
            int j = 0;
            int id = (readId) ? Integer.parseInt(fields[j++]) : 0;
            int peak = Integer.parseInt(fields[j++]);
            int endPeak = (readEndFrame) ? Integer.parseInt(fields[j++]) : 0;
            int origX = Integer.parseInt(fields[j++]);
            int origY = Integer.parseInt(fields[j++]);
            float origValue = Float.parseFloat(fields[j++]);
            double chiSquared = Double.parseDouble(fields[j++]);
            float noise = Float.parseFloat(fields[j++]);
            float signal = Float.parseFloat(fields[j++]);
            for (int i = 0; i < params.length; i++) {
                params[i] = Float.parseFloat(fields[j++]);
                paramsStdDev[i] = Float.parseFloat(fields[j++]);
            }
            params[Gaussian2DFunction.SIGNAL] = signal;
            if (readId || readEndFrame)
                return new ExtendedPeakResult(peak, origX, origY, origValue, chiSquared, noise, params, paramsStdDev, endPeak, id);
            else
                return new PeakResult(peak, origX, origY, origValue, chiSquared, noise, params, paramsStdDev);
        }
    } catch (InputMismatchException e) {
    } catch (NoSuchElementException e) {
    } catch (IndexOutOfBoundsException e) {
    } catch (NumberFormatException e) {
    }
    return null;
}
Also used : Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with InputMismatchException

use of java.util.InputMismatchException in project GDSC-SMLM by aherbert.

the class PeakResultsReader method createMALKResult.

private PeakResult createMALKResult(String line) {
    try {
        float[] params = new float[7];
        // To avoid problems handling the data
        params[Gaussian2DFunction.X_SD] = params[Gaussian2DFunction.Y_SD] = 1;
        if (isUseScanner()) {
            // Code using a Scanner
            Scanner scanner = new Scanner(line);
            scanner.useDelimiter(whitespacePattern);
            scanner.useLocale(Locale.US);
            params[Gaussian2DFunction.X_POSITION] = scanner.nextFloat();
            params[Gaussian2DFunction.Y_POSITION] = scanner.nextFloat();
            int peak = scanner.nextInt();
            params[Gaussian2DFunction.SIGNAL] = scanner.nextFloat();
            scanner.close();
            return new PeakResult(peak, 0, 0, 0, 0, 0, params, null);
        } else {
            // Code using split and parse
            String[] fields = whitespacePattern.split(line);
            params[Gaussian2DFunction.X_POSITION] = Float.parseFloat(fields[0]);
            params[Gaussian2DFunction.Y_POSITION] = Float.parseFloat(fields[1]);
            int peak = Integer.parseInt(fields[2]);
            params[Gaussian2DFunction.SIGNAL] = Float.parseFloat(fields[3]);
            return new PeakResult(peak, 0, 0, 0, 0, 0, params, null);
        }
    } catch (InputMismatchException e) {
    } catch (NoSuchElementException e) {
    } catch (IndexOutOfBoundsException e) {
    } catch (NumberFormatException e) {
    }
    return null;
}
Also used : Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Example 5 with InputMismatchException

use of java.util.InputMismatchException in project GDSC-SMLM by aherbert.

the class PeakResultsReader method createTableResultV1.

private PeakResult createTableResultV1(String line) {
    // [Precision] 
    try {
        Scanner scanner = new Scanner(line);
        scanner.useDelimiter(tabPattern);
        scanner.useLocale(Locale.US);
        int id = 0, endPeak = 0;
        if (readId)
            id = scanner.nextInt();
        if (readSource)
            scanner.next();
        int peak = scanner.nextInt();
        if (readEndFrame)
            endPeak = scanner.nextInt();
        int origX = scanner.nextInt();
        int origY = scanner.nextInt();
        float origValue = scanner.nextFloat();
        double error = scanner.nextDouble();
        float noise = scanner.nextFloat();
        @SuppressWarnings("unused") float // Ignored but must be read
        signal = scanner.nextFloat();
        @SuppressWarnings("unused") float // Ignored but must be read
        snr = scanner.nextFloat();
        float[] params = new float[7];
        float[] paramsStdDev = (deviations) ? new float[7] : null;
        for (int i = 0; i < params.length; i++) {
            params[i] = scanner.nextFloat();
            if (deviations)
                paramsStdDev[i] = scanner.nextFloat();
        }
        scanner.close();
        // For the new format we store the signal (not the amplitude).
        // Convert the amplitude into a signal
        params[Gaussian2DFunction.SIGNAL] *= 2 * Math.PI * params[Gaussian2DFunction.X_SD] * params[Gaussian2DFunction.Y_SD];
        if (readId || readEndFrame)
            return new ExtendedPeakResult(peak, origX, origY, origValue, error, noise, params, paramsStdDev, endPeak, id);
        else
            return new PeakResult(peak, origX, origY, origValue, error, noise, params, paramsStdDev);
    } catch (InputMismatchException e) {
    } catch (NoSuchElementException e) {
    }
    return null;
}
Also used : Scanner(java.util.Scanner) InputMismatchException(java.util.InputMismatchException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

InputMismatchException (java.util.InputMismatchException)83 Scanner (java.util.Scanner)56 NoSuchElementException (java.util.NoSuchElementException)47 Locale (java.util.Locale)23 BufferUnderflowException (java.nio.BufferUnderflowException)5 Pattern (java.util.regex.Pattern)5 BufferedReader (java.io.BufferedReader)4 BigInteger (java.math.BigInteger)4 UnicodeReader (gdsc.core.utils.UnicodeReader)3 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 TodoList (de.djuelg.neuronizer.domain.model.preview.TodoList)2 TodoListHeader (de.djuelg.neuronizer.domain.model.todolist.TodoListHeader)2 Point (java.awt.Point)2 Serializable (java.io.Serializable)2 BigDecimal (java.math.BigDecimal)2 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1