Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CountSplitRule |
|
| 4.2;4,2 |
1 | /* | |
2 | * Copyright (c) 2006 - 2011 Michael Schierl | |
3 | * | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * | |
10 | * - Redistributions of source code must retain the above copyright notice, | |
11 | * this list of conditions and the following disclaimer. | |
12 | * | |
13 | * - Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * | |
17 | * - Neither name of the copyright holders nor the names of its | |
18 | * contributors may be used to endorse or promote products derived from | |
19 | * this software without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND THE CONTRIBUTORS | |
22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
25 | * HOLDERS OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
26 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
27 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | |
28 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
29 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
30 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | |
31 | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | package net.sf.jmatchparser.util.split; | |
35 | ||
36 | /** | |
37 | * A split rule that creates a new output file every given number of bytes or | |
38 | * lines. | |
39 | */ | |
40 | public class CountSplitRule extends SplitRule { | |
41 | ||
42 | private final int desiredSize; | |
43 | private int current; | |
44 | private final char charToCount; | |
45 | ||
46 | /** | |
47 | * Create a new rule that splits after a given number of bytes or lines. | |
48 | * | |
49 | * @param desiredSize | |
50 | * Number of bytes/lines | |
51 | * @param lines | |
52 | * <code>true</code> for lines, <code>false</code> for bytes | |
53 | */ | |
54 | public CountSplitRule(int desiredSize, boolean lines) { | |
55 | 11 | this(desiredSize, lines ? '\n' : 0); |
56 | 11 | } |
57 | ||
58 | /** | |
59 | * Create a new rule that splits after a given number a give character. | |
60 | * | |
61 | * @param desiredSize | |
62 | * Number of bytes/lines | |
63 | * @param charToCount | |
64 | * character that should be counted, or 0 to count all characters | |
65 | */ | |
66 | 171 | public CountSplitRule(int desiredSize, char charToCount) { |
67 | 171 | this.desiredSize = desiredSize; |
68 | 171 | this.charToCount = charToCount; |
69 | 171 | current = 0; |
70 | 171 | } |
71 | ||
72 | private int count(byte[] data, int off, int len) { | |
73 | 1384 | if (charToCount != 0) { |
74 | 606 | int cnt = 0; |
75 | 3646 | for (int i = off; i < off + len; i++) { |
76 | 3040 | if (data[i] == charToCount) |
77 | 580 | cnt++; |
78 | } | |
79 | 606 | return cnt; |
80 | } else { | |
81 | 778 | return len; |
82 | } | |
83 | } | |
84 | ||
85 | @Override | |
86 | public int findSplitPos(byte[] data, int off, int len) { | |
87 | 1944 | int remaining = desiredSize - current; |
88 | 1944 | if (len < remaining) |
89 | 458 | return -1; |
90 | 1486 | if (charToCount == 0) |
91 | 740 | return remaining; |
92 | 4963 | for (int i = off; i < off + len; i++) { |
93 | 4722 | if (data[i] == charToCount) |
94 | 983 | remaining--; |
95 | 4722 | if (remaining == 0) |
96 | 505 | return i - off + 1; |
97 | } | |
98 | 241 | return -1; |
99 | } | |
100 | ||
101 | @Override | |
102 | public void update(byte[] data, int off, int len) { | |
103 | 1384 | current += count(data, off, len); |
104 | 1384 | if (current > desiredSize) |
105 | 0 | throw new IllegalStateException("SplitOutputStream has to split this!"); |
106 | 1384 | if (current == desiredSize) |
107 | 685 | current = 0; |
108 | 1384 | } |
109 | } |