Skip to content

Commit 02b18b9

Browse files
committed
Add a utility class which knows how to write UTF
The whole thing exists only to work around the fact that DataOutputStream.writeUTF(String, DataOutput), which is the method we really need, has package-protected access. This utility class grabs that method via reflection, makes it accessible, and caches the reference.
1 parent 21881a9 commit 02b18b9

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.io.handle;
33+
34+
import java.io.DataOutput;
35+
import java.io.DataOutputStream;
36+
import java.io.IOException;
37+
import java.lang.reflect.InvocationTargetException;
38+
import java.lang.reflect.Method;
39+
40+
/**
41+
* Utility methods for working with {@link DataHandle}s.
42+
*
43+
* @author Curtis Rueden
44+
*/
45+
public final class DataHandles {
46+
47+
private static Method utfMethod;
48+
49+
private DataHandles() {
50+
// Prevent instantiation of utility class.
51+
}
52+
53+
/**
54+
* Writes a string to the specified DataOutput using modified UTF-8 encoding
55+
* in a machine-independent manner.
56+
* <p>
57+
* First, two bytes are written to out as if by the {@code writeShort} method
58+
* giving the number of bytes to follow. This value is the number of bytes
59+
* actually written out, not the length of the string. Following the length,
60+
* each character of the string is output, in sequence, using the modified
61+
* UTF-8 encoding for the character. If no exception is thrown, the counter
62+
* {@code written} is incremented by the total number of bytes written to the
63+
* output stream. This will be at least two plus the length of {@code str},
64+
* and at most two plus thrice the length of {@code str}.
65+
* </p>
66+
*
67+
* @param str a string to be written.
68+
* @param out destination to write to
69+
* @return The number of bytes written out.
70+
* @throws IOException if an I/O error occurs.
71+
*/
72+
public static int writeUTF(final String str, final DataOutput out)
73+
throws IOException
74+
{
75+
// HACK: Strangely, DataOutputStream.writeUTF(String, DataOutput)
76+
// has package-private access. We work around it via reflection.
77+
try {
78+
return (Integer) utfMethod().invoke(null, str, out);
79+
}
80+
catch (final IllegalAccessException | IllegalArgumentException
81+
| InvocationTargetException exc)
82+
{
83+
throw new IllegalStateException(
84+
"Cannot invoke DataOutputStream.writeUTF(String, DataOutput)", exc);
85+
}
86+
}
87+
88+
// -- Helper methods --
89+
90+
/** Gets the {@link #utfMethod} field, initializing if needed. */
91+
private static Method utfMethod() {
92+
if (utfMethod == null) initUTFMethod();
93+
return utfMethod;
94+
}
95+
96+
/** Initializes the {@link #utfMethod} field. */
97+
private static synchronized void initUTFMethod() {
98+
if (utfMethod != null) return;
99+
try {
100+
final Method m = DataOutputStream.class.getDeclaredMethod("writeUTF",
101+
String.class, DataOutput.class);
102+
m.setAccessible(true);
103+
utfMethod = m;
104+
}
105+
catch (final NoSuchMethodException | SecurityException exc) {
106+
throw new IllegalStateException(
107+
"No usable DataOutputStream.writeUTF(String, DataOutput)", exc);
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)