src/java.base/share/classes/java/util/jar/Attributes.java
changeset 53095 33a51275fee0
parent 52061 070186461dbb
child 53630 53c72d9d962b
equal deleted inserted replaced
53094:9e590041fcd4 53095:33a51275fee0
    34 import java.util.Objects;
    34 import java.util.Objects;
    35 import java.util.Set;
    35 import java.util.Set;
    36 
    36 
    37 import sun.util.logging.PlatformLogger;
    37 import sun.util.logging.PlatformLogger;
    38 
    38 
       
    39 import static java.nio.charset.StandardCharsets.UTF_8;
       
    40 
    39 /**
    41 /**
    40  * The Attributes class maps Manifest attribute names to associated string
    42  * The Attributes class maps Manifest attribute names to associated string
    41  * values. Valid attribute names are case-insensitive, are restricted to
    43  * values. Valid attribute names are case-insensitive, are restricted to
    42  * the ASCII characters in the set [0-9a-zA-Z_-], and cannot exceed 70
    44  * the ASCII characters in the set [0-9a-zA-Z_-], and cannot exceed 70
    43  * characters in length. There must be a colon and a SPACE after the name;
    45  * characters in length. There must be a colon and a SPACE after the name;
   296 
   298 
   297     /*
   299     /*
   298      * Writes the current attributes to the specified data output stream.
   300      * Writes the current attributes to the specified data output stream.
   299      * XXX Need to handle UTF8 values and break up lines longer than 72 bytes
   301      * XXX Need to handle UTF8 values and break up lines longer than 72 bytes
   300      */
   302      */
   301      @SuppressWarnings("deprecation")
   303     void write(DataOutputStream out) throws IOException {
   302      void write(DataOutputStream os) throws IOException {
   304         StringBuilder buffer = new StringBuilder(72);
   303          for (Entry<Object, Object> e : entrySet()) {
   305         for (Entry<Object, Object> e : entrySet()) {
   304              StringBuffer buffer = new StringBuffer(
   306             buffer.setLength(0);
   305                                          ((Name) e.getKey()).toString());
   307             buffer.append(e.getKey().toString());
   306              buffer.append(": ");
   308             buffer.append(": ");
   307 
   309             buffer.append(e.getValue());
   308              String value = (String) e.getValue();
   310             Manifest.println72(out, buffer.toString());
   309              if (value != null) {
   311         }
   310                  byte[] vb = value.getBytes("UTF8");
   312         Manifest.println(out); // empty line after individual section
   311                  value = new String(vb, 0, 0, vb.length);
       
   312              }
       
   313              buffer.append(value);
       
   314 
       
   315              Manifest.make72Safe(buffer);
       
   316              buffer.append("\r\n");
       
   317              os.writeBytes(buffer.toString());
       
   318          }
       
   319         os.writeBytes("\r\n");
       
   320     }
   313     }
   321 
   314 
   322     /*
   315     /*
   323      * Writes the current attributes to the specified data output stream,
   316      * Writes the current attributes to the specified data output stream,
   324      * make sure to write out the MANIFEST_VERSION or SIGNATURE_VERSION
   317      * make sure to write out the MANIFEST_VERSION or SIGNATURE_VERSION
   325      * attributes first.
   318      * attributes first.
   326      *
   319      *
   327      * XXX Need to handle UTF8 values and break up lines longer than 72 bytes
   320      * XXX Need to handle UTF8 values and break up lines longer than 72 bytes
   328      */
   321      */
   329     @SuppressWarnings("deprecation")
   322     void writeMain(DataOutputStream out) throws IOException {
   330     void writeMain(DataOutputStream out) throws IOException
   323         StringBuilder buffer = new StringBuilder(72);
   331     {
   324 
   332         // write out the *-Version header first, if it exists
   325         // write out the *-Version header first, if it exists
   333         String vername = Name.MANIFEST_VERSION.toString();
   326         String vername = Name.MANIFEST_VERSION.toString();
   334         String version = getValue(vername);
   327         String version = getValue(vername);
   335         if (version == null) {
   328         if (version == null) {
   336             vername = Name.SIGNATURE_VERSION.toString();
   329             vername = Name.SIGNATURE_VERSION.toString();
   337             version = getValue(vername);
   330             version = getValue(vername);
   338         }
   331         }
   339 
   332 
   340         if (version != null) {
   333         if (version != null) {
   341             out.writeBytes(vername+": "+version+"\r\n");
   334             buffer.append(vername);
       
   335             buffer.append(": ");
       
   336             buffer.append(version);
       
   337             out.write(buffer.toString().getBytes(UTF_8));
       
   338             Manifest.println(out);
   342         }
   339         }
   343 
   340 
   344         // write out all attributes except for the version
   341         // write out all attributes except for the version
   345         // we wrote out earlier
   342         // we wrote out earlier
   346         for (Entry<Object, Object> e : entrySet()) {
   343         for (Entry<Object, Object> e : entrySet()) {
   347             String name = ((Name) e.getKey()).toString();
   344             String name = ((Name) e.getKey()).toString();
   348             if ((version != null) && !(name.equalsIgnoreCase(vername))) {
   345             if ((version != null) && !(name.equalsIgnoreCase(vername))) {
   349 
   346                 buffer.setLength(0);
   350                 StringBuffer buffer = new StringBuffer(name);
   347                 buffer.append(name);
   351                 buffer.append(": ");
   348                 buffer.append(": ");
   352 
   349                 buffer.append(e.getValue());
   353                 String value = (String) e.getValue();
   350                 Manifest.println72(out, buffer.toString());
   354                 if (value != null) {
   351             }
   355                     byte[] vb = value.getBytes("UTF8");
   352         }
   356                     value = new String(vb, 0, 0, vb.length);
   353 
   357                 }
   354         Manifest.println(out); // empty line after main attributes section
   358                 buffer.append(value);
       
   359 
       
   360                 Manifest.make72Safe(buffer);
       
   361                 buffer.append("\r\n");
       
   362                 out.writeBytes(buffer.toString());
       
   363             }
       
   364         }
       
   365         out.writeBytes("\r\n");
       
   366     }
   355     }
   367 
   356 
   368     /*
   357     /*
   369      * Reads attributes from the specified input stream.
   358      * Reads attributes from the specified input stream.
   370      * XXX Need to handle UTF8 values.
       
   371      */
   359      */
   372     void read(Manifest.FastInputStream is, byte[] lbuf) throws IOException {
   360     void read(Manifest.FastInputStream is, byte[] lbuf) throws IOException {
   373         read(is, lbuf, null, 0);
   361         read(is, lbuf, null, 0);
   374     }
   362     }
   375 
   363 
   376     @SuppressWarnings("deprecation")
       
   377     int read(Manifest.FastInputStream is, byte[] lbuf, String filename, int lineNumber) throws IOException {
   364     int read(Manifest.FastInputStream is, byte[] lbuf, String filename, int lineNumber) throws IOException {
   378         String name = null, value;
   365         String name = null, value;
   379         byte[] lastline = null;
   366         byte[] lastline = null;
   380 
   367 
   381         int len;
   368         int len;
   407                 System.arraycopy(lbuf, 1, buf, lastline.length, len - 1);
   394                 System.arraycopy(lbuf, 1, buf, lastline.length, len - 1);
   408                 if (is.peek() == ' ') {
   395                 if (is.peek() == ' ') {
   409                     lastline = buf;
   396                     lastline = buf;
   410                     continue;
   397                     continue;
   411                 }
   398                 }
   412                 value = new String(buf, 0, buf.length, "UTF8");
   399                 value = new String(buf, 0, buf.length, UTF_8);
   413                 lastline = null;
   400                 lastline = null;
   414             } else {
   401             } else {
   415                 while (lbuf[i++] != ':') {
   402                 while (lbuf[i++] != ':') {
   416                     if (i >= len) {
   403                     if (i >= len) {
   417                         throw new IOException("invalid header field ("
   404                         throw new IOException("invalid header field ("
   420                 }
   407                 }
   421                 if (lbuf[i++] != ' ') {
   408                 if (lbuf[i++] != ' ') {
   422                     throw new IOException("invalid header field ("
   409                     throw new IOException("invalid header field ("
   423                                 + Manifest.getErrorPosition(filename, lineNumber) + ")");
   410                                 + Manifest.getErrorPosition(filename, lineNumber) + ")");
   424                 }
   411                 }
   425                 name = new String(lbuf, 0, 0, i - 2);
   412                 name = new String(lbuf, 0, i - 2, UTF_8);
   426                 if (is.peek() == ' ') {
   413                 if (is.peek() == ' ') {
   427                     lastline = new byte[len - i];
   414                     lastline = new byte[len - i];
   428                     System.arraycopy(lbuf, i, lastline, 0, len - i);
   415                     System.arraycopy(lbuf, i, lastline, 0, len - i);
   429                     continue;
   416                     continue;
   430                 }
   417                 }
   431                 value = new String(lbuf, i, len - i, "UTF8");
   418                 value = new String(lbuf, i, len - i, UTF_8);
   432             }
   419             }
   433             try {
   420             try {
   434                 if ((putValue(name, value) != null) && (!lineContinued)) {
   421                 if ((putValue(name, value) != null) && (!lineContinued)) {
   435                     PlatformLogger.getLogger("java.util.jar").warning(
   422                     PlatformLogger.getLogger("java.util.jar").warning(
   436                                      "Duplicate name in Manifest: " + name
   423                                      "Duplicate name in Manifest: " + name