1 /*
   2  * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.tools.java;
  27 
  28 import java.io.IOException;
  29 import java.io.DataInputStream;
  30 import java.io.DataOutputStream;
  31 
  32 /**
  33  * This class is used to represent an attribute from a binary class.
  34  * This class should go away once arrays are objects.
  35  *
  36  * WARNING: The contents of this source file are not part of any
  37  * supported API.  Code that depends on them does so at its own risk:
  38  * they are subject to change or removal without notice.
  39  */
  40 public final
  41 class BinaryAttribute implements Constants {
  42     Identifier name;
  43     byte data[];
  44     BinaryAttribute next;
  45 
  46     /**
  47      * Constructor
  48      */
  49     BinaryAttribute(Identifier name, byte data[], BinaryAttribute next) {
  50         this.name = name;
  51         this.data = data;
  52         this.next = next;
  53     }
  54 
  55     /**
  56      * Load a list of attributes
  57      */
  58     public static BinaryAttribute load(DataInputStream in, BinaryConstantPool cpool, int mask) throws IOException {
  59         BinaryAttribute atts = null;
  60         int natt = in.readUnsignedShort();  // JVM 4.6 method_info.attrutes_count
  61 
  62         for (int i = 0 ; i < natt ; i++) {
  63             // id from JVM 4.7 attribute_info.attribute_name_index
  64             Identifier id = cpool.getIdentifier(in.readUnsignedShort());
  65             // id from JVM 4.7 attribute_info.attribute_length
  66             int len = in.readInt();
  67 
  68             if (id.equals(idCode) && ((mask & ATT_CODE) == 0)) {
  69                 in.skipBytes(len);
  70             } else {
  71                 byte data[] = new byte[len];
  72                 in.readFully(data);
  73                 atts = new BinaryAttribute(id, data, atts);
  74             }
  75         }
  76         return atts;
  77     }
  78 
  79     // write out the Binary attributes to the given stream
  80     // (note that attributes may be null)
  81     static void write(BinaryAttribute attributes, DataOutputStream out,
  82                       BinaryConstantPool cpool, Environment env) throws IOException {
  83         // count the number of attributes
  84         int attributeCount = 0;
  85         for (BinaryAttribute att = attributes; att != null; att = att.next)
  86             attributeCount++;
  87         out.writeShort(attributeCount);
  88 
  89         // write out each attribute
  90         for (BinaryAttribute att = attributes; att != null; att = att.next) {
  91             Identifier name = att.name;
  92             byte data[] = att.data;
  93             // write the identifier
  94             out.writeShort(cpool.indexString(name.toString(), env));
  95             // write the length
  96             out.writeInt(data.length);
  97             // write the data
  98             out.write(data, 0, data.length);
  99         }
 100     }
 101 
 102     /**
 103      * Accessors
 104      */
 105 
 106     public Identifier getName() { return name; }
 107 
 108     public byte[] getData() { return data; }
 109 
 110     public BinaryAttribute getNextAttribute() { return next; }
 111 
 112 }