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);
}
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);
}
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);
}
}
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;
}
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());
}
}
Aggregations