Java Code Examples for org.apache.commons.compress.utils.IOUtils#toByteArray()

The following examples show how to use org.apache.commons.compress.utils.IOUtils#toByteArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RemoteArtifactTests.java    From embedded-cassandra with Apache License 2.0 6 votes vote down vote up
@BeforeEach
void setUp() throws Exception {
	System.setOut(new PrintStream(this.out));
	this.httpServer.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
	this.httpServer.start();
	byte[] content;
	try (InputStream inputStream = new ClassPathResource("apache-cassandra-3.11.6-bin.tar.gz").getInputStream()) {
		content = IOUtils.toByteArray(inputStream);
	}
	this.httpServer.createContext("/apache-cassandra-3.11.6-bin.tar.gz", exchange -> {
		exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, content.length);
		exchange.getResponseBody().write(content);
		exchange.close();
	});
	this.httpServer.createContext("/dist/apache-cassandra-3.11.6-bin.tar.gz", exchange -> {
		exchange.getResponseHeaders().put("Location",
				Collections.singletonList("/apache-cassandra-3.11.6-bin.tar.gz"));
		exchange.sendResponseHeaders(HttpURLConnection.HTTP_MOVED_PERM, content.length);
		exchange.close();
	});
}
 
Example 2
Source File: FileSystemTargetRepository.java    From ache with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <T> T unserializeData(InputStream inputStream) {
    T object = null;
    try {
        if (dataFormat.equals(DataFormat.CBOR)) {
                object = (T) cborMapper.readValue(inputStream, TargetModelCbor.class);
        } else if (dataFormat.equals(DataFormat.JSON)) {
            object = (T) jsonMapper.readValue(inputStream, TargetModelJson.class);
        } else if (dataFormat.equals(DataFormat.HTML)) {
            byte[] fileData = IOUtils.toByteArray(inputStream);
            object = (T) new String(fileData);
        }
    } catch (IOException e) {
        throw new RuntimeException("Failed to unserialize object.", e);
    }
    return object;
}
 
Example 3
Source File: CryptoPrimitivesTest.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
// TODO need to regen key now that we're using CryptoSuite
public void testSign() {

    byte[] plainText = "123456".getBytes(UTF_8);
    byte[] signature;
    try {
        PrivateKey key = (PrivateKey) crypto.getTrustStore().getKey("key", "123456".toCharArray());
        signature = crypto.sign(key, plainText);

        BufferedInputStream bis = new BufferedInputStream(
                this.getClass().getResourceAsStream("/keypair-signed.crt"));
        byte[] cert = IOUtils.toByteArray(bis);
        bis.close();

        assertTrue(crypto.verify(cert, SIGNING_ALGORITHM, signature, plainText));
    } catch (KeyStoreException | CryptoException | IOException | UnrecoverableKeyException
            | NoSuchAlgorithmException e) {
        fail("Could not verify signature. Error: " + e.getMessage());
    }
}
 
Example 4
Source File: GraphQLPsiSearchHelper.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
private PsiFile getGraphQLPsiFileFromResources(String resourceName, String displayName, Key<PsiFile> cacheProjectKey) {
    PsiFile psiFile = myProject.getUserData(cacheProjectKey);
    if (psiFile == null) {
        final PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(myProject);
        String specSchemaText = "";
        try {
            try (InputStream inputStream = pluginDescriptor.getPluginClassLoader().getResourceAsStream("/META-INF/" + resourceName)) {
                if (inputStream != null) {
                    specSchemaText = new String(IOUtils.toByteArray(inputStream));
                }
            }
        } catch (IOException e) {
            log.error("Unable to load schema", e);
            Notifications.Bus.notify(new Notification("GraphQL", "Unable to load " + displayName, e.getMessage(), NotificationType.ERROR));
        }
        psiFile = psiFileFactory.createFileFromText(displayName, GraphQLLanguage.INSTANCE, specSchemaText);
        myProject.putUserData(cacheProjectKey, psiFile);
        try {
            psiFile.getVirtualFile().setWritable(false);
        } catch (IOException ignored) {
        }
    }
    return psiFile;
}
 
Example 5
Source File: BulkUploadTest.java    From Insights with Apache License 2.0 5 votes vote down vote up
@Test(priority = 7, expectedExceptions = InsightsCustomException.class)
public void testUploadDataWithWrongInsightTimeFieldInDatabase() throws InsightsCustomException, IOException {
	FileInputStream input = new FileInputStream(fileWithNullEpochTime);
	MultipartFile multipartFile = new MockMultipartFile("file", fileWithNullEpochTime.getName(), "text/plain",
			IOUtils.toByteArray(input));
	boolean response = bulkUploadService.uploadDataInDatabase(multipartFile, bulkUploadTestData.toolName,
			bulkUploadTestData.label, bulkUploadTestData.wrongInsightTimeField,
			bulkUploadTestData.insightTimeWithTimeZoneFormat);
	String expectedOutcome = "Insight Time Field not present in the file";
	Assert.assertEquals(response, expectedOutcome);
	Assert.assertFalse(multipartFile.isEmpty());
	Assert.assertTrue(multipartFile.getSize() < filesizeMaxValue);
}
 
