Java Code Examples for org.apache.commons.io.IOUtils#read()

The following examples show how to use org.apache.commons.io.IOUtils#read() . 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: TestSignedChunksInputStream.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
@Test
public void singlechunkwithoutend() throws IOException {
  //test simple read()
  InputStream is = fileContent("0A;chunk-signature"
      +
      "=23abb2bd920ddeeaac78a63ed808bc59fa6e7d3ef0e356474b82cdc2f8c93c40\r"
      + "\n1234567890");
  String result = IOUtils.toString(is, Charset.forName("UTF-8"));
  Assert.assertEquals("1234567890", result);

  //test read(byte[],int,int)
  is = fileContent("0A;chunk-signature"
      +
      "=23abb2bd920ddeeaac78a63ed808bc59fa6e7d3ef0e356474b82cdc2f8c93c40\r"
      + "\n1234567890");
  byte[] bytes = new byte[10];
  IOUtils.read(is, bytes, 0, 10);
  Assert.assertEquals("1234567890", new String(bytes));
}
 
Example 2
Source File: CachedBlobStore.java    From james-project with Apache License 2.0 6 votes vote down vote up
static RequireStream eager() {
    return in -> length -> {
        //+1 is to evaluate hasMore
        var stream = new PushbackInputStream(in, length + 1);
        var bytes = new byte[length];
        int readByteCount = IOUtils.read(stream, bytes);
        Optional<byte[]> firstBytes;
        boolean hasMore;
        if (readByteCount < 0) {
            firstBytes = Optional.empty();
            hasMore = false;
        } else {
            byte[] readBytes = Arrays.copyOf(bytes, readByteCount);
            hasMore = hasMore(stream);
            stream.unread(readBytes);
            firstBytes = Optional.of(readBytes);
        }
        return new ReadAheadInputStream(stream, firstBytes, hasMore);
    };
}
 
Example 3
Source File: MediaEntityTestITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void read(final ODataClient client, final ContentType contentType) throws IOException {
  final URIBuilder builder = client.newURIBuilder(testDemoServiceRootURL).
      appendEntitySetSegment("Advertisements").
      appendKeySegment(UUID.fromString("f89dee73-af9f-4cd4-b330-db93c25ff3c7"));
  final ODataEntityRequest<ClientEntity> entityReq =
      client.getRetrieveRequestFactory().getEntityRequest(builder.build());
  entityReq.setFormat(contentType);

  final ClientEntity entity = entityReq.execute().getBody();
  assertNotNull(entity);
  assertTrue(entity.isMediaEntity());
  // cast to workaround JDK 6 bug, fixed in JDK 7
  assertEquals(EdmPrimitiveTypeKind.DateTimeOffset.getFullQualifiedName().toString(),
      ((ClientValuable) entity.getProperty("AirDate")).getValue().getTypeName());

  final ODataMediaRequest streamReq = client.getRetrieveRequestFactory().
      getMediaRequest(entity.getMediaContentSource());
  final ODataRetrieveResponse<InputStream> streamRes = streamReq.execute();
  assertEquals(200, streamRes.getStatusCode());

  final byte[] actual = new byte[Integer.parseInt(streamRes.getHeader("Content-Length").iterator().next())];
  IOUtils.read(streamRes.getBody(), actual, 0, actual.length);
}
 
Example 4
Source File: MediaEntityTestITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void update(final ContentType contentType) throws IOException, EdmPrimitiveTypeException {
  final URI uri = client.newURIBuilder(testDemoServiceRootURL).
      appendEntitySetSegment("Advertisements").
      appendKeySegment(UUID.fromString("f89dee73-af9f-4cd4-b330-db93c25ff3c7")).build();

  final String random = RandomStringUtils.random(124);

  // 1. update providing media content
  final ODataMediaEntityUpdateRequest<ClientEntity> updateReq = client.getCUDRequestFactory().
      getMediaEntityUpdateRequest(uri, IOUtils.toInputStream(random));
  updateReq.setFormat(contentType);

  final MediaEntityUpdateStreamManager<ClientEntity> streamManager = updateReq.payloadManager();
  final ODataMediaEntityUpdateResponse<ClientEntity> createRes = streamManager.getResponse();
  assertEquals(204, createRes.getStatusCode());

  // 2. check that media content was effectively uploaded
  final ODataMediaRequest streamReq = client.getRetrieveRequestFactory().getMediaEntityRequest(uri);
  final ODataRetrieveResponse<InputStream> streamRes = streamReq.execute();
  assertEquals(200, streamRes.getStatusCode());

  final byte[] actual = new byte[Integer.parseInt(streamRes.getHeader("Content-Length").iterator().next())];
  IOUtils.read(streamRes.getBody(), actual, 0, actual.length);
  assertEquals(random, new String(actual));
}
 
