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();
}
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();
}
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);
}
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);
}
}
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);
}
}
Aggregations