Example 6
Source File: CryptoPrimitivesTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidateBadCertificateByteArray() {
    try {
        BufferedInputStream bis = new BufferedInputStream(this.getClass().getResourceAsStream("/notsigned.crt"));
        byte[] certBytes = IOUtils.toByteArray(bis);

        assertFalse(crypto.validateCertificate(certBytes));
    } catch (IOException e) {
        Assert.fail("cannot read cert file");
    }
}
 
Example 7
Source File: KubernetesPipelineTest.java    From kubernetes-pipeline-plugin with Apache License 2.0 5 votes vote down vote up
private String loadPipelineScript(String name) {
    try {
        return new String(IOUtils.toByteArray(getClass().getResourceAsStream(name)));
    } catch (Throwable t) {
        throw new RuntimeException("Could not read resource:["+name+"].");
    }
}
 
Example 8
Source File: BulkUploadTest.java    From Insights with Apache License 2.0 5 votes vote down vote up
@Test(priority = 13)
public void testFileWithNumericValues() throws InsightsCustomException, IOException {
	FileInputStream input = new FileInputStream(fileWithNumericValues);
	MultipartFile multipartFile = new MockMultipartFile("file", fileWithNumericValues.getName(), "text/plain",
			IOUtils.toByteArray(input));
	boolean response = bulkUploadService.uploadDataInDatabase(multipartFile, bulkUploadTestData.toolName,
			bulkUploadTestData.labelForNumericCheck, bulkUploadTestData.fileWithNumericValues_insighstimeField,
			bulkUploadTestData.nullInsightTimeFormat);
	Assert.assertEquals(response, true);
	Assert.assertFalse(multipartFile.isEmpty());
}
 
Example 9
Source File: BulkUploadTest.java    From Insights with Apache License 2.0 5 votes vote down vote up
@Test(priority = 10, expectedExceptions = InsightsCustomException.class)
public void testFileSizeException() throws InsightsCustomException, IOException {

	FileInputStream input = new FileInputStream(fileSize);
	MultipartFile multipartFile = new MockMultipartFile("file", fileSize.getName(), "text/plain",
			IOUtils.toByteArray(input));

	boolean response = bulkUploadService.uploadDataInDatabase(multipartFile, bulkUploadTestData.toolName,
			bulkUploadTestData.label, bulkUploadTestData.insightTimeField,
			bulkUploadTestData.nullInsightTimeFormat);

}
 
Example 10
Source File: MavenTestHelper.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public Payload read(final Repository repository, final String path) throws IOException {
  HttpGet get = new HttpGet(url(repository, path));
  try (CloseableHttpResponse response = client.execute(get)) {
    if (response.getStatusLine().getStatusCode() >= 300) {
      return null;
    }

    HttpEntity entity = response.getEntity();
    try (InputStream in = entity.getContent()) {
      byte[] content = IOUtils.toByteArray(in);
      String contentType = entity.getContentType().toString();

      return new Payload()
      {
        @Override
        public InputStream openInputStream() throws IOException {
          return new ByteArrayInputStream(content);
        }

        @Override
        public long getSize() {
          return content.length;
        }

        @Override
        public String getContentType() {
          return contentType;
        }
      };
    }
  }
}
 
Example 11
Source File: BulkUploadTest.java    From Insights with Apache License 2.0 5 votes vote down vote up
@Test(priority = 9, expectedExceptions = InsightsCustomException.class)
public void testUploadDataWithNullInsightTimeFieldInDatabase() throws InsightsCustomException, IOException {
	FileInputStream input = new FileInputStream(fileWithVariedEpochTimes);
	MultipartFile multipartFile = new MockMultipartFile("file", fileWithVariedEpochTimes.getName(), "text/plain",
			IOUtils.toByteArray(input));
	boolean response = bulkUploadService.uploadDataInDatabase(multipartFile, bulkUploadTestData.toolName,
			bulkUploadTestData.label, bulkUploadTestData.nullInsightTimeField,
			bulkUploadTestData.nullInsightTimeFormat);
	String expectedOutcome = "Insight Time Field not present in the file";
	Assert.assertEquals(response, expectedOutcome);
	Assert.assertFalse(multipartFile.isEmpty());
	Assert.assertTrue(multipartFile.getSize() < filesizeMaxValue);
}
 
Example 12
Source File: BulkUploadTest.java    From Insights with Apache License 2.0 5 votes vote down vote up
@Test(priority = 6, expectedExceptions = InsightsCustomException.class)
	public void testUploadDataWithNullEpochTimesInDatabase() throws InsightsCustomException, IOException {
		FileInputStream input = new FileInputStream(fileWithNullEpochTime);
		MultipartFile multipartFile = new MockMultipartFile("file", fileWithNullEpochTime.getName(), "text/plain",
				IOUtils.toByteArray(input));
		boolean response = bulkUploadService.uploadDataInDatabase(multipartFile, bulkUploadTestData.toolName,
				bulkUploadTestData.label, bulkUploadTestData.insightTimeField,
				bulkUploadTestData.insightTimeWithTimeZoneFormat);
//		String expectedOutcome = "Null values in column commitTime";
//		Assert.assertEquals(response, expectedOutcome);
		Assert.assertFalse(multipartFile.isEmpty());
		Assert.assertTrue(multipartFile.getSize() < filesizeMaxValue);
	}
 