Example 5
Source File: multiplefile.java    From RestServices with Apache License 2.0 6 votes vote down vote up
@Override
public java.lang.Boolean executeAction() throws Exception
{
	this.file = __file == null ? null : tests.proxies.TestFile.initialize(getContext(), __file);

	// BEGIN USER CODE
	int size = (int) (long) Misc.getFileSize(getContext(), __file);
	byte[] buffer = new byte[size];
	IOUtils.read(Core.getFileDocumentContent(getContext(), __file), buffer);
	
	byte[] resultbuffer = new byte[size * (int)file.getMultiplier()];
	
	//MWE: worst implementation ever, but thats not the point :)
	for(int i = 0; i < file.getMultiplier(); i++)
		for (int j = 0; j < size; j++)
			resultbuffer[i*size + j] = buffer[j];
	
	InputStream inputStream = new ByteArrayInputStream(resultbuffer);
	Core.storeFileDocumentContent(getContext(), __file, inputStream);
	file.commit();
	return true;
	// END USER CODE
}
 
Example 6
Source File: PluginManager.java    From Openfire with Apache License 2.0 6 votes vote down vote up
/**
 * Verifies that the first few bytes of the input stream correspond to any of the known 'magic numbers' that
 * are known to represent a JAR archive.
 *
 * This method uses the mark/reset functionality of InputStream. This ensures that the input stream is reset
 * back to its original position after execution of this method.
 *
 * @param bin The input to read (cannot be null).
 * @return true if the stream first few bytes are equal to any of the known magic number sequences, otherwise false.
 */
public static boolean validMagicNumbers( final BufferedInputStream bin ) throws IOException
{
    final List<String> validMagicBytesCollection = JiveGlobals.getListProperty( "plugins.upload.magic-number.values.expected-value", Arrays.asList( "504B0304", "504B0506", "504B0708" ) );
    for ( final String entry : validMagicBytesCollection )
    {
        final byte[] validMagicBytes = StringUtils.decodeHex( entry );
        bin.mark( validMagicBytes.length );
        try
        {
            final byte[] magicBytes = new byte[validMagicBytes.length];
            final int bytesRead = IOUtils.read( bin, magicBytes );
            if ( bytesRead == validMagicBytes.length && Arrays.equals( validMagicBytes, magicBytes ) )
            {
                return true;
            }
        }
        finally
        {
            bin.reset();
        }
    }

    return false;
}
 
Example 7
Source File: CryptoInputStream.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
private int readNextChunk() throws IOException {
    final ByteBuffer ciphertextBuf = ByteBuffer.allocate(chunkSize);
    final int read = IOUtils.read(proxy, ciphertextBuf.array());
    if(read == 0) {
        return IOUtils.EOF;
    }
    ciphertextBuf.position(read);
    ciphertextBuf.flip();
    try {
        buffer = cryptor.decryptChunk(ciphertextBuf, chunkIndexOffset++, header, true);
    }
    catch(CryptoException e) {
        throw new IOException(e.getMessage(), new CryptoAuthenticationException(e.getMessage(), e));
    }
    return read;
}
 
Example 8
Source File: TestSignedChunksInputStream.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
@Test
public void multichunks() throws IOException {
  //test simple read()
  InputStream is = fileContent("0a;chunk-signature=signature\r\n"
      + "1234567890\r\n"
      + "05;chunk-signature=signature\r\n"
      + "abcde\r\n");
  String result = IOUtils.toString(is, Charset.forName("UTF-8"));
  Assert.assertEquals("1234567890abcde", result);

  //test read(byte[],int,int)
  is = fileContent("0a;chunk-signature=signature\r\n"
      + "1234567890\r\n"
      + "05;chunk-signature=signature\r\n"
      + "abcde\r\n");
  byte[] bytes = new byte[15];
  IOUtils.read(is, bytes, 0, 15);
  Assert.assertEquals("1234567890abcde", new String(bytes));
}
 
Example 9
Source File: TestSignedChunksInputStream.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
@Test
public void singlechunk() throws IOException {
  //test simple read()
  InputStream is = fileContent("0A;chunk-signature"
      +
      "=23abb2bd920ddeeaac78a63ed808bc59fa6e7d3ef0e356474b82cdc2f8c93c40\r"
      + "\n1234567890\r\n");
  String result = IOUtils.toString(is, Charset.forName("UTF-8"));
  Assert.assertEquals("1234567890", result);

  //test read(byte[],int,int)
  is = fileContent("0A;chunk-signature"
      +
      "=23abb2bd920ddeeaac78a63ed808bc59fa6e7d3ef0e356474b82cdc2f8c93c40\r"
      + "\n1234567890\r\n");
  byte[] bytes = new byte[10];
  IOUtils.read(is, bytes, 0, 10);
  Assert.assertEquals("1234567890", new String(bytes));
}
 
Example 10
Source File: MediaEntityTestITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void create(final ContentType contentType) throws IOException {
  final String random = RandomStringUtils.random(110);
  final InputStream input = IOUtils.toInputStream(random);

  final URI uri = client.newURIBuilder(testDemoServiceRootURL).appendEntitySetSegment("Advertisements").build();
  final ODataMediaEntityCreateRequest<ClientEntity> createReq =
      client.getCUDRequestFactory().getMediaEntityCreateRequest(uri, input);
  final MediaEntityCreateStreamManager<ClientEntity> streamManager = createReq.payloadManager();

  final ODataMediaEntityCreateResponse<ClientEntity> createRes = streamManager.getResponse();
  assertEquals(201, createRes.getStatusCode());

  final Collection<String> location = createRes.getHeader(HttpHeader.LOCATION);
  assertNotNull(location);
  final URI createdLocation = URI.create(location.iterator().next());

  final ClientEntity changes = client.getObjectFactory().
      newEntity(new FullQualifiedName("ODataDemo.Advertisement"));
  changes.getProperties().add(client.getObjectFactory().newPrimitiveProperty("AirDate",
      getClient().getObjectFactory().newPrimitiveValueBuilder().
      setType(EdmPrimitiveTypeKind.DateTimeOffset).setValue(Calendar.getInstance()).build()));

  final ODataEntityUpdateRequest<ClientEntity> updateReq = getClient().getCUDRequestFactory().
      getEntityUpdateRequest(createdLocation, UpdateType.PATCH, changes);
  updateReq.setFormat(contentType);

  final ODataEntityUpdateResponse<ClientEntity> updateRes = updateReq.execute();
  assertEquals(204, updateRes.getStatusCode());

  final ODataMediaRequest retrieveReq = client.getRetrieveRequestFactory().
      getMediaEntityRequest(client.newURIBuilder(createdLocation.toASCIIString()).build());
  final ODataRetrieveResponse<InputStream> retrieveRes = retrieveReq.execute();
  assertEquals(200, retrieveRes.getStatusCode());

  final byte[] actual = new byte[Integer.parseInt(retrieveRes.getHeader("Content-Length").iterator().next())];
  IOUtils.read(retrieveRes.getBody(), actual, 0, actual.length);
  assertEquals(random, new String(actual));
}
 
Example 11
Source File: FileBattery.java    From coffee-gb with MIT License 5 votes vote down vote up
private void loadClock(long[] clockData, InputStream is) throws IOException {
    byte[] byteBuff = new byte[4 * clockData.length];
    IOUtils.read(is, byteBuff);
    ByteBuffer buff = ByteBuffer.wrap(byteBuff);
    buff.order(ByteOrder.LITTLE_ENDIAN);
    int i = 0;
    while (buff.hasRemaining()) {
        clockData[i++] = buff.getInt() & 0xffffffff;
    }
}
 
Example 12
Source File: TripleCryptInputStream.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
private int readNextChunk() throws IOException {
    final ByteBuffer ciphertextBuf = ByteBuffer.allocate(SDSSession.DEFAULT_CHUNKSIZE);
    final int read = IOUtils.read(proxy, ciphertextBuf.array());
    if(lastread == 0) {
        return IOUtils.EOF;
    }
    ciphertextBuf.position(read);
    ciphertextBuf.flip();
    try {
        final PlainDataContainer pDataContainer;
        if(read == 0) {
            final PlainDataContainer c1 = cipher.processBytes(createEncryptedDataContainer(ciphertextBuf.array(), read, null));
            final PlainDataContainer c2 = cipher.doFinal(new EncryptedDataContainer(null, tag));
            pDataContainer = new PlainDataContainer(ArrayUtils.addAll(c1.getContent(), c2.getContent()));
        }
        else {
            pDataContainer = cipher.processBytes(createEncryptedDataContainer(ciphertextBuf.array(), read, null));
        }
        final byte[] content = pDataContainer.getContent();
        buffer = ByteBuffer.allocate(content.length);
        buffer.put(content);
        buffer.flip();
        lastread = read;
        return content.length;
    }
    catch(CryptoException e) {
        throw new IOException(e);
    }
}
 
