Skip to content

Package: UniqueValue

UniqueValue

nameinstructionbranchcomplexitylinemethod
UniqueValue()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getUniqueBoundaryValue()
M: 26 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
getUniqueMessageIDValue(Session)
M: 46 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package jakarta.mail.internet;
18:
19: import jakarta.mail.Session;
20:
21: import java.util.concurrent.atomic.AtomicInteger;
22:
23: /**
24: * This is a utility class that generates unique values. The generated
25: * String contains only US-ASCII characters and hence is safe for use
26: * in RFC822 headers. <p>
27: *
28: * This is a package private class.
29: *
30: * @author John Mani
31: * @author Max Spivak
32: * @author Bill Shannon
33: */
34:
35: class UniqueValue {
36: /**
37: * A global unique number, to ensure uniqueness of generated strings.
38: */
39: private static AtomicInteger id = new AtomicInteger();
40:
41: /**
42: * Get a unique value for use in a multipart boundary string.
43: *
44: * This implementation generates it by concatenating a global
45: * part number, a newly created object's <code>hashCode()</code>,
46: * and the current time (in milliseconds).
47: */
48: public static String getUniqueBoundaryValue() {
49: StringBuilder s = new StringBuilder();
50: long hash = s.hashCode();
51:
52: // Unique string is ----=_Part_<part>_<hashcode>.<currentTime>
53: s.append("----=_Part_").append(id.getAndIncrement()).append("_").
54: append(hash).append('.').
55: append(System.currentTimeMillis());
56: return s.toString();
57: }
58:
59: /**
60: * Get a unique value for use in a Message-ID.
61: *
62: * This implementation generates it by concatenating a newly
63: * created object's <code>hashCode()</code>, a global ID
64: * (incremented on every use), the current time (in milliseconds),
65: * and the host name from this user's local address generated by
66: * <code>InternetAddress.getLocalAddress()</code>.
67: * (The host name defaults to "localhost" if
68: * <code>getLocalAddress()</code> returns null.)
69: *
70: * @param ssn Session object used to get the local address
71: * @see jakarta.mail.internet.InternetAddress
72: */
73: public static String getUniqueMessageIDValue(Session ssn) {
74: String suffix = null;
75:
76: InternetAddress addr = InternetAddress.getLocalAddress(ssn);
77:• if (addr != null)
78: suffix = addr.getAddress();
79: else {
80: suffix = "jakartamailuser@localhost"; // worst-case default
81: }
82: int at = suffix.lastIndexOf('@');
83:• if (at >= 0)
84: suffix = suffix.substring(at);
85:
86: StringBuilder s = new StringBuilder();
87:
88: // Unique string is <hashcode>.<id>.<currentTime><suffix>
89: s.append(s.hashCode()).append('.').
90: append(id.getAndIncrement()).append('.').
91: append(System.currentTimeMillis()).
92: append(suffix);
93: return s.toString();
94: }
95: }