G
G
gleendo2017-03-12 13:03:43
Java
gleendo, 2017-03-12 13:03:43

Why are unfiltered bytes written to the filter?

Why, if after writing to display the recorded data, it turns out that unfiltered bytes (in this case, zeros) were written at the end. And if you remove the output of the bytes written to the file, then the method works correctly and all bytes except zeros are written to the file. How does the output of data affect the recording of this data?

import java.io.*;
import java.util.Arrays;
import java.util.Random;

public class App {
    public static void main(String[] args) throws IOException {
        byte[] arr = new byte[10000];

        for (int i = 0; i < arr.length; i++) {
            arr[i] = (byte) (Math.random() * 2);
        }

        try (InputStream is = new ByteArrayInputStream(arr); OutputStream fos = new FileOutputStream("c://outfile.txt")) {
            long startTime = System.nanoTime();

            Filters.zeroFilter(is, fos, 1024);

            long deltaTime = System.nanoTime() - startTime;

            System.out.println("Скорость работы: " + deltaTime);
        }

        try (InputStream fis = new FileInputStream("c://outfile.txt")) { // Если убрать этот вывод, то все работает норм
            int value;

            while ((value = fis.read()) != -1) {
                System.out.print(value);
            }
        }
    }
}

class Filters {
    public static void zeroFilter(InputStream src, OutputStream dst, int buffSize)  throws IOException {
        try (OutputStream os = new BufferedOutputStream(dst)) {
            final byte STATE_ZERO = 0;
            final byte STATE_NUMBERS = 1;

            int count;
            byte[] buffer = new byte[buffSize];

            while ((count = src.read(buffer)) != -1) {
                byte state = STATE_ZERO;
                int fromIndex = -1;

                for (int index = 0; index < count; index++) {
                    byte elem = buffer[index];

                    switch (state) {
                        case STATE_ZERO:
                            if (elem == 0) {
                                state = STATE_ZERO;
                            } else {
                                state = STATE_NUMBERS;

                                fromIndex = index;
                            }

                            break;
                        case STATE_NUMBERS:
                            if (elem == 0) {
                                state = STATE_ZERO;

                                os.write(Arrays.copyOfRange(buffer, fromIndex, index));
                            } else {
                                state = STATE_NUMBERS;
                            }
                    }
                }

                if (state == STATE_NUMBERS) {
                    os.write(Arrays.copyOfRange(buffer, fromIndex, buffer.length));
                }
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#algooptimize #bottize, 2017-03-12
@user004

1. Using BufferedOutputStream it was possible to keep within a couple of lines. (zeroFilter) 2.
The input data is random, the output result is also possible .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question