Share

Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





#Java - Code Snippets for '#OutputStream' - 19 code snippet(s) found

 Sample 1. Code Sample / Example / Snippet of java.util.zip.DeflaterOutputStream

    private byte[] deflate(byte[] b) throws IOException {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

DeflaterOutputStream dos = new DeflaterOutputStream(baos);

dos.write(b);

dos.close();

return baos.toByteArray();

}


   Like      Feedback      java.util.zip.DeflaterOutputStream


 Sample 2. Write to a file using File, FileOutputStream and ObjectOutputStream

class BuggyBread1{
   public static void main(String[] args){
      try {
         BuggyBread1 buggybread1 = new BuggyBread1();
         ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("newFile.txt")));
         objectOutputStream.writeObject(buggybread1);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

   Like      Feedback     File   FileOutputStream   ObjectOutputStream   file handling


 Sample 3. Usage of ByteArrayOutputStream

ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(validator);
oos.flush();
oos.close();
} catch (Exception e) {
}

   Like      Feedback     java.io.ByteArrayOutputStream  java.io  ObjectOutputStream


 Sample 4. Code Sample / Example / Snippet of org.apache.storm.blobstore.AtomicOutputStream

    private static void createBlobWithContent(String blobKey, ClientBlobStore clientBlobStore, File file)

throws AuthorizationException, KeyAlreadyExistsException, IOException,KeyNotFoundException {

String stringBlobACL = "o::rwa";

AccessControl blobACL = BlobStoreAclHandler.parseAccessControl(stringBlobACL);

List<AccessControl> acls = new LinkedList<AccessControl>();

acls.add(blobACL); // more ACLs can be added here

SettableBlobMeta settableBlobMeta = new SettableBlobMeta(acls);

AtomicOutputStream blobStream = clientBlobStore.createBlob(blobKey,settableBlobMeta);

blobStream.write(readFile(file).toString().getBytes());

blobStream.close();

}


   Like      Feedback      org.apache.storm.blobstore.AtomicOutputStream


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 5. Code Sample / Example / Snippet of java.io.ByteArrayOutputStream

  private static Pair<SqlLine.Status, String> run(String... args)

throws Throwable {

SqlLine sqlline = new SqlLine();

ByteArrayOutputStream os = new ByteArrayOutputStream();

PrintStream sqllineOutputStream = new PrintStream(os);

sqlline.setOutputStream(sqllineOutputStream);

sqlline.setErrorStream(sqllineOutputStream);

SqlLine.Status status = SqlLine.Status.OK;



Bug.upgrade("[sqlline-35] Make Sqlline.begin public");



return Pair.of(status, os.toString("UTF8"));

}


   Like      Feedback      java.io.ByteArrayOutputStream


 Sample 6. Code Sample / Example / Snippet of javax.servlet.ServletOutputStream

    protected void doGet(HttpServletRequest request, HttpServletResponse response) {

ServletOutputStream output = null;

try {

output = response.getOutputStream();

output.println(request.getQueryString());

}

catch (IOException e) {

} finally {

if (output != null) {

try {

output.close();

}

catch (IOException e) {

}

}

}

}


   Like      Feedback      javax.servlet.ServletOutputStream


 Sample 7. Code Sample / Example / Snippet of java.security.DigestOutputStream

    public void setUpTestCase() throws Exception {

File file = File.createTempFile("test", ".bin", new File("generated"));

file.deleteOnExit();



DigestOutputStream dos = null;

try {

dos = new DigestOutputStream(new FileOutputStream(file), MessageDigest.getInstance("MD5"));



for (int i = 0; i < 10000; i++) {

dos.write(String.valueOf(System.currentTimeMillis()).getBytes());

dos.write(" Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum Lorum Ipsum ".getBytes());

}

dos.flush();

}

finally {

if (dos != null) {

dos.close();

}

}



m_testContentURL = file.toURI().toURL();

m_contentLength = file.length();

m_digest = new String(dos.getMessageDigest().digest());

}


   Like      Feedback      java.security.DigestOutputStream


 Sample 8. Code Sample / Example / Snippet of java.io.ByteArrayOutputStream

            public int compare(ResourceImpl r1, ResourceImpl r2) {

String s1 = getName(r1);

String s2 = getName(r2);

return s1.compareTo(s2);

}

});



Tag tag = doIndex(sorted);

