Search in sources :

Example 51 with Scanner

use of java.util.Scanner in project camel by apache.

the class FileStateRepository method loadStore.

/**
     * Loads the given file store into the 1st level cache
     */
protected void loadStore() throws IOException {
    // auto create starting directory if needed
    if (!fileStore.exists()) {
        LOG.debug("Creating filestore: {}", fileStore);
        File parent = fileStore.getParentFile();
        if (parent != null) {
            parent.mkdirs();
        }
        boolean created = FileUtil.createNewFile(fileStore);
        if (!created) {
            throw new IOException("Cannot create filestore: " + fileStore);
        }
    }
    LOG.trace("Loading to 1st level cache from state filestore: {}", fileStore);
    cache.clear();
    Scanner scanner = null;
    try {
        scanner = new Scanner(fileStore);
        scanner.useDelimiter(STORE_DELIMITER);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            int separatorIndex = line.indexOf(KEY_VALUE_DELIMITER);
            String key = line.substring(0, separatorIndex);
            String value = line.substring(separatorIndex + KEY_VALUE_DELIMITER.length());
            cache.put(key, value);
        }
    } catch (IOException e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
    LOG.debug("Loaded {} to the 1st level cache from state filestore: {}", cache.size(), fileStore);
}
Also used : Scanner(java.util.Scanner) IOException(java.io.IOException) File(java.io.File)

Example 52 with Scanner

use of java.util.Scanner in project camel by apache.

the class GroupTokenIteratorTest method testGroupIteratorWithDifferentEncodingFromDefault.

public void testGroupIteratorWithDifferentEncodingFromDefault() throws Exception {
    if (Charset.defaultCharset() == StandardCharsets.UTF_8) {
        // can't think of test case where having default charset set to UTF-8 is affected
        return;
    }
    byte[] buf = "£1\n£2\n".getBytes(StandardCharsets.UTF_8);
    ByteArrayInputStream in = new ByteArrayInputStream(buf);
    Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.displayName());
    scanner.useDelimiter("\n");
    exchange.setProperty(Exchange.CHARSET_NAME, StandardCharsets.UTF_8.displayName());
    GroupTokenIterator gi = new GroupTokenIterator(exchange, scanner, "\n", 1, false);
    assertTrue(gi.hasNext());
    assertEquals("£1", gi.next());
    assertEquals("£2", gi.next());
    assertFalse(gi.hasNext());
    IOHelper.close(gi);
}
Also used : Scanner(java.util.Scanner) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 53 with Scanner

use of java.util.Scanner in project camel by apache.

the class BindyKeyValuePairDataFormat method unmarshal.

public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
    BindyKeyValuePairFactory factory = (BindyKeyValuePairFactory) getFactory();
    // List of Pojos
    List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();
    // Pojos of the model
    Map<String, Object> model;
    // Map to hold the model @OneToMany classes while binding
    Map<String, List<Object>> lists = new HashMap<String, List<Object>>();
    InputStreamReader in = new InputStreamReader(inputStream, IOHelper.getCharsetName(exchange));
    // Scanner is used to read big file
    Scanner scanner = new Scanner(in);
    // Retrieve the pair separator defined to split the record
    ObjectHelper.notNull(factory.getPairSeparator(), "The pair separator property of the annotation @Message");
    String separator = factory.getPairSeparator();
    int count = 0;
    try {
        while (scanner.hasNextLine()) {
            // Read the line
            String line = scanner.nextLine().trim();
            if (ObjectHelper.isEmpty(line)) {
                // skip if line is empty
                continue;
            }
            // Increment counter
            count++;
            // Create POJO
            model = factory.factory();
            // Split the message according to the pair separator defined in
            // annotated class @Message
            List<String> result = Arrays.asList(line.split(separator));
            if (result.size() == 0 || result.isEmpty()) {
                throw new java.lang.IllegalArgumentException("No records have been defined in the KVP");
            }
            if (result.size() > 0) {
                // Bind data from message with model classes
                // Counter is used to detect line where error occurs
                factory.bind(result, model, count, lists);
                // Link objects together
                factory.link(model);
                // Add objects graph to the list
                models.add(model);
                LOG.debug("Graph of objects created: {}", model);
            }
        }
        // If this is the case (correspond to an empty stream, ...)
        if (models.size() == 0) {
            throw new java.lang.IllegalArgumentException("No records have been defined in the CSV");
        } else {
            return extractUnmarshalResult(models);
        }
    } finally {
        scanner.close();
        IOHelper.close(in, "in", LOG);
    }
}
Also used : Scanner(java.util.Scanner) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BindyKeyValuePairFactory(org.apache.camel.dataformat.bindy.BindyKeyValuePairFactory) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 54 with Scanner

