Some general utility functions for dealing with Streams : Stream « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » File Input Output » StreamScreenshots 
Some general utility functions for dealing with Streams
   
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/*
 * StreamUtility.java
 *
 * Created on October 26, 2002, 4:05 PM
 *
 * Copyright (C) 2002  Robert Cooper, Temple of the Screaming Penguin
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


/** This class contains some general utility functions for dealing with Streams.
 * <cite>by Robert Cooper, Temple of the Screaming Penguin. Distributed under the
 * terms of the <a href="http://www.gnu.org/licenses/lgpl.html">GNU Lesser General
 * Public License</a>.</cite>
 @version $Rev: 55 $
 @author  <a href="mailto:cooper@screaming-penguin.com>Robert Cooper</a>, 2002
 */
 class StreamUtility {
    /**
     * default read size for stream copy
     */
    public static final int DEFAULT_BUFFER_SIZE = 1024;

    /** Copies the data from an InputStream object to an OutputStream object.
     @param sourceStream The input stream to be read.
     @param destinationStream The output stream to be written to.
     @return int value of the number of bytes copied.
     @exception IOException from java.io calls.
     */
    public static int copyStream(InputStream sourceStream,OutputStream destinationStreamthrows IOException {
        int bytesRead = 0;
        int totalBytes = 0;
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

        while(bytesRead >= 0) {
            bytesRead = sourceStream.read(buffer,0,buffer.length);

            if(bytesRead > 0) {
                destinationStream.write(buffer,0,bytesRead);
            }

            totalBytes += bytesRead;
        }

        return totalBytes;
    }

    /**
     * Converts little endian bytes to a int
     @param value byte array to read
     @return integer value of the byte array
     */
    public static int littleEndianToInt(byte[] value) {
        return ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getInt();
    }

    /**
     * Converts little endian bytes to a long
     @param value byte array to read
     @return long value of the byte array
     */
    public static long littleEndianToLong(byte[] value) {
        return ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).getLong();
    }

    /**
     * Converts little endian Unicode bytes to a String
     @param value byte array of Unicode Little Endian
     @return String value of the byte array
     */
    public static String littleEndianToString(byte[] value) {
        return ByteBuffer.wrap(value).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer().toString();
    }

    /**
     * Read an entire stream and return it as a String
     *
     @param sourceStream the InputStream to read.
     @return a String containing the contents of the stream.
     @exception IOException from java.io calls.
     */
    public static String readStreamAsString(InputStream sourceStreamthrows IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(sourceStream));
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        copyStream(sourceStream,output);

        return output.toString();
    }

    /**
     * This is a quick method to get a subset of a byte array as a new array
     @param start index of the source to begin reading from
     @param count number of bytes to copy
     @param source byte array to read from
     @return byte array of size <code>count</code>
     */
    public static byte[] subset(int start,int count,byte[] source) {
        byte[] ret = new byte[count];

        for(int i = 0; i < count; i++) {
            ret[i= source[start + i];
        }

        return ret;
    }
}

   
    
    
  
Related examples in the same category
1. Show the content of a file
2. Utilities related to file and stream handling.
3. Utility functions related to Streams
4. Utility methods for handling streams
5. Various utility methods that have something to do with I/O
6. General IO Stream manipulation
7. General IO stream manipulation utilities
8. Count the number of bytes read through the stream
9. Count OutputStream
10. File utilities for file read and write
11. An InputStream class that terminates the stream when it encounters a particular byte sequence.
12. An InputStream that implements HTTP/1.1 chunking
13. An OutputStream which relays all data written into it into a list of given OutputStreams
14. Utility code for dealing with different endian systems
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.