Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

[Specialists] Java Streams Tutorial

[Image: er62IVW.png]
This tutorial has been brought to you by the Specialists.
Difficulty: Easy


Foreword
First off all, to those of you waiting for the bytecode tutorials to continue. I will do those in another thread. They are just more work than this one and need more time.

In this tutorial you will learn how streams work and how to use them. We will also be going over examples such as copying files, downloading files and more.
[Image: 6XDSKOv.png]
What are Streams?
A stream is something that can transfer data. These are the basics of streams, there exists many more stream types but all streams rely on the simple definition of transfering data.

Here is a simple picture which shows an example stream:
[img]http://docs.oracle.com/javase/tutorial/figures/essential/io-ins.gif[/img]
Source: Click

You probably already heard about music streaming services like Spotify. These stream/transfer data to you which in this case is music.
When you save a file you are also streaming as it writes something from your program to a file on a disk.

I hope that I explained what streaming is well enough. If you have any questions you can always comment on the thread and I will be glad to help you out.

[Image: 6XDSKOv.png]
Input/Output?
There exist two kind of streams; input and output streams. An input stream can only READ and an output stream can only WRITE. You should know the differences because else your program will not work.
[Image: 6XDSKOv.png]
Example
I will now give an example on how to download a file using streams. Everything is explained in the code so it should be easy to follow.

Code
package the.specialists.looka.testing;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

/**
* Our test class
* @author Luca
*
*/
public class Test {

