Custom implementation of InputStream

To send data to a file on my FTP server, I need to create a custom InputStream implementation that reads the database data line by line, converts it to CSV and publishes it using read () methods: from the database, I get a List<Application>data object. For each object, ApplicationI want to create a line in a CSV file.

My idea is to load all the data in the constructor and then override the read method. Do I need to override all InputStream methods? I tried a few examples, but I couldn’t - could you end up giving me a link to it?

+3
source share
7 answers

For possibly big data, you can use com.google.common.io.FileBackedOutputStream from guava .

Javadoc: OutputStream, which starts buffering into a byte array, but switches to file buffering when the data reaches a custom size.

Using out.getSupplier().getInput(), you get your InputStream.

+3
source

You just do not implement the method read()without parameters . All other methods are implemented as calls to this method. For performance reasons (and even simplicity of implementation), it would be easier to implement a method with three argumentsread() and re-execute -args read()from the point of view of this method.

+7
source

, InputStream.

  • (). :

    InputStream 0. .

    , , false. , InputStream inputStreamReader, false reader.ready().

  • return -1 read(). :

    , , -1. , , .

    read(), , return -1 . , read(byte b[], int off, int len) :

    for (; i < len ; i++) {// default len is a relative large number (8192 - readPosition)
        c = read();
        if (c == -1) {
            break;
        }
        b[off + i] = (byte)c;
    }
    

    ( ) , readLine(), read() ..

+2

, :

  • CSV ()
  • ( String.getBytes(encoding))
  • ByteArrayInputStream
0

InputStream. ByteArrayInputStream, - :

public static InputStream createStream(){
    final String csv = createCsvFromDataBaseValues();
    return new ByteArrayInputStream(csv.getBytes());
}

:

- , .

, , InputStream. .

0

custon? csv, , ftp-?

0

, InputStream:

  • (). -1, . , , -1 ( ).
  • (). 0, . (1 ).
0

Source: https://habr.com/ru/post/1787951/


All Articles