< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/util/ByteBuffer.java

Print this page




 130 
 131     /** Append a double as a eight byte number.
 132      */
 133     public void appendDouble(double x) {
 134         ByteArrayOutputStream buffer = new ByteArrayOutputStream(8);
 135         DataOutputStream bufout = new DataOutputStream(buffer);
 136         try {
 137             bufout.writeDouble(x);
 138             appendBytes(buffer.toByteArray(), 0, 8);
 139         } catch (IOException e) {
 140             throw new AssertionError("write");
 141         }
 142     }
 143 
 144     /** Append a name.
 145      */
 146     public void appendName(Name name) {
 147         appendBytes(name.getByteArray(), name.getByteOffset(), name.getByteLength());
 148     }
 149 
 150      /** Append the content of a given input stream.
 151      */
 152     public void appendStream(InputStream is) throws IOException {
 153         try {
 154             int start = length;
 155             int initialSize = is.available();
 156             elems = ArrayUtils.ensureCapacity(elems, length + initialSize);
 157             int r = is.read(elems, start, initialSize);
 158             int bp = start;
 159             while (r != -1) {
 160                 bp += r;
 161                 elems = ArrayUtils.ensureCapacity(elems, bp);
 162                 r = is.read(elems, bp, elems.length - bp);
 163             }
 164         } finally {
 165             try {
 166                 is.close();
 167             } catch (IOException e) {
 168                 /* Ignore any errors, as this stream may have already
 169                  * thrown a related exception which is the one that
 170                  * should be reported.
 171                  */
 172             }
 173         }
 174     }
 175 
 176     /** Extract an integer at position bp from elems.
 177      */
 178     public int getInt(int bp) {
 179         return
 180             ((elems[bp] & 0xFF) << 24) +
 181             ((elems[bp+1] & 0xFF) << 16) +
 182             ((elems[bp+2] & 0xFF) << 8) +
 183             (elems[bp+3] & 0xFF);
 184     }
 185 
 186 
 187     /** Extract a long integer at position bp from elems.
 188      */
 189     public long getLong(int bp) {
 190         DataInputStream elemsin =
 191             new DataInputStream(new ByteArrayInputStream(elems, bp, 8));
 192         try {
 193             return elemsin.readLong();
 194         } catch (IOException e) {
 195             throw new AssertionError(e);
 196         }
 197     }
 198 
 199     /** Extract a float at position bp from elems.
 200      */
 201     public float getFloat(int bp) {
 202         DataInputStream elemsin =
 203             new DataInputStream(new ByteArrayInputStream(elems, bp, 4));
 204         try {
 205             return elemsin.readFloat();
 206         } catch (IOException e) {
 207             throw new AssertionError(e);
 208         }
 209     }
 210 
 211     /** Extract a double at position bp from elems.
 212      */
 213     public double getDouble(int bp) {
 214         DataInputStream elemsin =
 215             new DataInputStream(new ByteArrayInputStream(elems, bp, 8));
 216         try {
 217             return elemsin.readDouble();
 218         } catch (IOException e) {
 219             throw new AssertionError(e);
 220         }
 221     }
 222 
 223     /** Extract a character at position bp from elems.
 224      */
 225     public char getChar(int bp) {
 226         return
 227             (char)(((elems[bp] & 0xFF) << 8) + (elems[bp+1] & 0xFF));
 228     }
 229 
 230     public byte getByte(int bp) {
 231         return elems[bp];
 232     }
 233 
 234     /** Reset to zero length.
 235      */
 236     public void reset() {
 237         length = 0;
 238     }
 239 
 240     /** Convert contents to name.
 241      */
 242     public Name toName(Names names) {
 243         return names.fromUtf(elems, 0, length);
 244     }
 245 }


 130 
 131     /** Append a double as a eight byte number.
 132      */
 133     public void appendDouble(double x) {
 134         ByteArrayOutputStream buffer = new ByteArrayOutputStream(8);
 135         DataOutputStream bufout = new DataOutputStream(buffer);
 136         try {
 137             bufout.writeDouble(x);
 138             appendBytes(buffer.toByteArray(), 0, 8);
 139         } catch (IOException e) {
 140             throw new AssertionError("write");
 141         }
 142     }
 143 
 144     /** Append a name.
 145      */
 146     public void appendName(Name name) {
 147         appendBytes(name.getByteArray(), name.getByteOffset(), name.getByteLength());
 148     }
 149 




















































































 150     /** Reset to zero length.
 151      */
 152     public void reset() {
 153         length = 0;
 154     }
 155 
 156     /** Convert contents to name.
 157      */
 158     public Name toName(Names names) {
 159         return names.fromUtf(elems, 0, length);
 160     }
 161 }
< prev index next >