    /**
     * Code entry point
     * @param args
     */
    public static void main(String[] args) {
        try {
            /*
             * The link we're going to download
             */
            URL downloadUrl = new URL("http://pastebin.com/raw.php?i=xcyMsF8N");

            /*
             * The file we're going to save it to
             */
            File saveFile = new File("thingy.txt");

            /*
             * If the save file already exists delete it(optional)
             */
            if(saveFile.exists())
                saveFile.delete();

            /*
             * Create a new saveFile
             */
            saveFile.createNewFile();

            /*
             * Get the inputstream from the url we're going to download
             */
            InputStream downloadInputStream = downloadUrl.openStream();

            /*
             * Get the outputstream for the file so we can write to it
             */
            FileOutputStream fileOutputStream = new FileOutputStream(saveFile);

            /*
             * Read a single byte
             */
            int read = downloadInputStream.read();

            /*
             * Check if the byte read is equal to -1
             * -1 is most oftenly used as indicator of end of stream so we know we can stop reading.
             */
            while(read != -1) {
                /*
                 * Write the read byte to the file
                 */
                fileOutputStream.write(read);
                /*
                 * Read the next byte
                 */
                read = downloadInputStream.read();
            }

            /*
             * Be sure to close BOTH streams when done.
             */
            downloadInputStream.close();
            fileOutputStream.close();

            System.out.println("Done downloading!");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

This little example will download a text file from pastebin into a file called thingy.txt
[Image: 6XDSKOv.png]
Wait a minute, I've heard about readers and writers too, why not use those?
If you did not know yet, there is also something called readers and writers in Java. An example of this is the FileWriter. This will also allow you to write just like an output stream. However there is one big difference. Readers and writers are based around reading/writing characters such as text files and streams are there to read bytes. You can choose which one to use yourself. I choose readers/writers for text files and streams for handling io things such as copying files. I will not go into readers and writers in this tutorial so we will forget about those now.
[Image: 6XDSKOv.png]
What kind of streams are there?
There are a lot of streams in Java ranging from sending entire Java objects to just sending a single byte. In the java.io package you will be able to find (almost)all streams that Java provides.
[Image: 6XDSKOv.png]
Obtaining a Stream
If you for example want to write to a file you first need to get an instance of FileOutputStream.
After you got the instance of FileOutputStream you can write to the file by calling a write method.
However there are different ways to obtain an instance of a stream.
With the example I gave earlier I used the method openStream to obtain an InputStream instance. Most of the time you can just get a stream with the new keyword though like with the FileOutputStream.
[Image: 6XDSKOv.png]
Reading & Writing
There are different ways to read and write with streams.
The default read methods are the following:
- read:
This will read the next byte from the input stream.

- read(byte[] b)
This will fill the byte array with read bytes. For example; if I have a file containing some information and want to have 10 bytes read I would create a byte array with the size of 10 and then call this method to fill/read it into the byte array.

- read(byte[] b, int off, int len)
This is the more advanced version of read(byte[] b). Here you can specify where to start reading and how much to read from the input stream. For example if I have a large file and want to read from 500 until 510 I would call the method like this:

Code
inputStream.read(bytes, 500, 10);

The first parameter of this method is the array to store it in. The second parameter is where to start reading from the stream(Also know as offset) and the last parameter is the length to read which in this case is 10.

Now you want to write your information to something using the OutputStream. There are 3 default write methods just like InputStream has 3 default read methods. These methods are almost identical. The only difference is that the output stream writes and the input stream reads. So lets check the methods out:

- write(byte[] b)
When you use this method you are writing to something. Now lets take a simple text file as exmaple. If I have a text file that already says "I love" I would only have to write the bytes of " the specialists!".
This method will write everything at the end of the already written bytes.

- write(byte[] b, int off, int len)
This method looks a lot like the read(byte[] b, int off, int len) method. However this method will write the byte array at the given location(the off parameter) and will only write the specified length(the len parameter). For example; if I have a file of information and want to write something at index 23 and want to only write 10 bytes I would do the following:

Code
outputStream.write(bytes, 23, 10);

[Image: 6XDSKOv.png]
Flushing?
You have probably heard about the term flushing. What is it and why do we need it?
Flushing only exists in output streams. When you call a flush method it will write all the bytes that were buffered out. When you don't flush you may have the problem of not all bytes being written. For now you don't need to know anything else about flushing as you probably won't need it. You won't have to call flush a lot though. Only if you really need something written like when another program will be reading a file which your program writes.
[Image: 6XDSKOv.png]
Closing Streams
The last method you should call when you are done reading or writing is the close method. This method will close the stream preventing any reading or writing. You probably also know the problem when you try to delete a file and it's saying a program is using it? Well that's a stream being busy with that file then. When that stream is closed you will be able to delete the specific file.
If you have an output stream you might think you also have to call the flush method before calling the close method as some bytes may not be written. This is not the case as the close method in an output stream always calls the flush method.

[Image: 6XDSKOv.png]
Copying
In the example I gave earlier I copy data from an inputstream coming from pastebin to an outputstream which in this case is a file. To copy data from an input stream to an output stream you will have to read a byte then check if it's the indicator for end-of-stream and then write it to the output stream.

Here is another example:

Code
/**
* The streams, we will be copying the input stream over to the output stream.
*/
InputStream input = getYourInputStream();
OutputStream output = getYourOutputStream();

/**
* The current read byte
*/
int read = input.read();
/**
* Check if it's end of stream(-1 is end of stream in almost all cases)
*/
while(read != -1) {
    /*
     *Write the read byte
    */
    output.write(read);
    /**
     * Read the next byte from the input stream
    */
    read = input.read();
    /**
     * Repeat this until the end of stream is reached.
    */
}

[Image: 6XDSKOv.png]
AutoClosable
In Java 7 there was a new feature introduced named try-with-resource.
This allows you to skip the closing method. Here is an example that uses try-with-resource:

Code
static String readFirstLineFromFile(String path) throws IOException {
    try (BufferedReader br =
      new BufferedReader(new FileReader(path))) {
      return br.readLine();
    }
}

I do not have a lot to explain about this rather than showing a piece of code. If you have questions about this feel free to post on my thread!
[Image: 6XDSKOv.png]
Conclusion
I hope you enjoyed, if you still don't understand something please post it in this thread and don't pm me about it as some people may be experiencing the same problem and can then find the answer in this thread.

This is my first tutorial for The Specialists so please give me feedback on what I could improve on and tell me if you see any mistakes!

Thanks!
[Image: 6XDSKOv.png]
Challenge
Create a program that can backup a folder.

This thread was reviewed by nJoyMe before posting.
Nice tutorial. It seems " Specialists ", is a different and experienced group. I like being member of a programming group, but i have not the post - requirements at this moment.

Btw, thank you for the contribution,
Psycho_Coder.
Nip-picking mode activated

- You shouldn't use JavaDoc comments for single line comments. Use // or at the very most multi-line comments where it makes sense ( /* comment / and NOT /* Javadoc */)
- If you share catch logic (here, all you do is print the stack trace) you should use try with multi-catch for easier reading. This is a Java 7 feature if I remember right.
- Why do you prefer streams over readers/writes when working with text files?
- You should mention the AutoCloasble interface ( http://docs.oracle.com/javase/7/docs/api...eable.html ) imo.

Fix these points and you're a step closer to calling yourself a specialist in entry level streams. :P
[Image: TEGE4Zx.png]

Puzzle level: 2/5
(Oct 16, 2014 10:32 AM)nOrr Wrote:  *Nip-picking mode activated*

- You shouldn't use JavaDoc comments for single line comments. Use // or at the very most multi-line comments where it makes sense ( /* comment / and NOT /* Javadoc */)
- If you share catch logic (here, all you do is print the stack trace) you should use try with multi-catch for easier reading. This is a Java 7 feature if I remember right.
- Why do you prefer streams over readers/writes when working with text files?
- You should mention the AutoCloasble interface ( http://docs.oracle.com/javase/7/docs/api...eable.html ) imo.

Fix these points and you're a step closer to calling yourself a specialist in entry level streams. :P

I'll improve on it when I get back on my pc. Maybe I'll add a piece about AutoCloseable. Oh and I choose readers/writers for text files and streams for handling io things such as copying files.
(Oct 16, 2014 10:54 AM)MrOrang Wrote:  I'll improve on it when I get back on my pc. Maybe I'll add a piece about AutoCloseable. Oh and I choose readers/writers for text files and streams for handling io things such as copying files.

Mention that in the post then. ;) Currently you have:

Quote:You can choose which one to use yourself. I prefer to use streams most of the time though. I will not go into readers and writers in this tutorial so we will forget about those now.

[Image: TEGE4Zx.png]

Puzzle level: 2/5
Nice thread, will be helpful for beginners.

Spoiler (Click to View)
(Oct 16, 2014 01:00 PM)Bibl Wrote:  
🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉:

Uhm... Yaaaarhh? 🤨

(Oct 16, 2014 02:21 PM)ObscureCoder Wrote:  OT: If you guys want a real challenge, actually try and do a JNI thread. JNI is a f*cking sour c*nt.

Is it? Why? I haven't actually worked with JNI, but what's "c*nty" about it?
[Image: TEGE4Zx.png]

Puzzle level: 2/5
(Oct 16, 2014 02:21 PM)ObscureCoder Wrote:  Nice thread, will be helpful for beginners.

Spoiler (Click to View)
I probably wont do JNI as I dont have any knowledge about the C language. Maybe someone else could do a tutorial about it if they want?
(Oct 16, 2014 02:24 PM)nOrr Wrote:  Is it? Why? I haven't actually worked with JNI, but what's "c*nty" about it?

Well, for a starter, since it's C++ you have to compile dlls and so files for both 32 bit and 64 bit which in my experience has been very frustrating with the compilers and IDEs I use. It's a real pain in the ass. A lot of people in this section talk about JNI but have never used it and if they had actually used it they'd probably stop talking about how 'great' it is for things like RATs etc. since it's a total nightmare in reality.

That's why. If you have the time and patience for it then go for it but it's a nightmare. I'm currently trying to compile a 64 bit dll using g++ for JNI so it will actually load in the 64bit JRE 1.8. I've had to turn to stackoverflow for help.
urm jni is only a problem is you're terrible at c/c++ otherwise its perfectly fine
(Oct 16, 2014 03:33 PM)Bibl Wrote:  urm jni is only a problem is you're terrible at c/c++ otherwise its perfectly fine

Obviously.

JNI takes a lot of work for 32 bit and 64 bit cross platform use.


(Oct 16, 2014 03:35 PM)ObscureCoder Wrote:  Obviously.

JNI takes a lot of work for 32 bit and 64 bit cross platform use.

I take this back actually.
JNI is a f*cking doddle if you install the correct f*cking C++ compiler xD
I was compiling for the wrong JVM arch.
(Oct 16, 2014 02:54 PM)ObscureCoder Wrote:  Well, for a starter, since it's C++ you have to compile dlls and so files for both 32 bit and 64 bit which in my experience has been very frustrating with the compilers and IDEs I use. It's a real pain in the ass. A lot of people in this section talk about JNI but have never used it and if they had actually used it they'd probably stop talking about how 'great' it is for things like RATs etc. since it's a total nightmare in reality.

That's why. If you have the time and patience for it then go for it but it's a nightmare. I'm currently trying to compile a 64 bit dll using g++ for JNI so it will actually load in the 64bit JRE 1.8. I've had to turn to stackoverflow for help.

You say this as if SO is a place like HF. :D

It sounds like a fun-but-frustrating project indeed. Surely it must be possible and surely there must exists tools to help you. If it's a common problem, there will be common solutions.

Edit:
(Oct 16, 2014 03:35 PM)ObscureCoder Wrote:  I take this back actually.
JNI is a f*cking doddle if you install the correct f*cking C++ compiler xD
I was compiling for the wrong JVM arch.

See? Told you!! 😝 🤣
[Image: TEGE4Zx.png]

Puzzle level: 2/5
(Oct 17, 2014 08:53 AM)nOrr Wrote:  You say this as if SO is a place like HF. :D

It kind of is though. You can post malware related questions and people will answer and not even care if it's for malicious purposes or not.
Awesome tutorial by a great programmer. Hope this can be used by everyone.
[Image: DTc14yu.png]
(Oct 17, 2014 10:03 AM)Matt AH Wrote:  Awesome tutorial by a great programmer. Hope this can be used by everyone.

Thanks man. I will be sure to continue tutorials like this.
whoring for bronze award

I'll also add a piece about AutoCloseable maybe later tonight or tomorrow.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Shell] by ________Apr 25, 2016 01:23 AM 8 169 Apr 25, 2016 01:23 AM
Last Post: ________
[C#] by ForeachJan 28, 2016 05:00 PM 4 221 Jan 28, 2016 05:00 PM
Last Post: Odus
[C] by Dickyb0bSueFeb 6, 2015 12:04 AM 7 1,064 Feb 6, 2015 12:04 AM
Last Post: 0x43