if (repositoryFileName != null) {

ByteArrayOutputStream out = new ByteArrayOutputStream();


   Like      Feedback      java.io.ByteArrayOutputStream


 Sample 9. Code Sample / Example / Snippet of java.util.jar.JarOutputStream

    private File createBundle(String symbolicName, String version) throws IOException {

File tmpFile = File.createTempFile("tmpbundle-", "jar");

tmpFile.deleteOnExit();

Manifest manifest = new Manifest();

manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");

if (symbolicName != null) {

manifest.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, symbolicName);

}

if (version != null) {

manifest.getMainAttributes().putValue(Constants.BUNDLE_VERSION, version);

}

JarOutputStream target = new JarOutputStream(new FileOutputStream(tmpFile), manifest);

target.close();

return tmpFile;

}


   Like      Feedback      java.util.jar.JarOutputStream


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 10. Code Sample / Example / Snippet of java.io.FileOutputStream

    public static void copy(File input, File output) throws IOException {

FileInputStream fis = new FileInputStream(input);

FileOutputStream fos = new FileOutputStream(output);



try {

FileChannel ic = fis.getChannel();

FileChannel oc = fos.getChannel();

try {

oc.transferFrom(ic, 0, ic.size());

}

finally {

oc.close();

ic.close();

}

}

finally {

fis.close();

fos.close();

}

}


   Like      Feedback      java.io.FileOutputStream


 Sample 11. Code Sample / Example / Snippet of java.io.OutputStream

    public static void generateBundle(ArtifactData data, Map<String, String> additionalHeaders) throws IOException {

OutputStream bundleStream = null;

try {

File dataFile = new File(data.getUrl().toURI());

OutputStream fileStream = new FileOutputStream(dataFile);

bundleStream = new JarOutputStream(fileStream, getBundleManifest(data.getSymbolicName(), data.getVersion(), additionalHeaders));

bundleStream.flush();

} catch (URISyntaxException e) {

throw new IOException();

} finally {

if (bundleStream != null) {

bundleStream.close();

}

}

}


   Like      Feedback      java.io.OutputStream


 Sample 12. Code Sample / Example / Snippet of org.apache.commons.compress.archivers.ArchiveOutputStream

    protected File createEmptyArchive(final String archivename) throws Exception {

ArchiveOutputStream out = null;

OutputStream stream = null;

archiveList = new ArrayList<String>();

try {

archive = File.createTempFile("empty", "." + archivename);

archive.deleteOnExit();

stream = new FileOutputStream(archive);

out = factory.createArchiveOutputStream(archivename, stream);

out.finish();

} finally {

if (out != null) {

out.close();

} else if (stream != null) {

stream.close();

}

}

return archive;

}


   Like      Feedback      org.apache.commons.compress.archivers.ArchiveOutputStream


 Sample 13. Code Sample / Example / Snippet of org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream

    private void testExtraFlags(final int compressionLevel, final int flag) throws Exception {

final FileInputStream fis = new FileInputStream(getFile("test3.xml"));

byte[] content;

try {

content = IOUtils.toByteArray(fis);

} finally {

fis.close();

}



final ByteArrayOutputStream bout = new ByteArrayOutputStream();



final GzipParameters parameters = new GzipParameters();

parameters.setCompressionLevel(compressionLevel);

final GzipCompressorOutputStream out = new GzipCompressorOutputStream(bout, parameters);

IOUtils.copy(new ByteArrayInputStream(content), out);

out.flush();

out.close();



assertEquals("extra flags (XFL)", flag, bout.toByteArray()[8]);

}


   Like      Feedback      org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream


 Sample 14. Code Sample / Example / Snippet of org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream

    public void testCompressionMethod() throws Exception {

final ZipArchiveOutputStream zos =

new ZipArchiveOutputStream(new ByteArrayOutputStream());

final ZipArchiveEntry entry = new ZipArchiveEntry("foo");

assertEquals(-1, entry.getMethod());

assertFalse(zos.canWriteEntryData(entry));



entry.setMethod(ZipEntry.STORED);

assertEquals(ZipEntry.STORED, entry.getMethod());

assertTrue(zos.canWriteEntryData(entry));



entry.setMethod(ZipEntry.DEFLATED);

assertEquals(ZipEntry.DEFLATED, entry.getMethod());

assertTrue(zos.canWriteEntryData(entry));



entry.setMethod(6);

assertEquals(6, entry.getMethod());

assertFalse(zos.canWriteEntryData(entry));

zos.close();

}


   Like      Feedback      org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
 Sample 15. Code Sample / Example / Snippet of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream

    public void shouldUseSpecifiedEncodingWhenReadingGNULongNames()

