1 18 package org.apache.tools.ant.util; 19 20 import java.io.InputStream ; 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 import java.io.PrintStream ; 24 25 32 33 public class UUEncoder { 34 protected static final int DEFAULT_MODE = 644; 35 private static final int MAX_CHARS_PER_LINE = 45; 36 private OutputStream out; 37 private String name; 38 39 47 public UUEncoder(String name) { 48 this.name = name; 49 } 50 51 59 public void encode(InputStream is, OutputStream out) 60 throws IOException { 61 this.out = out; 62 encodeBegin(); 63 byte[] buffer = new byte[MAX_CHARS_PER_LINE * 100]; 64 int count; 65 while ((count = is.read(buffer, 0, buffer.length)) != -1) { 66 int pos = 0; 67 while (count > 0) { 68 int num = count > MAX_CHARS_PER_LINE 69 ? MAX_CHARS_PER_LINE 70 : count; 71 encodeLine(buffer, pos, num, out); 72 pos += num; 73 count -= num; 74 } 75 } 76 out.flush(); 77 encodeEnd(); 78 } 79 80 83 private void encodeString(String n) throws IOException { 84 PrintStream writer = new PrintStream (out); 85 writer.print(n); 86 writer.flush(); 87 } 88 89 private void encodeBegin() throws IOException { 90 encodeString("begin " + DEFAULT_MODE + " " + name + "\n"); 91 } 92 93 private void encodeEnd() throws IOException { 94 encodeString(" \nend\n"); 95 } 96 97 107 private void encodeLine( 108 byte[] data, int offset, int length, OutputStream out) 109 throws IOException { 110 out.write((byte) ((length & 0x3F) + ' ')); 112 byte a; 113 byte b; 114 byte c; 115 116 for (int i = 0; i < length;) { 117 b = 1; 119 c = 1; 120 a = data[offset + i++]; 122 if (i < length) { 123 b = data[offset + i++]; 124 if (i < length) { 125 c = data[offset + i++]; 126 } 127 } 128 129 byte d1 = (byte) (((a >>> 2) & 0x3F) + ' '); 130 byte d2 = (byte) ((((a << 4) & 0x30) | ((b >>> 4) & 0x0F)) + ' '); 131 byte d3 = (byte) ((((b << 2) & 0x3C) | ((c >>> 6) & 0x3)) + ' '); 132 byte d4 = (byte) ((c & 0x3F) + ' '); 133 134 out.write(d1); 135 out.write(d2); 136 out.write(d3); 137 out.write(d4); 138 } 139 140 out.write('\n'); 142 } 143 } 144 | Popular Tags |