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

hazendaz / sitemesh2 / 59

22 Mar 2026 02:30AM UTC coverage: 40.347%. Remained the same
59

push

github

hazendaz
[mvn] Update maven wrapper

698 of 1891 branches covered (36.91%)

Branch coverage included in aggregate %.

1555 of 3693 relevant lines covered (42.11%)

0.42 hits per line

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

0.0
/src/main/java/com/opensymphony/module/sitemesh/filter/TextEncoder.java
1
/*
2
 * SPDX-License-Identifier: Apache-2.0
3
 * Copyright 2011-2026 Hazendaz
4
 */
5
/* This software is published under the terms of the OpenSymphony Software
6
 * License version 1.1, of which a copy has been included with this
7
 * distribution in the LICENSE.txt file. */
8
package com.opensymphony.module.sitemesh.filter;
9

10
import java.io.IOException;
11
import java.nio.ByteBuffer;
12
import java.nio.CharBuffer;
13
import java.nio.charset.Charset;
14
import java.nio.charset.CharsetDecoder;
15
import java.nio.charset.CoderResult;
16
import java.nio.charset.CodingErrorAction;
17

18
/**
19
 * Converts text stored in byte[] to char[] using specified encoding.
20
 *
21
 * @author <a href="mailto:scott@atlassian.com">Scott Farquhar</a>
22
 * @author <a href="mailto:hani@formicary.net">Hani Suleiman</a>
23
 * @author Joe Walnes
24
 */
25
public class TextEncoder {
×
26

27
    /** The Constant DEFAULT_ENCODING. */
28
    private static final String DEFAULT_ENCODING = Charset.defaultCharset().displayName();
×
29

30
    /**
31
     * Encode.
32
     *
33
     * @param data
34
     *            the data
35
     * @param encoding
36
     *            the encoding
37
     *
38
     * @return the char[]
39
     *
40
     * @throws IOException
41
     *             Signals that an I/O exception has occurred.
42
     */
43
    public char[] encode(byte[] data, String encoding) throws IOException {
44
        if (encoding == null) {
×
45
            encoding = DEFAULT_ENCODING;
×
46
        }
47
        return getBuffer(data, encoding);
×
48
    }
49

50
    /**
51
     * Gets the buffer.
52
     *
53
     * @param data
54
     *            the data
55
     * @param encoding
56
     *            the encoding
57
     *
58
     * @return the buffer
59
     *
60
     * @throws IOException
61
     *             Signals that an I/O exception has occurred.
62
     */
63
    private char[] getBuffer(byte[] data, String encoding) throws IOException {
64
        if (!Charset.isSupported(encoding)) {
×
65
            throw new IOException("Unsupported encoding " + encoding);
×
66
        }
67
        Charset charset = Charset.forName(encoding);
×
68
        CharsetDecoder cd = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE)
×
69
                .onUnmappableCharacter(CodingErrorAction.REPLACE);
×
70
        int en = (int) (cd.maxCharsPerByte() * data.length);
×
71
        char[] ca = new char[en];
×
72
        ByteBuffer bb = ByteBuffer.wrap(data);
×
73
        CharBuffer cb = CharBuffer.wrap(ca);
×
74
        CoderResult cr = cd.decode(bb, cb, true);
×
75
        if (!cr.isUnderflow()) {
×
76
            cr.throwException();
×
77
        }
78
        cr = cd.flush(cb);
×
79
        if (!cr.isUnderflow()) {
×
80
            cr.throwException();
×
81
        }
82
        return trim(ca, cb.position());
×
83
    }
84

85
    /**
86
     * Trim.
87
     *
88
     * @param ca
89
     *            the ca
90
     * @param len
91
     *            the len
92
     *
93
     * @return the char[]
94
     */
95
    private char[] trim(char[] ca, int len) {
96
        if (len == ca.length) {
×
97
            return ca;
×
98
        }
99
        char[] tca = new char[len];
×
100
        System.arraycopy(ca, 0, tca, 0, len);
×
101
        return tca;
×
102
    }
103

104
}
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