use of java.util.Scanner in project camel by apache.

the class ObjectHelper method getScanner.

/**
     * Creates a {@link Scanner} for scanning the given value.
     *
     * @param exchange  the current exchange
     * @param value     the value, typically the message IN body
     * @return the scanner, is newer <tt>null</tt>
     */
public static Scanner getScanner(Exchange exchange, Object value) {
    if (value instanceof WrappedFile) {
        WrappedFile<?> gf = (WrappedFile<?>) value;
        Object body = gf.getBody();
        if (body != null) {
            // we have loaded the file content into the body so use that
            value = body;
        } else {
            // generic file is just a wrapper for the real file so call again with the real file
            return getScanner(exchange, gf.getFile());
        }
    }
    String charset = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
    Scanner scanner = null;
    if (value instanceof Readable) {
        scanner = new Scanner((Readable) value);
    } else if (value instanceof InputStream) {
        scanner = charset == null ? new Scanner((InputStream) value) : new Scanner((InputStream) value, charset);
    } else if (value instanceof File) {
        try {
            scanner = charset == null ? new Scanner((File) value) : new Scanner((File) value, charset);
        } catch (FileNotFoundException e) {
            throw new RuntimeCamelException(e);
        }
    } else if (value instanceof String) {
        scanner = new Scanner((String) value);
    } else if (value instanceof ReadableByteChannel) {
        scanner = charset == null ? new Scanner((ReadableByteChannel) value) : new Scanner((ReadableByteChannel) value, charset);
    }
    if (scanner == null) {
        // value is not a suitable type, try to convert value to a string
        String text = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, value);
        if (text != null) {
            scanner = new Scanner(text);
        }
    }
    if (scanner == null) {
        scanner = new Scanner("");
    }
    return scanner;
}
Also used : Scanner(java.util.Scanner) ReadableByteChannel(java.nio.channels.ReadableByteChannel) WrappedFile(org.apache.camel.WrappedFile) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile)

Example 55 with Scanner

use of java.util.Scanner in project realm-java by realm.

the class RealmTests method utf8Tests.

@Test
public void utf8Tests() {
    realm.beginTransaction();
    realm.delete(AllTypes.class);
    realm.commitTransaction();
    String file = "assets/unicode_codepoints.csv";
    Scanner scanner = new Scanner(getClass().getClassLoader().getResourceAsStream(file), "UTF-8");
    int i = 0;
    String currentUnicode = null;
    try {
        realm.beginTransaction();
        while (scanner.hasNextLine()) {
            currentUnicode = scanner.nextLine();
            char[] chars = Character.toChars(Integer.parseInt(currentUnicode, 16));
            String codePoint = new String(chars);
            AllTypes o = realm.createObject(AllTypes.class);
            o.setColumnLong(i);
            o.setColumnString(codePoint);
            AllTypes realmType = realm.where(AllTypes.class).equalTo("columnLong", i).findFirst();
            if (i > 1) {
                assertEquals("Codepoint: " + i + " / " + currentUnicode, codePoint, // codepoint 0 is NULL, ignore for now.
                realmType.getColumnString());
            }
            i++;
        }
        realm.commitTransaction();
    } catch (Exception e) {
        fail("Failure, Codepoint: " + i + " / " + currentUnicode + " " + e.getMessage());
    }
}
Also used : Scanner(java.util.Scanner) AllTypes(io.realm.entities.AllTypes) PrimaryKeyRequiredAsString(io.realm.entities.PrimaryKeyRequiredAsString) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) JSONException(org.json.JSONException) RealmPrimaryKeyConstraintException(io.realm.exceptions.RealmPrimaryKeyConstraintException) RealmFileException(io.realm.exceptions.RealmFileException) RealmException(io.realm.exceptions.RealmException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

Scanner (java.util.Scanner)862 File (java.io.File)135 ArrayList (java.util.ArrayList)77 IOException (java.io.IOException)72 Test (org.junit.Test)70 NoSuchElementException (java.util.NoSuchElementException)59 InputStream (java.io.InputStream)54 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)49 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)49 Response (com.github.scribejava.core.model.Response)49 FileNotFoundException (java.io.FileNotFoundException)47 InputMismatchException (java.util.InputMismatchException)47 HashMap (java.util.HashMap)35 FileInputStream (java.io.FileInputStream)33 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)31 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)29 Locale (java.util.Locale)24 BigInteger (java.math.BigInteger)23 List (java.util.List)22 OAuth1AccessToken (com.github.scribejava.core.model.OAuth1AccessToken)18