Search in sources :

Example 41 with OutputStream

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

the class CryptoStreamsTestBase method testHasEnhancedByteBufferAccess.

@Test(timeout = 120000)
public void testHasEnhancedByteBufferAccess() throws Exception {
    OutputStream out = getOutputStream(defaultBufferSize);
    writeData(out);
    InputStream in = getInputStream(defaultBufferSize);
    final int len1 = dataLen / 8;
    // ByteBuffer size is len1
    ByteBuffer buffer = ((HasEnhancedByteBufferAccess) in).read(getBufferPool(), len1, EnumSet.of(ReadOption.SKIP_CHECKSUMS));
    int n1 = buffer.remaining();
    byte[] readData = new byte[n1];
    buffer.get(readData);
    byte[] expectedData = new byte[n1];
    System.arraycopy(data, 0, expectedData, 0, n1);
    Assert.assertArrayEquals(readData, expectedData);
    ((HasEnhancedByteBufferAccess) in).releaseBuffer(buffer);
    // Read len1 bytes
    readData = new byte[len1];
    readAll(in, readData, 0, len1);
    expectedData = new byte[len1];
    System.arraycopy(data, n1, expectedData, 0, len1);
    Assert.assertArrayEquals(readData, expectedData);
    // ByteBuffer size is len1
    buffer = ((HasEnhancedByteBufferAccess) in).read(getBufferPool(), len1, EnumSet.of(ReadOption.SKIP_CHECKSUMS));
    int n2 = buffer.remaining();
    readData = new byte[n2];
    buffer.get(readData);
    expectedData = new byte[n2];
    System.arraycopy(data, n1 + len1, expectedData, 0, n2);
    Assert.assertArrayEquals(readData, expectedData);
    ((HasEnhancedByteBufferAccess) in).releaseBuffer(buffer);
    in.close();
}
Also used : InputStream(java.io.InputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) OutputStream(java.io.OutputStream) HasEnhancedByteBufferAccess(org.apache.hadoop.fs.HasEnhancedByteBufferAccess) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 42 with OutputStream

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

the class TestCryptoOutputStreamClosing method testOutputStreamNotClosing.

@Test
public void testOutputStreamNotClosing() throws Exception {
    OutputStream outputStream = mock(OutputStream.class);
    CryptoOutputStream cos = new CryptoOutputStream(outputStream, codec, new byte[16], new byte[16], 0L, false);
    cos.close();
    verify(outputStream, never()).close();
}
Also used : OutputStream(java.io.OutputStream) Test(org.junit.Test)

Example 43 with OutputStream

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

the class TestCryptoStreamsNormal method getOutputStream.

@Override
protected OutputStream getOutputStream(int bufferSize, byte[] key, byte[] iv) throws IOException {
    OutputStream out = new ByteArrayOutputStream() {

        @Override
        public void flush() throws IOException {
            buffer = buf;
            bufferLen = count;
        }

        @Override
        public void close() throws IOException {
            buffer = buf;
            bufferLen = count;
        }
    };
    return new CryptoOutputStream(out, codec, bufferSize, key, iv);
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 44 with OutputStream

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

the class NioGroovyMethods method append.

/**
     * Append the text at the end of the Path, using a specified encoding.  If
     * the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias),
     * <code>writeBom</code> is <code>true</code>, and the file doesn't already
     * exist, the requisite byte order mark is written to the file before the
     * text is appended.
     *
     * @param self     a Path
     * @param text     the text to append at the end of the Path
     * @param charset  the charset used
     * @param writeBom whether to write the BOM
     * @throws java.io.IOException if an IOException occurs.
     * @since 2.5.0
     */
public static void append(Path self, Object text, String charset, boolean writeBom) throws IOException {
    Writer writer = null;
    try {
        Charset resolvedCharset = Charset.forName(charset);
        boolean shouldWriteBom = writeBom && !self.toFile().exists();
        OutputStream out = Files.newOutputStream(self, CREATE, APPEND);
        if (shouldWriteBom) {
            IOGroovyMethods.writeUTF16BomIfRequired(out, resolvedCharset);
        }
        writer = new OutputStreamWriter(out, resolvedCharset);
        InvokerHelper.write(writer, text);
        writer.flush();
        Writer temp = writer;
        writer = null;
        temp.close();
    } finally {
        closeWithWarning(writer);
    }
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) DataOutputStream(java.io.DataOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) GroovyPrintWriter(groovy.io.GroovyPrintWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

Example 45 with OutputStream

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

the class NioGroovyMethods method append.

/**
     * Append bytes to the end of a Path.  It <strong>will not</strong> be
     * interpreted as text.
     *
     * @param self  a Path
     * @param bytes the byte array to append to the end of the Path
     * @throws java.io.IOException if an IOException occurs.
     * @since 2.3.0
     */
public static void append(Path self, byte[] bytes) throws IOException {
    OutputStream stream = null;
    try {
        stream = Files.newOutputStream(self, CREATE, APPEND);
        stream.write(bytes, 0, bytes.length);
        stream.flush();
        OutputStream temp = stream;
        stream = null;
        temp.close();
    } finally {
        closeWithWarning(stream);
    }
}
Also used : BufferedOutputStream(java.io.BufferedOutputStream) DataOutputStream(java.io.DataOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream)

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