8066619: Fix deprecation warnings in java.util.jar

Reviewed-by: rriggs, lancea
This commit is contained in:
Philipp Kunz 2018-12-21 09:54:32 -05:00 committed by Roger Riggs
parent 203f6ad99a
commit 1dae61a374
5 changed files with 560 additions and 57 deletions

View file

@ -36,6 +36,8 @@ import java.util.Set;
import sun.util.logging.PlatformLogger;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* The Attributes class maps Manifest attribute names to associated string
* values. Valid attribute names are case-insensitive, are restricted to
@ -298,25 +300,16 @@ public class Attributes implements Map<Object,Object>, Cloneable {
* Writes the current attributes to the specified data output stream.
* XXX Need to handle UTF8 values and break up lines longer than 72 bytes
*/
@SuppressWarnings("deprecation")
void write(DataOutputStream os) throws IOException {
for (Entry<Object, Object> e : entrySet()) {
StringBuffer buffer = new StringBuffer(
((Name) e.getKey()).toString());
buffer.append(": ");
String value = (String) e.getValue();
if (value != null) {
byte[] vb = value.getBytes("UTF8");
value = new String(vb, 0, 0, vb.length);
}
buffer.append(value);
Manifest.make72Safe(buffer);
buffer.append("\r\n");
os.writeBytes(buffer.toString());
}
os.writeBytes("\r\n");
void write(DataOutputStream out) throws IOException {
StringBuilder buffer = new StringBuilder(72);
for (Entry<Object, Object> e : entrySet()) {
buffer.setLength(0);
buffer.append(e.getKey().toString());
buffer.append(": ");
buffer.append(e.getValue());
Manifest.println72(out, buffer.toString());
}
Manifest.println(out); // empty line after individual section
}
/*
@ -326,9 +319,9 @@ public class Attributes implements Map<Object,Object>, Cloneable {
*
* XXX Need to handle UTF8 values and break up lines longer than 72 bytes
*/
@SuppressWarnings("deprecation")
void writeMain(DataOutputStream out) throws IOException
{
void writeMain(DataOutputStream out) throws IOException {
StringBuilder buffer = new StringBuilder(72);
// write out the *-Version header first, if it exists
String vername = Name.MANIFEST_VERSION.toString();
String version = getValue(vername);
@ -338,7 +331,11 @@ public class Attributes implements Map<Object,Object>, Cloneable {
}
if (version != null) {
out.writeBytes(vername+": "+version+"\r\n");
buffer.append(vername);
buffer.append(": ");
buffer.append(version);
out.write(buffer.toString().getBytes(UTF_8));
Manifest.println(out);
}
// write out all attributes except for the version
@ -346,34 +343,24 @@ public class Attributes implements Map<Object,Object>, Cloneable {
for (Entry<Object, Object> e : entrySet()) {
String name = ((Name) e.getKey()).toString();
if ((version != null) && !(name.equalsIgnoreCase(vername))) {
StringBuffer buffer = new StringBuffer(name);
buffer.setLength(0);
buffer.append(name);
buffer.append(": ");
String value = (String) e.getValue();
if (value != null) {
byte[] vb = value.getBytes("UTF8");
value = new String(vb, 0, 0, vb.length);
}
buffer.append(value);
Manifest.make72Safe(buffer);
buffer.append("\r\n");
out.writeBytes(buffer.toString());
buffer.append(e.getValue());
Manifest.println72(out, buffer.toString());
}
}
out.writeBytes("\r\n");
Manifest.println(out); // empty line after main attributes section
}
/*
* Reads attributes from the specified input stream.
* XXX Need to handle UTF8 values.
*/
void read(Manifest.FastInputStream is, byte[] lbuf) throws IOException {
read(is, lbuf, null, 0);
}
@SuppressWarnings("deprecation")
int read(Manifest.FastInputStream is, byte[] lbuf, String filename, int lineNumber) throws IOException {
String name = null, value;
byte[] lastline = null;
@ -409,7 +396,7 @@ public class Attributes implements Map<Object,Object>, Cloneable {
lastline = buf;
continue;
}
value = new String(buf, 0, buf.length, "UTF8");
value = new String(buf, 0, buf.length, UTF_8);
lastline = null;
} else {
while (lbuf[i++] != ':') {
@ -422,13 +409,13 @@ public class Attributes implements Map<Object,Object>, Cloneable {
throw new IOException("invalid header field ("
+ Manifest.getErrorPosition(filename, lineNumber) + ")");
}
name = new String(lbuf, 0, 0, i - 2);
name = new String(lbuf, 0, i - 2, UTF_8);
if (is.peek() == ' ') {
lastline = new byte[len - i];
System.arraycopy(lbuf, i, lastline, 0, len - i);
continue;
}
value = new String(lbuf, i, len - i, "UTF8");
value = new String(lbuf, i, len - i, UTF_8);
}
try {
if ((putValue(name, value) != null) && (!lineContinued)) {

View file

@ -35,6 +35,8 @@ import java.util.Map;
import sun.security.util.SecurityProperties;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* The Manifest class is used to maintain Manifest entry names and their
* associated Attributes. There are main Manifest Attributes as well as
@ -197,31 +199,28 @@ public class Manifest implements Cloneable {
* @exception IOException if an I/O error has occurred
* @see #getMainAttributes
*/
@SuppressWarnings("deprecation")
public void write(OutputStream out) throws IOException {
DataOutputStream dos = new DataOutputStream(out);
// Write out the main attributes for the manifest
attr.writeMain(dos);
// Now write out the per-entry attributes
StringBuilder buffer = entries.isEmpty() ? null : new StringBuilder(72);
for (Map.Entry<String, Attributes> e : entries.entrySet()) {
StringBuffer buffer = new StringBuffer("Name: ");
String value = e.getKey();
if (value != null) {
byte[] vb = value.getBytes("UTF8");
value = new String(vb, 0, 0, vb.length);
}
buffer.append(value);
make72Safe(buffer);
buffer.append("\r\n");
dos.writeBytes(buffer.toString());
buffer.setLength(0);
buffer.append("Name: ");
buffer.append(e.getKey());
println72(dos, buffer.toString());
e.getValue().write(dos);
}
dos.flush();
}
/**
* Adds line breaks to enforce a maximum 72 bytes per line.
* Adds line breaks to enforce a maximum of 72 bytes per line.
*
* @deprecation Replaced with {@link #println72}.
*/
@Deprecated(since = "13")
static void make72Safe(StringBuffer line) {
int length = line.length();
int index = 72;
@ -230,7 +229,38 @@ public class Manifest implements Cloneable {
index += 74; // + line width + line break ("\r\n")
length += 3; // + line break ("\r\n") and space
}
return;
}
/**
* Writes {@code line} to {@code out} with line breaks and continuation
* spaces within the limits of 72 bytes of contents per line followed
* by a line break.
*/
static void println72(OutputStream out, String line) throws IOException {
if (!line.isEmpty()) {
byte[] lineBytes = line.getBytes(UTF_8);
int length = lineBytes.length;
// first line can hold one byte more than subsequent lines which
// start with a continuation line break space
out.write(lineBytes[0]);
int pos = 1;
while (length - pos > 71) {
out.write(lineBytes, pos, 71);
pos += 71;
println(out);
out.write(' ');
}
out.write(lineBytes, pos, length - pos);
}
println(out);
}
/**
* Writes a line break to {@code out}.
*/
static void println(OutputStream out) throws IOException {
out.write('\r');
out.write('\n');
}
static String getErrorPosition(String filename, final int lineNumber) {
@ -304,7 +334,7 @@ public class Manifest implements Cloneable {
lastline = buf;
continue;
}
name = new String(buf, 0, buf.length, "UTF8");
name = new String(buf, 0, buf.length, UTF_8);
lastline = null;
}
Attributes attr = getAttributes(name);
@ -330,7 +360,7 @@ public class Manifest implements Cloneable {
toLower(lbuf[2]) == 'm' && toLower(lbuf[3]) == 'e' &&
lbuf[4] == ':' && lbuf[5] == ' ') {
try {
return new String(lbuf, 6, len - 6, "UTF8");
return new String(lbuf, 6, len - 6, UTF_8);
}
catch (Exception e) {
}