List of usage examples for java.util Scanner Scanner
public Scanner(ReadableByteChannel source, Charset charset)
From source file:org.terracotta.management.registry.ManagementRegistryTest.java
@Test public void test_management_registry_exposes() throws IOException { ManagementRegistry registry = new AbstractManagementRegistry() { @Override/*from w ww .j a v a2 s.co m*/ public ContextContainer getContextContainer() { return new ContextContainer("cacheManagerName", "my-cm-name"); } }; registry.addManagementProvider(new MyManagementProvider()); registry.register(new MyObject("myCacheManagerName", "myCacheName1")); registry.register(new MyObject("myCacheManagerName", "myCacheName2")); Scanner scanner = new Scanner(ManagementRegistryTest.class.getResourceAsStream("/capabilities.json"), "UTF-8").useDelimiter("\\A"); String expectedJson = scanner.next(); scanner.close(); ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); mapper.addMixIn(CapabilityContext.class, CapabilityContextMixin.class); String actual = mapper.writeValueAsString(registry.getCapabilities()); System.out.println(expectedJson); System.out.println(actual); assertEquals(expectedJson, actual); ContextualReturn<?> cr = registry.withCapability("TheActionProvider") .call("incr", int.class, new Parameter(Integer.MAX_VALUE, "int")).on(Context.empty() .with("cacheManagerName", "myCacheManagerName").with("cacheName", "myCacheName1")) .build().execute().getSingleResult(); try { cr.getValue(); fail(); } catch (ExecutionException e) { assertEquals(IllegalArgumentException.class, e.getCause().getClass()); } }
From source file:com.mgmtp.perfload.perfalyzer.binning.Binner.java
/** * Performs the binning operation on the specified file. * * @param file// w w w . j ava 2s .c o m * the file to be binned and/or aggregated; must be relative to the source directory */ public void binFile(final PerfAlyzerFile file) throws IOException { FileOutputStream fos = null; try (FileInputStream fis = new FileInputStream(new File(sourceDir, file.getFile().getPath())); ChannelManager channelManager = new ChannelManager(destDir, channelKey -> file.copy().addFileNamePart(channelKey))) { Scanner scanner = new Scanner(fis.getChannel(), Charsets.UTF_8.name()); if (binningStrategy.needsBinning()) { File destFile = new File(destDir, binningStrategy.transformDefautBinnedFilePath(file)); Files.createParentDirs(destFile); fos = new FileOutputStream(destFile); binningStrategy.binData(scanner, fos.getChannel()); } else { // if binning is not necessary, no channel is provided binningStrategy.binData(scanner, null); } binningStrategy.aggregateData(channelManager); } finally { closeQuietly(fos); } }
From source file:com.joliciel.talismane.machineLearning.ModelFactory.java
public MachineLearningModel getMachineLearningModel(ZipInputStream zis) { try {/* ww w . j a va2 s.co m*/ MachineLearningModel machineLearningModel = null; ZipEntry ze = zis.getNextEntry(); if (!ze.getName().equals("algorithm.txt")) { throw new JolicielException("Expected algorithm.txt as first entry in zip. Was: " + ze.getName()); } // note: assuming the model type will always be the first entry Scanner typeScanner = new Scanner(zis, "UTF-8"); MachineLearningAlgorithm algorithm = MachineLearningAlgorithm.MaxEnt; if (typeScanner.hasNextLine()) { String algorithmString = typeScanner.nextLine(); try { algorithm = MachineLearningAlgorithm.valueOf(algorithmString); } catch (IllegalArgumentException iae) { LogUtils.logError(LOG, iae); throw new JolicielException("Unknown algorithm: " + algorithmString); } } else { throw new JolicielException("Cannot find algorithm in zip file"); } switch (algorithm) { case MaxEnt: machineLearningModel = maxentService.getMaxentModel(); break; case LinearSVM: machineLearningModel = linearSVMService.getLinearSVMModel(); break; case Perceptron: machineLearningModel = perceptronService.getPerceptronModel(); break; case PerceptronRanking: machineLearningModel = perceptronService.getPerceptronRankingModel(); break; case OpenNLPPerceptron: machineLearningModel = maxentService.getPerceptronModel(); break; default: throw new JolicielException("Machine learning algorithm not yet supported: " + algorithm); } while ((ze = zis.getNextEntry()) != null) { LOG.debug(ze.getName()); machineLearningModel.loadZipEntry(zis, ze); } // next zip entry machineLearningModel.onLoadComplete(); return machineLearningModel; } catch (IOException ioe) { LogUtils.logError(LOG, ioe); throw new RuntimeException(ioe); } finally { try { zis.close(); } catch (IOException ioe) { LogUtils.logError(LOG, ioe); } } }
From source file:org.opens.urlmanager.it.utils.AHttpRequestBasedTest.java
protected String getInputStreamContent(InputStream is) throws Exception { try {/*from w ww .j a v a2s. co m*/ return new Scanner(is, "UTF-8").useDelimiter("\\A").next(); } catch (NoSuchElementException e) { return ""; } }
From source file:org.geppetto.core.model.services.OBJModelInterpreterService.java
@Override public IModel readModel(URL url, List<URL> recordings, String instancePath) throws ModelInterpreterException { ModelWrapper collada = new ModelWrapper(instancePath); try {/*from w ww . j a v a 2 s . com*/ Scanner scanner = new Scanner(url.openStream(), "UTF-8"); String objContent = scanner.useDelimiter("\\A").next(); scanner.close(); collada.wrapModel(OBJ, objContent); } catch (IOException e) { throw new ModelInterpreterException(e); } return collada; }