Search in sources :

Example 96 with OutputStream

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

the class TestIOUtils method testCopyBytesShouldCloseInputSteamWhenInputStreamCloseThrowsRunTimeException.

@Test
public void testCopyBytesShouldCloseInputSteamWhenInputStreamCloseThrowsRunTimeException() throws Exception {
    InputStream inputStream = Mockito.mock(InputStream.class);
    OutputStream outputStream = Mockito.mock(OutputStream.class);
    Mockito.doReturn(-1).when(inputStream).read(new byte[1]);
    Mockito.doThrow(new RuntimeException()).when(inputStream).close();
    try {
        IOUtils.copyBytes(inputStream, outputStream, 1, true);
        fail("Didn't throw exception");
    } catch (RuntimeException e) {
    }
    Mockito.verify(inputStream, Mockito.atLeastOnce()).close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Test(org.junit.Test)

Example 97 with OutputStream

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

the class TestIOUtils method testCopyBytesShouldCloseInputSteamWhenOutputStreamCloseThrowsRunTimeException.

@Test
public void testCopyBytesShouldCloseInputSteamWhenOutputStreamCloseThrowsRunTimeException() throws Exception {
    InputStream inputStream = Mockito.mock(InputStream.class);
    OutputStream outputStream = Mockito.mock(OutputStream.class);
    Mockito.doReturn(-1).when(inputStream).read(new byte[1]);
    Mockito.doThrow(new RuntimeException()).when(outputStream).close();
    try {
        IOUtils.copyBytes(inputStream, outputStream, 1, true);
        fail("Didn't throw exception");
    } catch (RuntimeException e) {
    }
    Mockito.verify(outputStream, Mockito.atLeastOnce()).close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Test(org.junit.Test)

Example 98 with OutputStream

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

the class TestReloadingX509TrustManager method testReloadCorruptTrustStore.

@Test(timeout = 30000)
public void testReloadCorruptTrustStore() throws Exception {
    KeyPair kp = generateKeyPair("RSA");
    cert1 = generateCertificate("CN=Cert1", kp, 30, "SHA1withRSA");
    cert2 = generateCertificate("CN=Cert2", kp, 30, "SHA1withRSA");
    String truststoreLocation = BASEDIR + "/testcorrupt.jks";
    createTrustStore(truststoreLocation, "password", "cert1", cert1);
    ReloadingX509TrustManager tm = new ReloadingX509TrustManager("jks", truststoreLocation, "password", 10);
    try {
        tm.init();
        assertEquals(1, tm.getAcceptedIssuers().length);
        final X509Certificate cert = tm.getAcceptedIssuers()[0];
        // Wait so that the file modification time is different
        Thread.sleep((tm.getReloadInterval() + 1000));
        assertFalse(reloaderLog.getOutput().contains(ReloadingX509TrustManager.RELOAD_ERROR_MESSAGE));
        OutputStream os = new FileOutputStream(truststoreLocation);
        os.write(1);
        os.close();
        waitForFailedReloadAtLeastOnce((int) tm.getReloadInterval());
        assertEquals(1, tm.getAcceptedIssuers().length);
        assertEquals(cert, tm.getAcceptedIssuers()[0]);
    } finally {
        reloaderLog.stopCapturing();
        tm.destroy();
    }
}
Also used : KeyPair(java.security.KeyPair) KeyStoreTestUtil.generateKeyPair(org.apache.hadoop.security.ssl.KeyStoreTestUtil.generateKeyPair) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Example 99 with OutputStream

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

the class TransferFsImage method writeFileToPutRequest.

private static void writeFileToPutRequest(Configuration conf, HttpURLConnection connection, File imageFile, Canceler canceler) throws IOException {
    connection.setRequestProperty(Util.CONTENT_TYPE, "application/octet-stream");
    connection.setRequestProperty(Util.CONTENT_TRANSFER_ENCODING, "binary");
    OutputStream output = connection.getOutputStream();
    FileInputStream input = new FileInputStream(imageFile);
    try {
        copyFileToStream(output, imageFile, input, ImageServlet.getThrottler(conf), canceler);
    } finally {
        IOUtils.closeStream(input);
        IOUtils.closeStream(output);
    }
}
Also used : OutputStream(java.io.OutputStream) FileInputStream(java.io.FileInputStream)

Example 100 with OutputStream

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

the class TestDomainSocket method testClientServer1.

/**
   * Test a simple client/server interaction.
   *
   * @throws IOException
   */
void testClientServer1(final Class<? extends WriteStrategy> writeStrategyClass, final Class<? extends ReadStrategy> readStrategyClass, final DomainSocket[] preConnectedSockets) throws Exception {
    final String TEST_PATH = new File(sockDir.getDir(), "test_sock_client_server1").getAbsolutePath();
    final byte[] clientMsg1 = new byte[] { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6 };
    final byte[] serverMsg1 = new byte[] { 0x9, 0x8, 0x7, 0x6, 0x5 };
    final byte clientMsg2 = 0x45;
    final ArrayBlockingQueue<Throwable> threadResults = new ArrayBlockingQueue<Throwable>(2);
    final DomainSocket serv = (preConnectedSockets != null) ? null : DomainSocket.bindAndListen(TEST_PATH);
    Thread serverThread = new Thread() {

        public void run() {
            // Run server
            DomainSocket conn = null;
            try {
                conn = preConnectedSockets != null ? preConnectedSockets[0] : serv.accept();
                byte[] in1 = new byte[clientMsg1.length];
                ReadStrategy reader = readStrategyClass.newInstance();
                reader.init(conn);
                reader.readFully(in1, 0, in1.length);
                Assert.assertTrue(Arrays.equals(clientMsg1, in1));
                WriteStrategy writer = writeStrategyClass.newInstance();
                writer.init(conn);
                writer.write(serverMsg1);
                InputStream connInputStream = conn.getInputStream();
                int in2 = connInputStream.read();
                Assert.assertEquals((int) clientMsg2, in2);
                conn.close();
            } catch (Throwable e) {
                threadResults.add(e);
                Assert.fail(e.getMessage());
            }
            threadResults.add(new Success());
        }
    };
    serverThread.start();
    Thread clientThread = new Thread() {

        public void run() {
            try {
                DomainSocket client = preConnectedSockets != null ? preConnectedSockets[1] : DomainSocket.connect(TEST_PATH);
                WriteStrategy writer = writeStrategyClass.newInstance();
                writer.init(client);
                writer.write(clientMsg1);
                ReadStrategy reader = readStrategyClass.newInstance();
                reader.init(client);
                byte[] in1 = new byte[serverMsg1.length];
                reader.readFully(in1, 0, in1.length);
                Assert.assertTrue(Arrays.equals(serverMsg1, in1));
                OutputStream clientOutputStream = client.getOutputStream();
                clientOutputStream.write(clientMsg2);
                client.close();
            } catch (Throwable e) {
                threadResults.add(e);
            }
            threadResults.add(new Success());
        }
    };
    clientThread.start();
    for (int i = 0; i < 2; i++) {
        Throwable t = threadResults.take();
        if (!(t instanceof Success)) {
            Assert.fail(t.getMessage() + ExceptionUtils.getStackTrace(t));
        }
    }
    serverThread.join(120000);
    clientThread.join(120000);
    if (serv != null) {
        serv.close();
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) File(java.io.File)

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