Java. IO. How are types converted?

String check = "word";
void dropAbuses(InputStream src, OutputStream dst) throws IOException {
    while (true) {
        int element = src.read();
        if (element != -1) {
            dst.write(element);
        } else {
            break;
        }
    }
}

I have this code, but I can't understand a few things that come down to one thing:

  1. If the .write(byte[]) method requires an array of bytes as an argument (which is quite logical and the development environment highlights this), then how does it agree with what I send it int?

  2. How do I turn the int obtained from src.read() back into a byte array?

Just why am I asking? I need to before I I will do the operation dst.write(element) to check that "word" is not included there and if it is included, then do not copy and skip.

That is, now the method just copies and I need a filter method and the question is how do I implement this filtering? The first thing that comes is "word" to break up into bytes byte[] bytes = "word".getBytes() and compare with the result src.read() and it returns an int infection! And what to do???

Of course, you can try to represent "word" as int. But still, can't I read the result from InputStream, an interface designed to work with byte streams, represent as an array of bytes? Help me understand what I did not understand?

void dropAbuses(InputStream src, OutputStream dst, String[] words) {
        try (Close close = new Close()) {
            int[] forCheck = new int[words.length];
            for (int i = 0; i != words.length; i++) {
                forCheck[i] = new ByteArrayInputStream(words[i].getBytes("UTF8")).read();
            }

            while (true) {
                boolean equal = true;
                int element = src.read();
                if (element != -1) {
                    int i;
                    for (i = 0; i != words.length; i++) {
                        if (forCheck[i] == element) {
                            equal = false;
                            break;
                        }
                    }
                    if (equal) dst.write(element);
                } else {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

Yes, I had to do this((( but still it does not delete words but separate letters. And with words it does not work. And by the way, only the English alphabet renderit can who knows what the nuance is?

 2
Author: post_zeew, 2016-12-01

2 answers

InputStream - Abstract class for reading bytes. Its read method is overloaded. If you use it without arguments, it will return a byte expanded to int. If you pass an array of bytes to it, it will return the number of bytes actually read.

OutputStream - Abstract class for writing bytes. Its write method is overloaded and can accept an array byte or a single byte that automatically expands to int. Returns nothing.

You can read more about them here and here.

Here is a good video lecture on this topic in Russian.

About your task... What prevents you from reading a stream of bytes, then making a string out of them (new String(bytes[])? Next, you can call the replaceAll method. For example, this method will return you a string without word: mystring.replaceAll("word", ""). Next, you call the getBytes method on the received string and you can do whatever you want with the received bytes. For example, write to the output stream.

I will demonstrate my idea on example. I have a file fil1.txt with this content:

This word has to be deleted!

I run it through such a test class:

public class Test {

    public static void main(String[] args) throws IOException
    {
        FileInputStream fileInputStream = new FileInputStream("/home/someone/file1.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("/home/someone/file2.txt");

        dropAbuses(fileInputStream, fileOutputStream);

        fileInputStream.close();
        fileOutputStream.close();
    }

    static void dropAbuses(InputStream src, OutputStream dst) throws IOException {

        byte bytes[] = new byte[src.available()];
        src.read(bytes);
        String stringWithoutWord = new String(bytes).replaceAll("word ", "");

        dst.write(stringWithoutWord.getBytes());
        dst.flush();
    }

}

Content file2.txt:

This has to be deleted!

The word word is missing. That's what was required. :)

 3
Author: faoxis, 2016-12-02 07:06:47

If the .write(byte[]) method requires an array of bytes as an argument (which is quite logical and the development environment highlights this), then how does it agree with what I send it int?

OutputStream – an abstract class whose write(...) method has three signatures:

  1. void write(byte[] b);
  2. void write(byte[] b, int off, int len);
  3. abstract void write(int b).

In your method:

void dropAbuses(InputStream src, OutputStream dst, String[] words)

As dst You pass any non-abstract subclass of the class OutputStream, which implements the void write(int b) method, which is why you can pass a int type parameter to the write(...) method.

For example, if you pass an object of the DataOutputStream class to your method as dst, then calling dst.write(5) will call the void write(int b) method of the DataOutputStream class.

How do I turn the int obtained from src.read() back into an array of bytes.

To an array of bytes? What for? The read() method of the InputStream class reads one byte and returns it int - ovoe performance.

To get an array of bytes from InputStream, you can use one of the methods:

  1. int read(byte[] b);
  2. int read(byte[] b, int off, int len);

When using these methods, the read bytes will be written to byte[] b, and these methods will return the number of bytes read.

 1
Author: post_zeew, 2016-12-01 13:50:37