Share

Search Java Code Snippets


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





#Java - Code Snippets for '#InputStream' - 23 code snippet(s) found

 Sample 1. Load XLS file and print first column using poi XSSFWorkbook

public XSSFWorkbook loadXLSAndPrintFirstColumn(File xlsFile) {
   InputStream inputStream = null;
   try {
      inputStream = new FileInputStream(xlsFile);
   XSSFWorkbook xssFWorkbook = new XSSFWorkbook(inputStream);
      Sheet sheet = workbook.getSheetAt(workbook.getActiveSheetIndex());
      for (int index = 0; index < sheet.getPhysicalNumberOfRows(); index++) {
      try {
         Row row = sheet.getRow(index);
            System.out.println(row.getCell(0,Row.CREATE_NULL_AS_BLANK).getStringCellValue()));   
      } catch (Exception e) {
      System.out.println("Exception");
   }
}

}

   Like      Feedback     readinf xls  reading excel  apache poi  XSSFWorkbook  InputStream


 Sample 2. Load Properties using Property Class

private static Properties props = null;
   
private static void loadProperties(){
   try {
      if(props == null || props1 == null || props2 == null){
         props = new Properties();
         props.load(new FileInputStream(Constants.PROP_BASE_DIR + Constants.EMPLOYEE_REPORTING_PROP));
      } catch (FileNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

   }
}

   Like      Feedback     property class  properties  FileInputStream  file handling


 Sample 3. Usage of java.io.FileInputStream

FileInputStream fis = new FileInputStream("mypodcast.mp3");
InputStreamRequestEntity re = new InputStreamRequestEntity(fis, "audio/mp3");

   Like      Feedback     ileInputStrea


 Sample 4. Usage of java.io.ByteArrayInputStream

ByteArrayInputStream bais =
new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
result = ois.readObject();

   Like      Feedback     ByteArrayInputStream  java.io  input stream  ObjectInputStream


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. Usage of org.apache.hadoop.hdfs.server.namenode.FSEditLog.EditLogFileInputStream

EditLogFileInputStream edits =
new EditLogFileInputStream(getImageFile(sd, NameNodeFile.EDITS));
numEdits = FSEditLog.loadFSEdits(edits);
edits.close();
File editsNew = getImageFile(sd, NameNodeFile.EDITS_NEW);
if (editsNew.exists() && editsNew.length() > 0) {
edits = new EditLogFileInputStream(editsNew);
numEdits += FSEditLog.loadFSEdits(edits);
edits.close();

   Like      Feedback     EditLogFileInputStream  Apache Hadoop


 Sample 6. Code Sample / Example / Snippet of org.apache.hadoop.fs.FSDataInputStream

    private void init() throws IOException {

if (reader == null) {

FSDataInputStream in = fs.open(file);

in.seek(segmentOffset);

reader = new Reader<K, V>(conf, in, segmentLength, codec);

}

}


   Like      Feedback      org.apache.hadoop.fs.FSDataInputStream


 Sample 7. Code Sample / Example / Snippet of java.util.zip.GZIPInputStream

  private static CSVReader openCsv(File file) throws IOException {

final Reader fileReader;

if (file.getName().endsWith(".gz")) {

final GZIPInputStream inputStream =

new GZIPInputStream(new FileInputStream(file));

fileReader = new InputStreamReader(inputStream);

} else {

fileReader = new FileReader(file);

}

return new CSVReader(fileReader);

}


   Like      Feedback      java.util.zip.GZIPInputStream


 Sample 8. Code Sample / Example / Snippet of java.security.DigestInputStream

    private String getDigest(InputStream is) throws Exception {

DigestInputStream dis = new DigestInputStream(is, MessageDigest.getInstance("MD5"));

while (dis.read() != -1) {

}

dis.close();

return new String(dis.getMessageDigest().digest());

}


   Like      Feedback      java.security.DigestInputStream


 Sample 9. Code Sample / Example / Snippet of java.io.ByteArrayInputStream

    protected final void importSingleUser(Repository userRepository, String userName, String password) throws Exception {

ByteArrayInputStream bis = new ByteArrayInputStream((

"<roles>" +

"<user name="" + userName + "">" +

"<properties><username>" + userName + "</username></properties>" +

"<credentials><password type="String">" + password + "</password></credentials>" +

"</user>" +

"</roles>").getBytes());



Assert.assertTrue("Committing test user data failed!", userRepository.commit(bis, userRepository.getRange().getHigh()));

}


   Like      Feedback      java.io.ByteArrayInputStream


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.FileInputStream

    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.FileInputStream


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

    public static boolean filesDiffer(File first, File second) throws Exception {

if (first.length() != second.length()) {

return true;

}

InputStream firstStream = new FileInputStream(first);

InputStream secondStream = new FileInputStream(second);

try {

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

if (firstStream.read() != secondStream.read()) {

return false;

}

}

return true;

}

finally {

try {

firstStream.close();

}

finally {

secondStream.close();

}

}

}


   Like      Feedback      java.io.InputStream


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

    protected void checkArchiveContent(final File archive, final List<String> expected)

throws Exception {

final InputStream is = new FileInputStream(archive);

try {

final BufferedInputStream buf = new BufferedInputStream(is);

final ArchiveInputStream in = factory.createArchiveInputStream(buf);

this.checkArchiveContent(in, expected);

} finally {

is.close();

}

}


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


 Sample 13. Code Sample / Example / Snippet of org.apache.commons.compress.archivers.zip.ZipArchiveInputStream

    private void testZipStreamWithImplodeCompression(final String filename, final String entryName) throws IOException {

final ZipArchiveInputStream zin = new ZipArchiveInputStream(new FileInputStream(new File(filename)));

final ZipArchiveEntry entry = zin.getNextZipEntry();

assertEquals("entry name", entryName, entry.getName());

assertTrue("entry can't be read", zin.canReadEntryData(entry));

assertEquals("method", ZipMethod.IMPLODING.getCode(), entry.getMethod());



final InputStream bio = new BoundedInputStream(zin, entry.getSize());



final ByteArrayOutputStream bout = new ByteArrayOutputStream();

final CheckedOutputStream out = new CheckedOutputStream(bout, new CRC32());

IOUtils.copy(bio, out);



out.flush();



assertEquals("CRC32", entry.getCrc(), out.getChecksum().getValue());

}


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


 Sample 14. Code Sample / Example / Snippet of org.apache.commons.compress.archivers.arj.ArjArchiveInputStream

    public void testArjUnarchive() throws Exception {

final StringBuilder expected = new StringBuilder();

expected.append("test1.xml<?xml version="1.0"?> ");

expected.append("<empty/>test2.xml<?xml version="1.0"?> ");

expected.append("<empty/> ");





final ArjArchiveInputStream in = new ArjArchiveInputStream(new FileInputStream(getFile("bla.arj")));

ArjArchiveEntry entry;



final StringBuilder result = new StringBuilder();

while ((entry = in.getNextEntry()) != null) {

result.append(entry.getName());

int tmp;

while ((tmp = in.read()) != -1) {

result.append((char) tmp);

}

assertFalse(entry.isDirectory());

}

in.close();

assertEquals(result.toString(), expected.toString());

}


   Like      Feedback      org.apache.commons.compress.archivers.arj.ArjArchiveInputStream


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.cpio.CpioArchiveInputStream

    public void testCpioUnarchive() throws Exception {

final StringBuilder expected = new StringBuilder();

expected.append("./test1.xml<?xml version="1.0"?> ");

expected.append("<empty/>./test2.xml<?xml version="1.0"?> ");

expected.append("<empty/> ");





final CpioArchiveInputStream in = new CpioArchiveInputStream(new FileInputStream(getFile("bla.cpio")));

CpioArchiveEntry entry;



final StringBuilder result = new StringBuilder();

while ((entry = (CpioArchiveEntry) in.getNextEntry()) != null) {

result.append(entry.getName());

int tmp;

while ((tmp = in.read()) != -1) {

result.append((char) tmp);

}

}

in.close();

assertEquals(result.toString(), expected.toString());

}


   Like      Feedback      org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream


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

    public void testBzip2Unarchive() throws Exception {

final File input = getFile("bla.txt.bz2");

final File output = new File(dir, "bla.txt");

final InputStream is = new FileInputStream(input);

final CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream("bzip2", is);

final FileOutputStream os = new FileOutputStream(output);

IOUtils.copy(in, os);

is.close();

os.close();

}


   Like      Feedback      org.apache.commons.compress.compressors.CompressorInputStream


 Sample 17. Code Sample / Example / Snippet of org.apache.commons.compress.archivers.tar.TarArchiveInputStream

    public void readSimplePaxHeader() throws Exception {

final InputStream is = new ByteArrayInputStream(new byte[1]);

final TarArchiveInputStream tais = new TarArchiveInputStream(is);

final Map<String, String> headers = tais

.parsePaxHeaders(new ByteArrayInputStream("30 atime=1321711775.972059463 "

.getBytes(CharsetNames.UTF_8)));

assertEquals(1, headers.size());

assertEquals("1321711775.972059463", headers.get("atime"));

tais.close();

}


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


 Sample 18. Code Sample / Example / Snippet of org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream

    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.BZip2CompressorInputStream


 Sample 19. Code Sample / Example / Snippet of org.apache.commons.compress.utils.BitInputStream

    public void testClearBitCache() throws IOException {

final BitInputStream bis = new BitInputStream(getStream(), ByteOrder.LITTLE_ENDIAN);

assertEquals(0x08, bis.readBits(4));

bis.clearBitCache();

assertEquals(0, bis.readBits(1));

bis.close();

}


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


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

    public void writeTo(final ZipArchiveOutputStream target) throws IOException {

backingStore.closeForWriting();

final InputStream data = backingStore.getInputStream();

for (final CompressedEntry compressedEntry : items) {

final BoundedInputStream rawStream = new BoundedInputStream(data, compressedEntry.compressedSize);

target.addRawArchiveEntry(compressedEntry.transferToArchiveEntry(), rawStream);

rawStream.close();

}

data.close();

}


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


 Sample 21. Code Sample / Example / Snippet of org.apache.commons.compress.archivers.dump.DumpArchiveInputStream

    public void testConsumesArchiveCompletely() throws Exception {

final InputStream is = DumpArchiveInputStreamTest.class

.getResourceAsStream("/archive_with_trailer.dump");

final DumpArchiveInputStream dump = new DumpArchiveInputStream(is);

while (dump.getNextDumpEntry() != null) {

}

final byte[] expected = new byte[] {

'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', ' '

};

final byte[] actual = new byte[expected.length];

is.read(actual);

assertArrayEquals(expected, actual);

dump.close();

}


   Like      Feedback      org.apache.commons.compress.archivers.dump.DumpArchiveInputStream


 Sample 22. Code Sample / Example / Snippet of org.apache.commons.compress.compressors.snappy.FramedSnappyCompressorInputStream

    public void testRemainingChunkTypes() throws Exception {

final FileInputStream isSz = new FileInputStream(getFile("mixed.txt.sz"));

final ByteArrayOutputStream out = new ByteArrayOutputStream();

try {

final FramedSnappyCompressorInputStream in = new FramedSnappyCompressorInputStream(isSz);

IOUtils.copy(in, out);

out.close();

} finally {

isSz.close();

}



assertArrayEquals(new byte[] { '1', '2', '3', '4',

'5', '6', '7', '8', '9',

'5', '6', '7', '8', '9',

'5', '6', '7', '8', '9',

'5', '6', '7', '8', '9',

'5', '6', '7', '8', '9', 10,

'1', '2', '3', '4',

'1', '2', '3', '4',

}, out.toByteArray());

}


   Like      Feedback      org.apache.commons.compress.compressors.snappy.FramedSnappyCompressorInputStream


 Sample 23. Code Sample / Example / Snippet of org.apache.commons.compress.compressors.deflate.DeflateCompressorInputStream

    public void availableShouldReturnNonZero() throws IOException {

final File input = AbstractTestCase.getFile("bla.tar.deflatez");

final InputStream is = new FileInputStream(input);

try {

final DeflateCompressorInputStream in =

new DeflateCompressorInputStream(is);

Assert.assertTrue(in.available() > 0);

in.close();

} finally {

is.close();

}

}


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



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