< prev index next >

jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/messaging/saaj/packaging/mime/util/UUEncoderStream.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2013, 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


  66     public UUEncoderStream(OutputStream out, String name) {
  67         this(out, name, 644);
  68     }
  69 
  70     /**
  71      * Create a UUencoder that encodes the specified input stream
  72      * @param out        the output stream
  73      * @param name       Specifies a name for the encoded buffer
  74      * @param mode       Specifies permission mode for the encoded buffer
  75      */
  76     public UUEncoderStream(OutputStream out, String name, int mode) {
  77         super(out);
  78         this.name = name;
  79         this.mode = mode;
  80         buffer = new byte[45];
  81     }
  82 
  83     /**
  84      * Set up the buffer name and permission mode.
  85      * This method has any effect only if it is invoked before
  86      * you start writing into the output stream



  87      */
  88     public void setNameMode(String name, int mode) {
  89         this.name = name;
  90         this.mode = mode;
  91     }
  92 

  93     public void write(byte[] b, int off, int len) throws IOException {
  94         for (int i = 0; i < len; i++)
  95             write(b[off + i]);
  96     }
  97 

  98     public void write(byte[] data) throws IOException {
  99         write(data, 0, data.length);
 100     }
 101 

 102     public void write(int c) throws IOException {
 103         /* buffer up characters till we get a line's worth, then encode
 104          * and write them out. Max number of characters allowed per
 105          * line is 45.
 106          */
 107         buffer[bufsize++] = (byte)c;
 108         if (bufsize == 45) {
 109             writePrefix();
 110             encode();
 111             bufsize = 0;
 112         }
 113     }
 114 

 115     public void flush() throws IOException {
 116         if (bufsize > 0) { // If there's unencoded characters in the buffer
 117             writePrefix();
 118             encode();      // .. encode them
 119         }
 120         writeSuffix();
 121         out.flush();
 122     }
 123 

 124     public void close() throws IOException {
 125         flush();
 126         out.close();
 127     }
 128 
 129     /**
 130      * Write out the prefix: "begin <mode> <name>"
 131      */
 132     private void writePrefix() throws IOException {
 133         if (!wrotePrefix) {
 134             PrintStream ps = new PrintStream(out);
 135             ps.println("begin " + mode + " " + name);
 136             ps.flush();
 137             wrotePrefix = true;
 138         }
 139     }
 140 
 141     /**
 142      * Write a single line containing space and the suffix line
 143      * containing the single word "end" (terminated by a newline)


   1 /*
   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.
   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


  66     public UUEncoderStream(OutputStream out, String name) {
  67         this(out, name, 644);
  68     }
  69 
  70     /**
  71      * Create a UUencoder that encodes the specified input stream
  72      * @param out        the output stream
  73      * @param name       Specifies a name for the encoded buffer
  74      * @param mode       Specifies permission mode for the encoded buffer
  75      */
  76     public UUEncoderStream(OutputStream out, String name, int mode) {
  77         super(out);
  78         this.name = name;
  79         this.mode = mode;
  80         buffer = new byte[45];
  81     }
  82 
  83     /**
  84      * Set up the buffer name and permission mode.
  85      * This method has any effect only if it is invoked before
  86      * you start writing into the output stream.
  87      *
  88      * @param name name to set for the buffer.
  89      * @param mode permission mode.
  90      */
  91     public void setNameMode(String name, int mode) {
  92         this.name = name;
  93         this.mode = mode;
  94     }
  95 
  96     @Override
  97     public void write(byte[] b, int off, int len) throws IOException {
  98         for (int i = 0; i < len; i++)
  99             write(b[off + i]);
 100     }
 101 
 102     @Override
 103     public void write(byte[] data) throws IOException {
 104         write(data, 0, data.length);
 105     }
 106 
 107     @Override
 108         public void write(int c) throws IOException {
 109         /* buffer up characters till we get a line's worth, then encode
 110          * and write them out. Max number of characters allowed per
 111          * line is 45.
 112          */
 113         buffer[bufsize++] = (byte)c;
 114         if (bufsize == 45) {
 115             writePrefix();
 116             encode();
 117             bufsize = 0;
 118         }
 119     }
 120 
 121     @Override
 122     public void flush() throws IOException {
 123         if (bufsize > 0) { // If there's unencoded characters in the buffer
 124             writePrefix();
 125             encode();      // .. encode them
 126         }
 127         writeSuffix();
 128         out.flush();
 129     }
 130 
 131     @Override
 132     public void close() throws IOException {
 133         flush();
 134         out.close();
 135     }
 136 
 137     /**
 138      * Write out the prefix: "begin <mode> <name>"
 139      */
 140     private void writePrefix() throws IOException {
 141         if (!wrotePrefix) {
 142             PrintStream ps = new PrintStream(out);
 143             ps.println("begin " + mode + " " + name);
 144             ps.flush();
 145             wrotePrefix = true;
 146         }
 147     }
 148 
 149     /**
 150      * Write a single line containing space and the suffix line
 151      * containing the single word "end" (terminated by a newline)


< prev index next >