Skip to content
This repository was archived by the owner on Nov 10, 2017. It is now read-only.
This repository was archived by the owner on Nov 10, 2017. It is now read-only.

I/O Streams #133

@PanagiotisDrakatos

Description

@PanagiotisDrakatos

I found,that your way to deal with the raw bytes of elm327 not so efficiency, this is because you deal with characters (read until '>' appear) so you need a bridge from byte streams to character streams. InputStreamReader are used specifically to deal with characters.

For more efficiency, consider wrapping an InputStreamReader within a BufferedReader. For example take consider the below example to read the data from ELM327 Protocol.

public String readUntilChar( InputStream in,char target) {
        StringBuilder sb = new StringBuilder();

        try {
            int bufferSize = 8 * 1024;
            BufferedReader buffer = new BufferedReader(new InputStreamReader(in), bufferSize);
            int r;
            while ((r = buffer.read()) != -1) {
                char c = (char) r;
                if (c == target)
                    break;
                sb.append(c);
            }
            System.out.println(sb.toString());
        } catch (IOException e) {
            // Error handling
        }

        return sb.toString();
    }

The BufferedReader class (java.io.BufferedReader) supplies buffering to your Reader. Thus, buffering can speed up I/O stream quite a bit. So instead read one character at a time from the network, the BufferedReader reads a larger block at a time. This is typically much faster and more efficient, especially for larger data amounts.The efficient conversion of bytes to characters, carried out with more bytes to read ahead from the stream than are necessary to satisfy the current read process.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions