Search in sources :

Example 21 with OutputStream

use of java.io.OutputStream in project hadoop by apache.

the class AbstractJavaKeyStoreProvider method flush.

@Override
public void flush() throws IOException {
    writeLock.lock();
    try {
        if (!changed) {
            LOG.debug("Keystore hasn't changed, returning.");
            return;
        }
        LOG.debug("Writing out keystore.");
        try (OutputStream out = getOutputStreamForKeystore()) {
            keyStore.store(out, password);
        } catch (KeyStoreException e) {
            throw new IOException("Can't store keystore " + this, e);
        } catch (NoSuchAlgorithmException e) {
            throw new IOException("No such algorithm storing keystore " + this, e);
        } catch (CertificateException e) {
            throw new IOException("Certificate exception storing keystore " + this, e);
        }
        changed = false;
    } finally {
        writeLock.unlock();
    }
}
Also used : OutputStream(java.io.OutputStream) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 22 with OutputStream

use of java.io.OutputStream in project hadoop by apache.

the class CBZip2OutputStream method sendMTFValues7.

private void sendMTFValues7(final int nSelectors) throws IOException {
    final Data dataShadow = this.data;
    final byte[][] len = dataShadow.sendMTFValues_len;
    final int[][] code = dataShadow.sendMTFValues_code;
    final OutputStream outShadow = this.out;
    final byte[] selector = dataShadow.selector;
    final char[] sfmap = dataShadow.sfmap;
    final int nMTFShadow = this.nMTF;
    int selCtr = 0;
    int bsLiveShadow = this.bsLive;
    int bsBuffShadow = this.bsBuff;
    for (int gs = 0; gs < nMTFShadow; ) {
        final int ge = Math.min(gs + G_SIZE - 1, nMTFShadow - 1);
        final int selector_selCtr = selector[selCtr] & 0xff;
        final int[] code_selCtr = code[selector_selCtr];
        final byte[] len_selCtr = len[selector_selCtr];
        while (gs <= ge) {
            final int sfmap_i = sfmap[gs];
            //
            while (bsLiveShadow >= 8) {
                outShadow.write(bsBuffShadow >> 24);
                bsBuffShadow <<= 8;
                bsLiveShadow -= 8;
            }
            final int n = len_selCtr[sfmap_i] & 0xFF;
            bsBuffShadow |= code_selCtr[sfmap_i] << (32 - bsLiveShadow - n);
            bsLiveShadow += n;
            gs++;
        }
        gs = ge + 1;
        selCtr++;
    }
    this.bsBuff = bsBuffShadow;
    this.bsLive = bsLiveShadow;
}
Also used : OutputStream(java.io.OutputStream)

Example 23 with OutputStream

use of java.io.OutputStream in project hadoop by apache.

the class CBZip2OutputStream method close.

@Override
public void close() throws IOException {
    if (out != null) {
        OutputStream outShadow = this.out;
        try {
            finish();
            outShadow.close();
            outShadow = null;
        } finally {
            IOUtils.closeStream(outShadow);
        }
    }
}
Also used : OutputStream(java.io.OutputStream)

Example 24 with OutputStream

use of java.io.OutputStream in project hadoop by apache.

the class TestFileUtil method testUnTar.

@Test(timeout = 30000)
public void testUnTar() throws IOException {
    setupDirs();
    // make a simple tar:
    final File simpleTar = new File(del, FILE);
    OutputStream os = new FileOutputStream(simpleTar);
    TarOutputStream tos = new TarOutputStream(os);
    try {
        TarEntry te = new TarEntry("/bar/foo");
        byte[] data = "some-content".getBytes("UTF-8");
        te.setSize(data.length);
        tos.putNextEntry(te);
        tos.write(data);
        tos.closeEntry();
        tos.flush();
        tos.finish();
    } finally {
        tos.close();
    }
    // successfully untar it into an existing dir:
    FileUtil.unTar(simpleTar, tmp);
    // check result:
    assertTrue(new File(tmp, "/bar/foo").exists());
    assertEquals(12, new File(tmp, "/bar/foo").length());
    final File regularFile = new File(tmp, "QuickBrownFoxJumpsOverTheLazyDog");
    regularFile.createNewFile();
    assertTrue(regularFile.exists());
    try {
        FileUtil.unTar(simpleTar, regularFile);
        assertTrue("An IOException expected.", false);
    } catch (IOException ioe) {
    // okay
    }
}
Also used : TarOutputStream(org.apache.tools.tar.TarOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) TarOutputStream(org.apache.tools.tar.TarOutputStream) FileOutputStream(java.io.FileOutputStream) TarEntry(org.apache.tools.tar.TarEntry) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Example 25 with OutputStream