throws Exception {

final ByteArrayOutputStream bos = new ByteArrayOutputStream();

final String encoding = CharsetNames.UTF_16;

final String name = "1234567890123456789012345678901234567890123456789"

+ "01234567890123456789012345678901234567890123456789"

+ "01234567890u00e4";

final TarArchiveOutputStream tos =

new TarArchiveOutputStream(bos, encoding);

tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

TarArchiveEntry t = new TarArchiveEntry(name);

t.setSize(1);

tos.putArchiveEntry(t);

tos.write(30);

tos.closeArchiveEntry();

tos.close();

final byte[] data = bos.toByteArray();

final ByteArrayInputStream bis = new ByteArrayInputStream(data);

final TarArchiveInputStream tis =

new TarArchiveInputStream(bis, encoding);

t = tis.getNextTarEntry();

assertEquals(name, t.getName());

tis.close();

}


   Like      Feedback      org.apache.commons.compress.archivers.tar.TarArchiveOutputStream


 Sample 16. Code Sample / Example / Snippet of org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream

    public void readOfLength0ShouldReturn0() throws Exception {

final byte[] rawData = new byte[1048576];

for (int i=0; i < rawData.length; ++i) {

rawData[i] = (byte) Math.floor(Math.random()*256);

}



final ByteArrayOutputStream baos = new ByteArrayOutputStream();

final BZip2CompressorOutputStream bzipOut = new BZip2CompressorOutputStream(baos);

bzipOut.write(rawData);

bzipOut.flush();

bzipOut.close();

baos.flush();

baos.close();



final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

final BZip2CompressorInputStream bzipIn = new BZip2CompressorInputStream(bais);

final byte[] buffer = new byte[1024];

Assert.assertEquals(1024, bzipIn.read(buffer, 0, 1024));

Assert.assertEquals(0, bzipIn.read(buffer, 1024, 0));

Assert.assertEquals(1024, bzipIn.read(buffer, 0, 1024));

bzipIn.close();

}


   Like      Feedback      org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream


 Sample 17. Code Sample / Example / Snippet of org.apache.commons.compress.utils.CountingOutputStream

    public void output() throws Exception {

final ByteArrayOutputStream bos = new ByteArrayOutputStream();

final CountingOutputStream o = new CountingOutputStream(bos);

o.write(1);

assertEquals(1, o.getBytesWritten());

o.write(new byte[] { 2, 3 });

assertEquals(3, o.getBytesWritten());

o.write(new byte[] { 2, 3, 4, 5, }, 2, 1);

assertEquals(4, o.getBytesWritten());

o.count(-1);

assertEquals(4, o.getBytesWritten());

o.count(-2);

assertEquals(2, o.getBytesWritten());

o.close();

assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bos.toByteArray());

}


   Like      Feedback      org.apache.commons.compress.utils.CountingOutputStream


 Sample 18. Code Sample / Example / Snippet of org.apache.commons.compress.compressors.deflate.DeflateCompressorOutputStream

    public void canReadASingleByteFlushAndFinish() throws IOException {

final ByteArrayOutputStream bos = new ByteArrayOutputStream();

final DeflateCompressorOutputStream cos = new DeflateCompressorOutputStream(bos);

cos.write(99);

cos.flush();

cos.finish();

Assert.assertTrue(bos.toByteArray().length > 0);

cos.close();

}


   Like      Feedback      org.apache.commons.compress.compressors.deflate.DeflateCompressorOutputStream


 Sample 19. GunZip file using Apache Commons


   public void testGzipCreation() throws Exception {
final File input = getFile("test1.xml");
final File output = new File(dir, "test1.xml.gz");
final OutputStream out = new FileOutputStream(output);
try {
final CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream("gz", out);
try {
IOUtils.copy(new FileInputStream(input), cos);
} finally {
cos.close();
}
} finally {
out.close();
}
}

   Like      Feedback     Zip file using Apache Commons   Compress file using Apache Commons   GunZip file using Apache Commons  Code Sample / Example / Snippet of org.apache.commons.compress.compressors.CompressorOutputStream  IOUtils.copy



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus