Why do the error and output messages in my program change the order that they appear in on each run?

StackOverflow https://stackoverflow.com/questions/22239947

  •  10-06-2023
  •  | 
  •  

سؤال

I use netbeans 7 as an IDE. Nearly each time I run my code, without making changes to the code or file I'm operating on - the error messages appear in different parts of the output. Why does this happen and how will it affect my coding further down the line?

for instance on one run:

the  character read at switch Dictionary is:  d
this is the length  8
buffered string is  announce
the  character read at switch decodeString is:  3
this is the length  36
buffered string is  http://bt1.archive.org:6969/announce
this is the length  13
buffered string is  announce-list
the  character read at switch List is:  l
the  character read at switch decodeString is:  3
this is the length  36
buffered string is  http://bt1.archive.org:6969/announce
Exception in thread "main" java.io.IOException: Problem parsing bencoded file
the  character read at switch decodeString is:  3
this is the length  36
buffered string is  http://bt2.archive.org:6969/announce
list object -------------->http://bt1.archive.org:6969/announce
list object -------------->http://bt2.archive.org:6969/announce
    at javaapplication1.DotTorrentBencodeReader.Decode(DotTorrentBencodeReader.java:61)
this is the length  7
buffered string is  comment
the  character read at switch decodeString is:  6
this is the length  692
    at javaapplication1.DotTorrentBencodeReader.decodeDictionary(DotTorrentBencodeReader.java:82)
    at javaapplication1.DotTorrentBencodeReader.Decode(DotTorrentBencodeReader.java:46)
    at javaapplication1.DotTorrentBencodeReader.decodeDictionary(DotTorrentBencodeReader.java:82)
    at javaapplication1.DotTorrentBencodeReader.Decode(DotTorrentBencodeReader.java:46)
    at javaapplication1.DotTorrentBencodeReader.main(DotTorrentBencodeReader.java:26)
buffered string is  This content hosted at the Internet Archive at http://archive.org/details/dopapod2014-02-25.elam260.sbd.flac
Files may have changed, which prevents torrents from downloading correctly or completely; please check for an updated torrent at http://archive.org/download/dopapod2014-02-25.elam260.sbd.flac/dopapod2014-02-25.elam260.sbd.flac_archive.torrent
Note: retrieval usually requires a client that supports webseeding (GetRight style).
Note: many Internet Archive torrents contain a 'pad file' directory. This directory and the files within it may be erased once retrieval completes.
Note: the file dopapod2014-02-25.elam260.sbd.flac_meta.xml contains metadata about this torrent's conte
this is the length  10
buffered string is  created by
the  character read at switch decodeString is:  1
this is the length  15
buffered string is  ia_make_torrent
this is the length  13
buffered string is  creation date
the  character read at switch Number is:  i
this is the length  4
buffered string is  info
the  character read at switch Dictionary is:  d
this is the length  11
buffered string is  collections
the  character read at switch List is:  l
this is the length  6
buffered string is  org.ar

and the next run:

    the  character read at switch Dictionary is:  d
    this is the length  8
    buffered string is  announce
    the  character read at switch decodeString is:  3
    this is the length  36
    buffered string is  http://bt1.archive.org:6969/announce
    this is the length  13
    buffered string is  announce-list
    the  character read at switch List is:  l
    the  character read at switch decodeString is:  3
    this is the length  36
    buffered string is  http://bt1.archive.org:6969/announce
    the  character read at switch decodeString is:  3
    this is the length  36
    buffered string is  http://bt2.archive.org:6969/announce
    list object -------------->http://bt1.archive.org:6969/announce
    list object -------------->http://bt2.archive.org:6969/announce
    Exception in thread "main" java.io.IOException: Problem parsing bencoded file
        at javaapplication1.DotTorrentBencodeReader.Decode(DotTorrentBencodeReader.java:61)
    this is the length  7
    buffered string is  comment
    the  character read at switch decodeString is:  6
    this is the length  692
    buffered string is  This content hosted at the Internet Archive at http://archive.org/details/dopapod2014-02-25.elam260.sbd.flac
    Files may have changed, which prevents torrents from downloading correctly or completely; please check for an updated torrent at http://archive.org/download/dopapod2014-02-25.elam260.sbd.flac/dopapod2014-02-25.elam260.sbd.flac_archive.torrent
    Note: retrieval usually requires a client that supports webseeding (GetRight style).
    Note: many Internet Archive torrents contain a 'pad file' directory. This directory and the files within it may be erased once retrieval completes.
    Note: the file dopapod2014-02-25.elam260.sbd.flac_meta.xml contains metadata about this torrent's conte
    this is the length  10
    buffered string is  created by
    the  character read at switch decodeString is:  1
    this is the length  15
    buffered string is  ia_make_torrent
    this is the length  13
    buffered string is  creation date
    the  character read at switch Number is:  i
    this is the length  4
    buffered string is  info
    the  character read at switch Dictionary is:  d
    this is the length  11
    buffered string is  collections
        at javaapplication1.DotTorrentBencodeReader.decodeDictionary(DotTorrentBencodeReader.java:82)
    the  character read at switch List is:  l
    this is the length  6
    buffered string is  org.ar
        at javaapplication1.DotTorrentBencodeReader.Decode(DotTorrentBencodeReader.java:46)
        at javaapplication1.DotTorrentBencodeReader.decodeDictionary(DotTorrentBencodeReader.java:82)
        at javaapplication1.DotTorrentBencodeReader.Decode(DotTorrentBencodeReader.java:46)
        at javaapplication1.DotTorrentBencodeReader.main(DotTorrentBencodeReader.java:26)
    Java Result: 1