use of java.io.OutputStream in project flink by apache.

the class BlobCache method getURL.

/**
	 * Returns the URL for the BLOB with the given key. The method will first attempt to serve
	 * the BLOB from its local cache. If the BLOB is not in the cache, the method will try to download it
	 * from this cache's BLOB server.
	 *
	 * @param requiredBlob The key of the desired BLOB.
	 * @return URL referring to the local storage location of the BLOB.
	 * @throws IOException Thrown if an I/O error occurs while downloading the BLOBs from the BLOB server.
	 */
public URL getURL(final BlobKey requiredBlob) throws IOException {
    checkArgument(requiredBlob != null, "BLOB key cannot be null.");
    final File localJarFile = BlobUtils.getStorageLocation(storageDir, requiredBlob);
    if (localJarFile.exists()) {
        return localJarFile.toURI().toURL();
    }
    // first try the distributed blob store (if available)
    try {
        blobStore.get(requiredBlob, localJarFile);
    } catch (Exception e) {
        LOG.info("Failed to copy from blob store. Downloading from BLOB server instead.", e);
    }
    if (localJarFile.exists()) {
        return localJarFile.toURI().toURL();
    }
    // fallback: download from the BlobServer
    final byte[] buf = new byte[BlobServerProtocol.BUFFER_SIZE];
    // loop over retries
    int attempt = 0;
    while (true) {
        if (attempt == 0) {
            LOG.info("Downloading {} from {}", requiredBlob, serverAddress);
        } else {
            LOG.info("Downloading {} from {} (retry {})", requiredBlob, serverAddress, attempt);
        }
        try {
            BlobClient bc = null;
            InputStream is = null;
            OutputStream os = null;
            try {
                bc = new BlobClient(serverAddress, blobClientConfig);
                is = bc.get(requiredBlob);
                os = new FileOutputStream(localJarFile);
                while (true) {
                    final int read = is.read(buf);
                    if (read < 0) {
                        break;
                    }
                    os.write(buf, 0, read);
                }
                // we do explicitly not use a finally block, because we want the closing
                // in the regular case to throw exceptions and cause the writing to fail.
                // But, the closing on exception should not throw further exceptions and
                // let us keep the root exception
                os.close();
                os = null;
                is.close();
                is = null;
                bc.close();
                bc = null;
                // success, we finished
                return localJarFile.toURI().toURL();
            } catch (Throwable t) {
                // we use "catch (Throwable)" to keep the root exception. Otherwise that exception
                // it would be replaced by any exception thrown in the finally block
                IOUtils.closeQuietly(os);
                IOUtils.closeQuietly(is);
                IOUtils.closeQuietly(bc);
                if (t instanceof IOException) {
                    throw (IOException) t;
                } else {
                    throw new IOException(t.getMessage(), t);
                }
            }
        } catch (IOException e) {
            String message = "Failed to fetch BLOB " + requiredBlob + " from " + serverAddress + " and store it under " + localJarFile.getAbsolutePath();
            if (attempt < numFetchRetries) {
                attempt++;
                if (LOG.isDebugEnabled()) {
                    LOG.debug(message + " Retrying...", e);
                } else {
                    LOG.error(message + " Retrying...");
                }
            } else {
                LOG.error(message + " No retries left.", e);
                throw new IOException(message, e);
            }
        }
    }
// end loop over retries
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException)

Aggregations

OutputStream (java.io.OutputStream)3717 IOException (java.io.IOException)1470 FileOutputStream (java.io.FileOutputStream)1192 InputStream (java.io.InputStream)1171 File (java.io.File)808 ByteArrayOutputStream (java.io.ByteArrayOutputStream)751 Test (org.junit.Test)681 BufferedOutputStream (java.io.BufferedOutputStream)418 FileInputStream (java.io.FileInputStream)380 Socket (java.net.Socket)362 ByteArrayInputStream (java.io.ByteArrayInputStream)201 OutputStreamWriter (java.io.OutputStreamWriter)201 URL (java.net.URL)193 HttpURLConnection (java.net.HttpURLConnection)162 BufferedInputStream (java.io.BufferedInputStream)151 InputStreamReader (java.io.InputStreamReader)149 FileNotFoundException (java.io.FileNotFoundException)143 Path (org.apache.hadoop.fs.Path)143 Path (java.nio.file.Path)138 BufferedReader (java.io.BufferedReader)133