Example 13
Source File: AbstractX509LdapTests.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
public static void bootstrap() throws Exception {
    initDirectoryServer();
    getDirectory().populateEntries(new ClassPathResource("ldif/users-x509.ldif").getInputStream());

    /**
     * Dynamically set the attribute value to the crl content.
     * Encode it as base64 first. Doing this in the code rather
     * than in the ldif file to ensure the attribute can be populated
     * without dependencies on the classpath and or filesystem.
     */
    final Collection<LdapEntry> col = getDirectory().getLdapEntries();
    for (final LdapEntry ldapEntry : col) {
        if (ldapEntry.getDn().equals(DN)) {
            final LdapAttribute attr = new LdapAttribute(true);

            byte[] value = new byte[1024];
            IOUtils.read(new ClassPathResource("userCA-valid.crl").getInputStream(), value);
            value = CompressionUtils.encodeBase64ToByteArray(value);
            attr.setName("certificateRevocationList");
            attr.addBinaryValue(value);

            final LDAPConnection serverCon = getDirectory().getConnection();
            final String address = "ldap://" + serverCon.getConnectedAddress() + ":" + serverCon.getConnectedPort();
            final Connection conn = DefaultConnectionFactory.getConnection(address);
            conn.open();
            final ModifyOperation modify = new ModifyOperation(conn);
            modify.execute(new ModifyRequest(ldapEntry.getDn(),
                    new AttributeModification(AttributeModificationType.ADD, attr)));
            conn.close();
            serverCon.close();
            return;
        }
    }
}
 
Example 14
Source File: TestContainerPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void inform(ResourceLoader loader) throws IOException {
  this.resourceLoader = (SolrResourceLoader) loader;
  try {
    InputStream is = resourceLoader.openResource("org/apache/solr/handler/MyPlugin.class");
    byte[] buf = new byte[1024*5];
    int sz = IOUtils.read(is, buf);
    classData = ByteBuffer.wrap(buf, 0,sz);
  } catch (IOException e) {
    //do not do anything
  }
}
 
Example 15
Source File: UnZipStepExecution.java    From pipeline-utility-steps-plugin with MIT License 5 votes vote down vote up
@Override
public Boolean invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
    PrintStream logger = listener.getLogger();
    try (ZipFile zip = new ZipFile(f)) {
        logger.print("Checking ");
        logger.print(zip.size());
        logger.print(" zipped entries in ");
        logger.println(f.getAbsolutePath());

        Checksum checksum = new CRC32();
        byte[] buffer = new byte[4096];

        Enumeration<? extends ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {
            checksum.reset();

            ZipEntry entry = entries.nextElement();
            if (!entry.isDirectory()) {
                try (InputStream inputStream = zip.getInputStream(entry)) {
                    int length;
                    while ((length = IOUtils.read(inputStream, buffer)) > 0) {
                        checksum.update(buffer, 0, length);
                    }
                    if (checksum.getValue() != entry.getCrc()) {
                        listener.error("Checksum error in : " + f.getAbsolutePath() + ":" + entry.getName());
                        return false;
                    }
                }
            }
        }
        return true;
    } catch (ZipException e) {
        listener.error("Error validating zip file: " + e.getMessage());
        return false;
    } finally {
        logger.flush();
    }
}
 
Example 16
Source File: FileUtil.java    From DesignPatterns with Apache License 2.0 5 votes vote down vote up
public static String loadFileContentByClassPath(String fileName) throws IOException{
    Resource[] resources = getResources(fileName);
    if (resources.length == 0) {
        throw new FileNotFoundException(fileName);
    }
    InputStream io = resources[0].getInputStream();
    byte[] contents = new byte[102400];
    IOUtils.read(io, contents);
    IOUtils.closeQuietly(io);
    return new String(contents);
}
 
Example 17
Source File: RemoteFileFetcher.java    From phabricator-jenkins-plugin with MIT License 5 votes vote down vote up
/**
 * Attempt to read a remote  file
 *
 * @return the content of the remote comment file, if present
 * @throws InterruptedException if there is an error fetching the file
 * @throws IOException if any network error occurs
 */