`

my code:

`

package javaapplication1;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class DotTorrentBencodeReader {

    public static BufferedInputStream bufferedDotTorrrentFile;
    public static Map<String, Object> dotTorrentDictionary;

    public static void main(String[] args) throws FileNotFoundException, IOException {

        bufferedDotTorrrentFile = new BufferedInputStream(new FileInputStream("/home/user/dopapod.torrent"));
        dotTorrentDictionary = new LinkedHashMap<String, Object>();
        Decode(bufferedDotTorrrentFile);
        //  printDictionary();
    }

    public static Object Decode(BufferedInputStream bufferedDotTorrrentFile) throws IOException {

        bufferedDotTorrrentFile.mark(0);

        char theCharacterRead = (char) bufferedDotTorrrentFile.read();

        switch (theCharacterRead) {
            case 'i':
                System.out.println("the  character read at switch Number is:  " + theCharacterRead);
                return decodeNumber(bufferedDotTorrrentFile);

            case 'l':
                System.out.println("the  character read at switch List is:  " + theCharacterRead);
                return decodeList(bufferedDotTorrrentFile);
            case 'd':
                System.out.println("the  character read at switch Dictionary is:  " + theCharacterRead);
                return decodeDictionary(bufferedDotTorrrentFile);
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                bufferedDotTorrrentFile.reset();
                System.out.println("the  character read at switch decodeString is:  " + theCharacterRead);
                return decodeString(bufferedDotTorrrentFile);
            default:
                throw new IOException("Problem parsing bencoded file");

        }

    }

    public static Map decodeDictionary(BufferedInputStream bufferedDotTorrrentFile) throws IOException {

        bufferedDotTorrrentFile.mark(0);

        char readChar = (char) bufferedDotTorrrentFile.read();

        while (readChar != 'e') {

            if (readChar < 0) {

                throw new IOException("Unexpected EOF found");
            }

            bufferedDotTorrrentFile.reset();

            dotTorrentDictionary.put(decodeString(bufferedDotTorrrentFile), Decode(bufferedDotTorrrentFile));

            bufferedDotTorrrentFile.mark(0);

            readChar = (char) bufferedDotTorrrentFile.read();

        }

        return dotTorrentDictionary;

    }

    public static String decodeString(BufferedInputStream bufferedDotTorrrentFile) throws IOException {
        char readChar = (char) bufferedDotTorrrentFile.read();

        StringBuilder bufferedStringLength = new StringBuilder();

        do {

            if (readChar < 0) {
                throw new IOException("Unexpected EOF found");
            }

            bufferedStringLength.append(readChar);
            readChar = (char) bufferedDotTorrrentFile.read();

        } while (readChar != ':');

        System.out.println("this is the length  " + bufferedStringLength.toString().replaceAll("\\D+", ""));

        int eye = Integer.parseInt(bufferedStringLength.toString().replaceAll("\\D+", ""));

        StringBuilder bufferedString = new StringBuilder();

        int i = 0;

        while (i < eye) {

            bufferedString.append(((char) bufferedDotTorrrentFile.read()));
            i++;
        }
        System.out.println("buffered string is  " + bufferedString);
        return bufferedString.toString();

    }

    public static Long decodeNumber(BufferedInputStream bufferedDotTorrrentFile) throws IOException {

        char readChar = (char) bufferedDotTorrrentFile.read();

        StringBuilder bufferedNumber = new StringBuilder();

        do {

            if (readChar < 0) {
                throw new IOException("Unexpected EOF found");
            }

            bufferedNumber.append(readChar);
            readChar = (char) bufferedDotTorrrentFile.read();

        } while (readChar != 'e');

        return Long.parseLong(bufferedNumber.toString());

    }

    public static List decodeList(BufferedInputStream bufferedDotTorrrentFile) throws IOException {

        List listObject = new LinkedList();

        bufferedDotTorrrentFile.mark(0);

        if (((char) bufferedDotTorrrentFile.read()) == 'l') {
            bufferedDotTorrrentFile.reset();
            char readChar;

            while ((readChar = (char) bufferedDotTorrrentFile.read()) == 'l') {


                listObject.add(Decode(bufferedDotTorrrentFile));
                bufferedDotTorrrentFile.read();


            }
            for (int i = 0; i < listObject.size(); i++) {
                System.out.println("list object -------------->" + listObject.get(i));
            }
        }
        return listObject;
    }

    public static void printDictionary() {

        for (Map.Entry<String, Object> entry : dotTorrentDictionary.entrySet()) {
            System.out.println(entry.getKey() + ": \n" + entry.getValue());

        }
        System.out.println(DotTorrentReaderThree.listObject.get(0));
    }

}
`
هل كانت مفيدة؟

المحلول

Because Netbeans (or the shell) will use two different threads for reading each stream: 1 for error and 1 for output. Thus there is no guarantee about how each thread will get scheduled (and the output printed)!

Both these threads are competing to append text to the console, and typically the output stream will flush() at the occurrence of a line-feed character. This is why you get lines of error and output text interleaved.

I should also probably say that the reason that two threads are used is because reading from a stream typically blocks the thread if no data is available. So if only one thread was being used to read from both output and error, it may block trying to read from output (while no output data is available) even though data is available on error.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top