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

oracle / opengrok / #3691

09 Nov 2023 04:15PM UTC coverage: 74.721% (+8.6%) from 66.08%
#3691

push

web-flow
avoid annotations for binary files (#4476)

fixes #4473

6 of 13 new or added lines in 4 files covered. (46.15%)

258 existing lines in 28 files now uncovered.

43797 of 58614 relevant lines covered (74.72%)

0.75 hits per line

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

73.08
/opengrok-indexer/src/main/java/org/opengrok/indexer/util/HeadHandler.java
1
/*
2
 * CDDL HEADER START
3
 *
4
 * The contents of this file are subject to the terms of the
5
 * Common Development and Distribution License (the "License").
6
 * You may not use this file except in compliance with the License.
7
 *
8
 * See LICENSE.txt included in this distribution for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing Covered Code, include this CDDL HEADER in each
12
 * file and include the License file at LICENSE.txt.
13
 * If applicable, add the following below this CDDL HEADER, with the
14
 * fields enclosed by brackets "[]" replaced with your own identifying
15
 * information: Portions Copyright [yyyy] [name of copyright owner]
16
 *
17
 * CDDL HEADER END
18
 */
19

20
/*
21
 * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
22
 */
23
package org.opengrok.indexer.util;
24

25
import java.io.BufferedInputStream;
26
import java.io.BufferedReader;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.InputStreamReader;
30
import java.nio.charset.Charset;
31
import java.nio.charset.StandardCharsets;
32
import java.util.ArrayList;
33
import java.util.List;
34

35
/**
36
 * The purpose of this class is to provide {@code StreamHandler} that limits the output
37
 * to specified number of lines. Compared to {@code SpoolHandler} it consumes
38
 * limited amount of heap.
39
 */
40
public class HeadHandler implements Executor.StreamHandler {
41
    private final int maxLines;
42

43
    private final List<String> lines = new ArrayList<>();
1✔
44
    private final Charset charset;
45

46
    private static final int bufferedReaderSize = 200;
47

48
    /**
49
     * Charset of the underlying reader is set to UTF-8.
50
     * @param maxLines maximum number of lines to store
51
     */
52
    public HeadHandler(int maxLines) {
1✔
53
        this.maxLines = maxLines;
1✔
54
        this.charset = StandardCharsets.UTF_8;
1✔
55
    }
1✔
56

57
    /**
58
     * @param maxLines maximum number of lines to store
59
     * @param charset character set
60
     */
61
    public HeadHandler(int maxLines, Charset charset) {
×
62
        this.maxLines = maxLines;
×
63
        this.charset = charset;
×
64
    }
×
65

66
    /**
67
     * @return number of lines read
68
     */
69
    public int count() {
70
        return lines.size();
1✔
71
    }
72

73
    /**
74
     * @param index index
75
     * @return line at given index. Will be non {@code null} for valid index.
76
     */
77
    public String get(int index) {
78
        return lines.get(index);
1✔
79
    }
80

81
    // for testing
82
    static int getBufferedReaderSize() {
83
        return bufferedReaderSize;
1✔
84
    }
85

86
    @Override
87
    public void processStream(InputStream input) throws IOException {
88
        try (BufferedInputStream bufStream = new BufferedInputStream(input);
1✔
89
        BufferedReader reader = new BufferedReader(new InputStreamReader(bufStream, this.charset),
1✔
90
                bufferedReaderSize)) {
91
            int lineNum = 0;
1✔
92
            while (lineNum < maxLines) {
1✔
93
                String line = reader.readLine();
1✔
94
                if (line == null) { // EOF
1✔
95
                    return;
1✔
96
                }
97
                lines.add(line);
1✔
98
                lineNum++;
1✔
99
            }
1✔
100

101
            // Read and forget the rest.
UNCOV
102
            byte[] buf = new byte[1024];
×
UNCOV
103
            while ((bufStream.read(buf)) != -1) {
×
104
                ;
105
            }
106
        }
1✔
UNCOV
107
    }
×
108
}
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

© 2025 Coveralls, Inc