List of usage examples for org.apache.commons.io IOUtils toCharArray
public static char[] toCharArray(InputStream is, String encoding) throws IOException
InputStream
as a character array using the specified character encoding. From source file:de.saly.json.jsr353.benchmark.data.Buffers.java
private static char[] readChars(final int count, final Charset charset) { InputStream in = null;/* w w w. j a va 2s.c o m*/ try { in = new FileInputStream( "./generated/generated_benchmark_test_file_" + charset.name() + "_" + count + ".json"); return IOUtils.toCharArray(in, charset); } catch (final IOException e) { return null; } finally { IOUtils.closeQuietly(in); } }
From source file:eu.brokeratcloud.rest.gui.AdminFacingComponent.java
@POST @Path("/importBrokerPolicy") @Consumes("multipart/form-data") public Response importBrokerPolicy(MultipartFormDataInput input) { String fileName = ""; Map<String, List<InputPart>> uploadForm = input.getFormDataMap(); List<InputPart> inputParts_0 = uploadForm.get("import_mode"); List<InputPart> inputParts_1 = uploadForm.get("file"); // Get import mode String importMode = null;/*from ww w .ja v a 2 s .c om*/ String method = null; for (InputPart inputPart : inputParts_0) { try { //get value importMode = inputPart.getBodyAsString(); logger.debug("importBrokerPolicy: import mode={}", importMode); } catch (IOException e) { logger.error("importBrokerPolicy: Error while retrieving import mode: {}", e); } } if (importMode != null) { importMode = importMode.trim(); if (importMode.equalsIgnoreCase("append")) { importMode = "append"; method = "POST"; } else if (importMode.equalsIgnoreCase("replace")) { importMode = "replace"; method = "PUT"; } else { importMode = null; logger.error("importBrokerPolicy: Invalid import mode={}", importMode); } } logger.debug("importBrokerPolicy: import mode={}", importMode); // Get file contents String contents = null; for (InputPart inputPart : inputParts_1) { try { MultivaluedMap<String, String> header = inputPart.getHeaders(); fileName = getFileName(header); //convert the uploaded file to inputstream InputStream inputStream = inputPart.getBody(InputStream.class, null); char[] bytes = IOUtils.toCharArray(inputStream, "UTF-8"); contents = new String(bytes); logger.debug("importBrokerPolicy: file-contents=\n{}", contents); //writeFile(UPLOADED_FILE_PATH+"/"+fileName, contents); } catch (IOException e) { logger.error("importBrokerPolicy: Error while retrieving upload file contents: {}", e); } } // if all parameters are ok upload file to RDF repository int status = 400; if (importMode != null && contents != null) { String rdfReposUrl = this.configProperties.getProperty("triplestore-upload-uri"); logger.info( "importBrokerPolicy: Upload to RDF repository: STARTS: filename=\"{}\", action=\"{} {}\"...", fileName, method, rdfReposUrl); ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyWebTarget target = client.target(rdfReposUrl); long callStartTm = System.currentTimeMillis(); javax.ws.rs.client.Entity entity = Entity.entity(contents, new javax.ws.rs.core.MediaType("text", "turtle")); Response response = importMode.equals("append") ? target.request().post(entity) : target.request().put(entity); long callEndTm = System.currentTimeMillis(); status = response.getStatus(); logger.debug("Response: {}", status); logger.info("importBrokerPolicy: Upload to RDF repository: DONE: SPARQL server status={}", status); if (status >= 200 && status < 300) { return Response.status(200).header("Connection", "Keep-Alive") .header("Keep-Alive", "timeout=600, max=99") .entity(String.format("{ \"message\": \"File upload completed: status=%d\" }", status)) .build(); } } else { logger.debug("importBrokerPolicy: Missing parameter: import-mode={}, file-contents={}", importMode, contents); } return Response.status(status).header("Connection", "Keep-Alive") .header("Keep-Alive", "timeout=600, max=99").entity(String .format("{ \"message\": \"File upload failed: See logs for details: status=%d\" }", status)) .build(); }
From source file:org.jspringbot.keyword.json.JSONHelper.java
public void setJsonString(String jsonString) throws IOException { if (StringUtils.startsWith(jsonString, "file:") || StringUtils.startsWith(jsonString, "classpath:")) { ResourceEditor editor = new ResourceEditor(); editor.setAsText(jsonString);//from w w w .ja v a 2s. c o m Resource resource = (Resource) editor.getValue(); jsonString = new String(IOUtils.toCharArray(resource.getInputStream(), CharEncoding.UTF_8)); } if (!StringUtils.startsWith(jsonString, "[") || !StringUtils.startsWith(jsonString, "{")) { // LOG.warn("jsonString starts with an invalid character. trying to recover..."); for (int i = 0; i < jsonString.length(); i++) { if (jsonString.charAt(i) == '{' || jsonString.charAt(i) == '[') { jsonString = jsonString.substring(i); break; } } } try { LOG.createAppender().appendBold("JSON String:").appendJSON(prettyPrint(jsonString)).log(); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } this.jsonString = jsonString; }
From source file:org.sonar.flex.ParserAdapter2.java
private static char[] fileToCharArray(File file, Charset charset) { FileInputStream is = null;/*from w w w . j av a 2 s. c om*/ try { is = new FileInputStream(file); return IOUtils.toCharArray(is, charset.name()); } catch (IOException e) { throw new RecognitionException(0, e.getMessage(), e); } finally { IOUtils.closeQuietly(is); } }
From source file:spoon.support.compiler.jdt.FileCompilerConfig.java
@Override public void initializeCompiler(JDTBatchCompiler compiler) { JDTBasedSpoonCompiler jdtCompiler = compiler.getJdtCompiler(); List<CompilationUnit> cuList = new ArrayList<>(); InputStream inputStream = null; try {// www . j a v a2s.c o m for (SpoonFile f : getFiles(compiler)) { if (compiler.filesToBeIgnored.contains(f.getPath())) { continue; } String fName = f.isActualFile() ? f.getPath() : f.getName(); inputStream = f.getContent(); char[] content = IOUtils.toCharArray(inputStream, jdtCompiler.encoding); cuList.add(new CompilationUnit(content, fName, null)); IOUtils.closeQuietly(inputStream); } } catch (Exception e) { IOUtils.closeQuietly(inputStream); throw new SpoonException(e); } compiler.setCompilationUnits(cuList.toArray(new CompilationUnit[0])); }