jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/messaging/saaj/util/ByteOutputStream.java
changeset 43852 93a527059d8a
parent 28326 2b9860c0d68a
equal deleted inserted replaced
43752:3c68ef249093 43852:93a527059d8a
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    40  *
    40  *
    41  * <ol>
    41  * <ol>
    42  * <li>doesn't do synchronization
    42  * <li>doesn't do synchronization
    43  * <li>allows access to the raw buffer
    43  * <li>allows access to the raw buffer
    44  * <li>almost no parameter check
    44  * <li>almost no parameter check
       
    45  * </ol>
    45  */
    46  */
    46 public final class ByteOutputStream extends OutputStream {
    47 public final class ByteOutputStream extends OutputStream {
    47     /**
    48     /**
    48      * The buffer where data is stored.
    49      * The buffer where data is stored.
    49      */
    50      */
    62         buf = new byte[size];
    63         buf = new byte[size];
    63     }
    64     }
    64 
    65 
    65     /**
    66     /**
    66      * Copies all the bytes from this input into this buffer.
    67      * Copies all the bytes from this input into this buffer.
       
    68      *
       
    69      * @param in input stream.
       
    70      * @exception IOException in case of an I/O error.
    67      */
    71      */
    68     public void write(InputStream in) throws IOException {
    72     public void write(InputStream in) throws IOException {
    69         if (in instanceof ByteArrayInputStream) {
    73         if (in instanceof ByteArrayInputStream) {
    70             int size = in.available();
    74             int size = in.available();
    71             ensureCapacity(size);
    75             ensureCapacity(size);
    82                 // the buffer filled up. double the buffer
    86                 // the buffer filled up. double the buffer
    83                 ensureCapacity(count);
    87                 ensureCapacity(count);
    84         }
    88         }
    85     }
    89     }
    86 
    90 
       
    91     @Override
    87     public void write(int b) {
    92     public void write(int b) {
    88         ensureCapacity(1);
    93         ensureCapacity(1);
    89         buf[count] = (byte) b;
    94         buf[count] = (byte) b;
    90         count++;
    95         count++;
    91     }
    96     }
   100             System.arraycopy(buf, 0, newbuf, 0, count);
   105             System.arraycopy(buf, 0, newbuf, 0, count);
   101             buf = newbuf;
   106             buf = newbuf;
   102         }
   107         }
   103     }
   108     }
   104 
   109 
       
   110     @Override
   105     public void write(byte[] b, int off, int len) {
   111     public void write(byte[] b, int off, int len) {
   106         ensureCapacity(len);
   112         ensureCapacity(len);
   107         System.arraycopy(b, off, buf, count, len);
   113         System.arraycopy(b, off, buf, count, len);
   108         count += len;
   114         count += len;
   109     }
   115     }
   110 
   116 
       
   117     @Override
   111     public void write(byte[] b) {
   118     public void write(byte[] b) {
   112         write(b, 0, b.length);
   119         write(b, 0, b.length);
   113     }
   120     }
   114 
   121 
   115     /**
   122     /**
   116      * Writes a string as ASCII string.
   123      * Writes a string as ASCII string.
       
   124      *
       
   125      * @param s string to write.
   117      */
   126      */
   118     public void writeAsAscii(String s) {
   127     public void writeAsAscii(String s) {
   119         int len = s.length();
   128         int len = s.length();
   120 
   129 
   121         ensureCapacity(len);
   130         ensureCapacity(len);
   136 
   145 
   137     /**
   146     /**
   138      * Evil buffer reallocation method.
   147      * Evil buffer reallocation method.
   139      * Don't use it unless you absolutely have to.
   148      * Don't use it unless you absolutely have to.
   140      *
   149      *
       
   150      * @return byte array
       
   151      *
   141      * @deprecated
   152      * @deprecated
   142      *      because this is evil!
   153      *      because this is evil!
   143      */
   154      */
       
   155     @Deprecated
   144     public byte toByteArray()[] {
   156     public byte toByteArray()[] {
   145         byte[] newbuf = new byte[count];
   157         byte[] newbuf = new byte[count];
   146         System.arraycopy(buf, 0, newbuf, 0, count);
   158         System.arraycopy(buf, 0, newbuf, 0, count);
   147         return newbuf;
   159         return newbuf;
   148     }
   160     }
   160      * characters according to the platform's default character encoding.
   172      * characters according to the platform's default character encoding.
   161      *
   173      *
   162      * @return String translated from the buffer's contents.
   174      * @return String translated from the buffer's contents.
   163      * @since JDK1.1
   175      * @since JDK1.1
   164      */
   176      */
       
   177     @Override
   165     public String toString() {
   178     public String toString() {
   166         return new String(buf, 0, count);
   179         return new String(buf, 0, count);
   167     }
   180     }
   168 
   181 
       
   182     @Override
   169     public void close() {
   183     public void close() {
   170     }
   184     }
   171 
   185 
   172     public byte[] getBytes() {
   186     public byte[] getBytes() {
   173         return buf;
   187         return buf;