Example 13
Source File: BulkUploadTest.java    From Insights with Apache License 2.0 5 votes vote down vote up
@Test(priority = 4)
public void testUploadDataWithZFormatEpochTimesInDatabase() throws InsightsCustomException, IOException {
	FileInputStream input = new FileInputStream(fileWithZFormatEpochTimes);
	MultipartFile multipartFile = new MockMultipartFile("file", fileWithZFormatEpochTimes.getName(), "text/plain",
			IOUtils.toByteArray(input));
	boolean response = bulkUploadService.uploadDataInDatabase(multipartFile, bulkUploadTestData.toolName,
			bulkUploadTestData.label, bulkUploadTestData.insightTimeField, bulkUploadTestData.insightTimeZFormat);
	Assert.assertEquals(response, true);
	Assert.assertFalse(multipartFile.isEmpty());
	Assert.assertTrue(multipartFile.getSize() < filesizeMaxValue);
}
 
Example 14
Source File: BulkUploadTest.java    From Insights with Apache License 2.0 5 votes vote down vote up
@Test(priority = 3)
public void testUploadDataWithVariedEpochTimesInDatabase() throws InsightsCustomException, IOException {
	FileInputStream input = new FileInputStream(fileWithVariedEpochTimes);
	MultipartFile multipartFile = new MockMultipartFile("file", fileWithVariedEpochTimes.getName(), "text/plain",
			IOUtils.toByteArray(input));
	boolean response = bulkUploadService.uploadDataInDatabase(multipartFile, bulkUploadTestData.toolName,
			bulkUploadTestData.label, bulkUploadTestData.insightTimeField,
			bulkUploadTestData.nullInsightTimeFormat);
	Assert.assertEquals(response, true);
	Assert.assertFalse(multipartFile.isEmpty());
	Assert.assertTrue(multipartFile.getSize() < filesizeMaxValue);
}
 
Example 15
Source File: BulkUploadTest.java    From Insights with Apache License 2.0 5 votes vote down vote up
@Test(priority = 2)
public void testUploadDataInDatabase() throws InsightsCustomException, IOException {
	FileInputStream input = new FileInputStream(file);
	MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
			IOUtils.toByteArray(input));
	boolean response = bulkUploadService.uploadDataInDatabase(multipartFile, bulkUploadTestData.toolName,
			bulkUploadTestData.label, bulkUploadTestData.insightTimeField,
			bulkUploadTestData.nullInsightTimeFormat);
	Assert.assertEquals(response, true);
	Assert.assertFalse(multipartFile.isEmpty());
	Assert.assertTrue(multipartFile.getSize() < filesizeMaxValue);
}
 
Example 16
Source File: FullSerializationRecordCoder.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
@Override
public Record decode(final InputStream inputStream) throws IOException {
    final DatumReader<IndexedRecord> datumReader = new GenericDatumReader<>();
    try (final DataFileReader<IndexedRecord> reader =
            new DataFileReader<>(new SeekableByteArrayInput(IOUtils.toByteArray(inputStream)), datumReader)) {
        return new AvroRecord(reader.next());
    }
}
 
Example 17
Source File: ResourceUtil.java    From prebid-server-java with Apache License 2.0 5 votes vote down vote up
/**
 * Reads files from classpath as array of bytes. Throws {@link IllegalArgumentException} if file was not found.
 */
public static byte[] readByteArrayFromClassPath(String path) throws IOException {
    final InputStream resourceAsStream = ResourceUtil.class.getClassLoader().getResourceAsStream(path);
    if (resourceAsStream == null) {
        throw new IllegalArgumentException(String.format("Could not find file at path: %s", path));
    }
    return IOUtils.toByteArray(resourceAsStream);
}
 
Example 18
Source File: KubernetesTestUtil.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
public static String loadPipelineScript(Class<?> clazz, String name) {
    try {
        return new String(IOUtils.toByteArray(clazz.getResourceAsStream(name)));
    } catch (Throwable t) {
        throw new RuntimeException("Could not read resource:[" + name + "].");
    }
}
 
Example 19
Source File: ConvolutionListenerPersistable.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public void decode(InputStream inputStream) throws IOException {
    byte[] b = IOUtils.toByteArray(inputStream);
    decode(b);
}
 
Example 20
Source File: TestUtils.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
public static void writeStreamToFile(File out, InputStream is) throws IOException {
    byte[] b = IOUtils.toByteArray(is);
    try (OutputStream os = new BufferedOutputStream(new FileOutputStream(out))) {
        os.write(b);
    }
}