public String getRemoteFile() throws InterruptedException, IOException {
    if (CommonUtils.isBlank(fileName)) {
        logger.info(LOGGER_TAG, "no file configured");
        return null;
    }

    FilePath[] src = workspace.list(fileName);
    if (src.length == 0) {
        logger.info(LOGGER_TAG, "no files found by path: '" + fileName + "'");
        return null;
    }
    if (src.length > 1) {
        logger.info(LOGGER_TAG, "Found multiple matches. Reading first only.");
    }

    FilePath source = src[0];

    int maxLength = DEFAULT_MAX_SIZE;
    if (!CommonUtils.isBlank(maxSize)) {
        maxLength = parseInt(maxSize, 10);
    }
    if (source.length() < maxLength) {
        maxLength = (int) source.length();
    }
    byte[] buffer = new byte[maxLength];
    InputStream stream = source.read();

    try {
        IOUtils.read(stream, buffer);
    } finally {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException e) { /* ignore */ }
    }

    return new String(buffer);
}
 
Example 18
Source File: TestTarFileCreator.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private static void readJar(TarInputStream tis) throws IOException {
  TarEntry fileEntry = readFile(tis);
  byte[] buffer = new byte[8192 * 8];
  int read = IOUtils.read(tis, buffer);
  JarInputStream jar = new JarInputStream(new ByteArrayInputStream(buffer, 0 , read));
  JarEntry entry = jar.getNextJarEntry();
  Assert.assertNotNull(Utils.format("Read {} bytes and found a null entry", read), entry);
  Assert.assertEquals("sample.txt", entry.getName());
  read = IOUtils.read(jar, buffer);
  Assert.assertEquals(FilenameUtils.getBaseName(fileEntry.getName()),
    new String(buffer, 0, read, StandardCharsets.UTF_8));
}
 
Example 19
Source File: JsonArrayChunkerInputSegment.java    From hollow with Apache License 2.0 4 votes vote down vote up
boolean fill(Reader reader) throws IOException {
    dataLength = IOUtils.read(reader, data);
    return dataLength < data.length;
}
 
Example 20
Source File: AwsUtils.java    From studio with GNU General Public License v3.0 4 votes vote down vote up
public static void uploadStream(String inputBucket, String inputKey, AmazonS3 s3Client, int partSize,
                                String filename, InputStream content) throws AwsException {
    List<PartETag> etags = new LinkedList<>();
    InitiateMultipartUploadResult initResult = null;
    try {
        int partNumber = 1;
        long totalBytes = 0;

        MimetypesFileTypeMap mimeMap = new MimetypesFileTypeMap();
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentType(mimeMap.getContentType(filename));

        InitiateMultipartUploadRequest initRequest =
                new InitiateMultipartUploadRequest(inputBucket, inputKey, meta);
        initResult = s3Client.initiateMultipartUpload(initRequest);
        byte[] buffer = new byte[partSize];
        int read;

        logger.debug("Starting upload for file '{}'", filename);

        while (0 < (read = IOUtils.read(content, buffer))) {
            totalBytes += read;
            if (logger.isTraceEnabled()) {
                logger.trace("Uploading part {} with size {} - total: {}", partNumber, read, totalBytes);
            }
            ByteArrayInputStream bais = new ByteArrayInputStream(buffer, 0, read);
            UploadPartRequest uploadRequest = new UploadPartRequest()
                    .withUploadId(initResult.getUploadId())
                    .withBucketName(inputBucket)
                    .withKey(inputKey)
                    .withInputStream(bais)
                    .withPartNumber(partNumber)
                    .withPartSize(read)
                    .withLastPart(read < partSize);
            etags.add(s3Client.uploadPart(uploadRequest).getPartETag());
            partNumber++;
        }

        if (totalBytes == 0) {
            // If the file is empty, use the simple upload instead of the multipart
            s3Client.abortMultipartUpload(
                    new AbortMultipartUploadRequest(inputBucket, inputKey, initResult.getUploadId()));

            s3Client.putObject(inputBucket, inputKey, new ByteArrayInputStream(new byte[0]), meta);
        } else {
            CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest(inputBucket,
                    inputKey, initResult.getUploadId(), etags);

            s3Client.completeMultipartUpload(completeRequest);
        }

        logger.debug("Upload completed for file '{}'", filename);

    } catch (Exception e) {
        if (initResult != null) {
            s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(inputBucket, inputKey,
                    initResult.getUploadId()));
        }
        throw new AwsException("Upload of file '" + filename + "' failed", e);
    }
}