private String getDescriptorContent(File jsonFile) { try (InputStream stream = new FileInputStream(jsonFile)) { return IOUtils.toString(stream); } catch (IOException e) { throw new IllegalArgumentException(format("Could not read extension describer on artifact '%s'", jsonFile.getAbsolutePath()), e); } }
@Test public void decodeWithoutUnzipping() throws Exception { final String payload = RandomStringUtils.randomAlphabetic(1024); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(payload.getBytes()); GZIPCompressorInputStream gzipCompressorInputStream = new GZIPCompressorInputStream(byteArrayInputStream); String encoded = Base64.encodeBytes(IOUtils.toByteArray(gzipCompressorInputStream), Base64.DONT_BREAK_LINES); GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(Base64.decodeWithoutUnzipping(encoded))); assertThat(IOUtils.toString(gzipInputStream), is(payload)); } }
protected String loadResourceAsString(String resourceName) throws IOException { return IOUtils.getResourceAsString(resourceName, getClass()); }
MulePluginBasedLoaderFinder(InputStream json) { try { this.mulePlugin = mulePluginSerializer.deserialize(IOUtils.toString(json)); } finally { closeQuietly(json); } }
private <T> T createCustomInstance(String classLocation) { InputStream classStream = null; try { classStream = this.getClass().getResourceAsStream(classLocation); byte[] classBytes = IOUtils.toByteArray(classStream); classStream.close(); Class clazz = this.defineClass(null, classBytes, 0, classBytes.length); return (T) clazz.newInstance(); } catch (Exception e) { throw new RuntimeException("Can not create instance from resource: " + classLocation, e); } finally { closeQuietly(classStream); } }
public static boolean compare(InputStream input1, InputStream input2) { byte[] bytes1 = IOUtils.toByteArray(input1); byte[] bytes2 = IOUtils.toByteArray(input2); return Arrays.equals(bytes1, bytes2); } }
private void discoverCustomPropertyEditor() { customPropertyEditorsCache = new HashMap<>(); for (ClassLoader classLoader : resolveContextArtifactPluginClassLoaders()) { // Look for any editors needed by extensions try { Enumeration<URL> urls = classLoader.getResources(CUSTOM_PROPERTY_EDITOR_RESOURCE_NAME); while (urls.hasMoreElements()) { URL url = urls.nextElement(); Properties props = new Properties(); InputStream stream = url.openStream(); try { props.load(stream); for (Map.Entry<Object, Object> entry : props.entrySet()) { String target = (String) entry.getKey(); String editor = (String) entry.getValue(); Class<?> requiredType = classLoader.loadClass(target); Class<PropertyEditor> propertyEditorClass = (Class<PropertyEditor>) classLoader.loadClass(editor); customPropertyEditorsCache.put(requiredType, propertyEditorClass); } } finally { closeQuietly(stream); } } } catch (Exception e) { throw new IllegalStateException("Error loading custom property editors", e); } } } }
private File getGenerationTargetDirectory() { URL url = getResourceAsUrl(getEffectiveConfigFile(), getClass(), true, true); File targetDirectory = new File(FileUtils.toFile(url).getParentFile(), "META-INF"); if (!targetDirectory.exists() && !targetDirectory.mkdir()) { throw new RuntimeException("Could not create target directory " + targetDirectory.getAbsolutePath()); } return targetDirectory; }
private String handleStream(InputStream input) { String string; try { string = IOUtils.toString(input); } finally { closeQuietly(input); } return string; }
/** * {@inheritDoc} */ @Override protected byte[] doSerialize(Object object) throws Exception { //TODO: MULE-11939 if (object instanceof CursorStreamProvider) { try (CursorStream cursor = ((CursorStreamProvider) object).openCursor()) { object = toByteArray(cursor); } } validateForSerialization(object); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(512); try (ObjectOutputStream out = new ArtifactClassLoaderObjectOutputStream(classLoaderRepository, outputStream)) { out.writeObject(object); } catch (IOException ex) { throw new SerializationException("Cannot serialize object", ex); } return outputStream.toByteArray(); }
public static synchronized void copyStreamToFile(InputStream input, File destination) throws IOException { if (destination.exists() && !destination.canWrite()) { throw new IOException("Destination file does not exist or is not writeable"); } try { FileOutputStream output = new FileOutputStream(destination); try { copy(input, output); } finally { IOUtils.closeQuietly(output); } } finally { IOUtils.closeQuietly(input); } }
private byte[] handleStream(InputStream input) { try { return toByteArray(input); } finally { closeQuietly(input); } } }
private void setupUserManagerFactory(FtpServerFactory serverFactory) throws IOException, URISyntaxException { PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); URL usersFile = IOUtils.getResourceAsUrl("users.properties", getClass()); if (usersFile == null) { throw new IOException("users.properties file not found in the classpath"); } userManagerFactory.setFile(new File(usersFile.toURI())); serverFactory.setUserManager(userManagerFactory.createUserManager()); }
@Override public MessageDispatcher connect() throws ConnectionException { return request -> { try { URL resource = Thread.currentThread().getContextClassLoader().getResource("test-http-response.xml"); String response = String.format(IOUtils.toString(resource.openStream()), getResponseWord()); return new DispatchingResponse(new ByteArrayInputStream(response.getBytes()), singletonMap("Content-Type", "text/xml")); } catch (Exception e) { throw new RuntimeException("Something went wrong when getting fake test response", e); } }; }
protected String toString(Object value) { if (value == null) { return null; } if (value instanceof Message) { value = ((Message) value).getPayload().getValue(); } if (value instanceof String) { return (String) value; } InputStream inputStream; if (value instanceof CursorStreamProvider) { inputStream = ((CursorStreamProvider) value).openCursor(); } else if (value instanceof InputStream) { inputStream = (InputStream) value; } else { throw new IllegalArgumentException("Result was not of expected type"); } try { return IOUtils.toString(inputStream); } finally { closeQuietly(inputStream); } }
@Test public void decodeWithoutUnzipping() throws Exception { final String payload = RandomStringUtils.randomAlphabetic(1024); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(payload.getBytes()); GZIPCompressorInputStream gzipCompressorInputStream = new GZIPCompressorInputStream(byteArrayInputStream); String encoded = Base64.encodeBytes(IOUtils.toByteArray(gzipCompressorInputStream), Base64.DONT_BREAK_LINES); GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(Base64.decodeWithoutUnzipping(encoded))); assertThat(IOUtils.toString(gzipInputStream), is(payload)); } }
public HttpMessage(HttpExchange httpExchange) { this.body = IOUtils.toByteArray(httpExchange.getRequestBody()); ImmutableMultimap.Builder<String, String> headersBuilder = ImmutableMultimap.builder(); Set<String> headerNames = httpExchange.getRequestHeaders().keySet(); headerNames.stream() .forEach(headerName -> headersBuilder.putAll(headerName, httpExchange.getRequestHeaders().get(headerName))); this.headers = headersBuilder.build(); uri = httpExchange.getRequestURI().getPath(); queryParams = queryToMap(httpExchange.getRequestURI().getQuery()); }
@Override public String getObject() throws Exception { String returnData = content; if (file != null) { try { returnData = getResourceAsString(file, getClass()); } catch (IOException e) { throw new MuleRuntimeException(createStaticMessage("Failed to load test-data resource: " + file), e); } } return returnData; }
public PropertiesMuleConfigurationFactory(String muleAppConfiguration) { URL muleAppURL = ClassUtils.getResource(muleAppConfiguration, getClass()); if (muleAppURL != null) { this.properties = new Properties(); InputStream inputStream = null; try { inputStream = muleAppURL.openStream(); this.properties.load(inputStream); } catch (IOException e) { logger.debug("Unable to read properties", e); } finally { closeQuietly(inputStream); } } }
private <T> T createCustomInstance(String classLocation) { InputStream classStream = null; try { classStream = this.getClass().getResourceAsStream(classLocation); byte[] classBytes = IOUtils.toByteArray(classStream); classStream.close(); Class clazz = this.defineClass(null, classBytes, 0, classBytes.length); return (T) clazz.newInstance(); } catch (Exception e) { throw new RuntimeException("Can not create instance from resource: " + classLocation, e); } finally { closeQuietly(classStream); } }