• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kermitt2 / grobid / 390

pending completion
390

push

circleci

more log debug; model update

2 of 2 new or added lines in 2 files covered. (100.0%)

14847 of 37498 relevant lines covered (39.59%)

0.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.13
/grobid-core/src/main/java/org/grobid/core/process/StreamGobbler.java
1
package org.grobid.core.process;
2

3
import java.io.IOException;
4
import java.io.InputStream;
5

6
/**
7
 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
8
 */
9
public class StreamGobbler extends InputStream {
10
    class GobblerThread extends Thread {
1✔
11
        @Override
12
        public void run() {
13
            byte[] buff = new byte[8192];
1✔
14

15
            while (true) {
16
                try {
17
                    int avail = is.read(buff);
1✔
18

19
                    synchronized (synchronizer) {
1✔
20
                        if (avail <= 0) {
1✔
21
                            isEOF = true;
1✔
22
                            synchronizer.notifyAll();
1✔
23
                            break;
1✔
24
                        }
25

26
                        int space_available = buffer.length - write_pos;
×
27

28
                        if (space_available < avail) {
×
29
                            /* compact/resize buffer */
30

31
                            int unread_size = write_pos - read_pos;
×
32
                            int need_space = unread_size + avail;
×
33

34
                            byte[] new_buffer = buffer;
×
35

36
                            if (need_space > buffer.length) {
×
37
                                int inc = need_space / 3;
×
38
                                inc = (inc < 256) ? 256 : inc;
×
39
                                inc = (inc > 8192) ? 8192 : inc;
×
40
                                new_buffer = new byte[need_space + inc];
×
41
                            }
42

43
                            if (unread_size > 0)
×
44
                                System.arraycopy(buffer, read_pos, new_buffer, 0, unread_size);
×
45

46
                            buffer = new_buffer;
×
47

48
                            read_pos = 0;
×
49
                            write_pos = unread_size;
×
50
                        }
51

52
                        System.arraycopy(buff, 0, buffer, write_pos, avail);
×
53
                        write_pos += avail;
×
54

55
                        synchronizer.notifyAll();
×
56
                    }
×
57
                } catch (IOException e) {
×
58
                    synchronized (synchronizer) {
×
59
                        exception = e;
×
60
                        synchronizer.notifyAll();
×
61
                        break;
×
62
                    }
63
                }
×
64
            }
65
        }
1✔
66
    }
67

68
    private InputStream is;
69

70
    private final Object synchronizer = new Object();
1✔
71

72
    private boolean isEOF = false;
1✔
73
    private boolean isClosed = false;
1✔
74
    private IOException exception = null;
1✔
75

76
    private byte[] buffer = new byte[2048];
1✔
77
    private int read_pos = 0;
1✔
78
    private int write_pos = 0;
1✔
79

80
    public StreamGobbler(InputStream is) {
1✔
81
        this.is = is;
1✔
82
        GobblerThread t = new GobblerThread();
1✔
83
        t.setDaemon(true);
1✔
84
        t.start();
1✔
85
    }
1✔
86

87
    @Override
88
    public int read() throws IOException {
89
        boolean wasInterrupted = false;
×
90

91
        try {
92
            synchronized (synchronizer) {
×
93
                if (isClosed)
×
94
                    throw new IOException("This StreamGobbler is closed.");
×
95

96
                while (read_pos == write_pos) {
×
97
                    if (exception != null)
×
98
                        throw exception;
×
99

100
                    if (isEOF)
×
101
                        return -1;
×
102

103
                    try {
104
                        synchronizer.wait();
×
105
                    } catch (InterruptedException e) {
×
106
                        wasInterrupted = true;
×
107
                    }
×
108
                }
109
                return buffer[read_pos++] & 0xff;
×
110
            }
111
        } finally {
112
            if (wasInterrupted)
×
113
                Thread.currentThread().interrupt();
×
114
        }
115
    }
116

117
    @Override
118
    public int available() throws IOException {
119
        synchronized (synchronizer) {
×
120
            if (isClosed)
×
121
                throw new IOException("This StreamGobbler is closed.");
×
122

123
            return write_pos - read_pos;
×
124
        }
125
    }
126

127
    @Override
128
    public int read(byte[] b) throws IOException {
129
        return read(b, 0, b.length);
×
130
    }
131

132
    @Override
133
    public void close() throws IOException {
134
        synchronized (synchronizer) {
1✔
135
            if (isClosed)
1✔
136
                return;
×
137
            isClosed = true;
1✔
138
            isEOF = true;
1✔
139
            synchronizer.notifyAll();
1✔
140
            is.close();
1✔
141
        }
1✔
142
    }
1✔
143

144
    @Override
145
    public int read(byte[] b, int off, int len) throws IOException {
146
        if (b == null)
×
147
            throw new NullPointerException();
×
148

149
        if ((off < 0) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0) || (off > b.length))
×
150
            throw new IndexOutOfBoundsException();
×
151

152
        if (len == 0)
×
153
            return 0;
×
154

155
        boolean wasInterrupted = false;
×
156

157
        try {
158
            synchronized (synchronizer) {
×
159
                if (isClosed)
×
160
                    throw new IOException("This StreamGobbler is closed.");
×
161

162
                while (read_pos == write_pos) {
×
163
                    if (exception != null)
×
164
                        throw exception;
×
165

166
                    if (isEOF)
×
167
                        return -1;
×
168

169
                    try {
170
                        synchronizer.wait();
×
171
                    } catch (InterruptedException e) {
×
172
                        wasInterrupted = true;
×
173
                    }
×
174
                }
175

176
                int avail = write_pos - read_pos;
×
177

178
                avail = (avail > len) ? len : avail;
×
179

180
                System.arraycopy(buffer, read_pos, b, off, avail);
×
181

182
                read_pos += avail;
×
183

184
                return avail;
×
185
            }
186
        } finally {
187
            if (wasInterrupted)
×
188
                Thread.currentThread().interrupt();
×
189
        }
190
    }
191
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc