1 2 12 package com.versant.core.jdbc.sql.conv; 13 14 import java.io.Reader ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 18 23 public class StreamUtils { 24 25 private static final int BUF_LEN = 4096; 26 27 30 public static class CharBuf { 31 public char[] cbuf = new char[BUF_LEN]; 32 public CharBuf next; 33 } 34 35 38 public static class ByteBuf { 39 public byte[] bbuf = new byte[BUF_LEN]; 40 public ByteBuf next; 41 } 42 43 46 public static String readAll(Reader in) throws IOException { 47 CharBuf root = new CharBuf(); 48 int sz = in.read(root.cbuf); 49 if (sz < 0) { 50 in.close(); 51 return ""; 52 } 53 if (sz < BUF_LEN) { 54 in.close(); 55 return new String (root.cbuf, 0, sz); 56 } 57 int bufCount = 0; 58 for (CharBuf buf = root;;) { 59 buf = buf.next = new CharBuf(); 60 ++bufCount; 61 sz = in.read(buf.cbuf); 62 if (sz < 0) { 63 sz = 0; 64 break; 65 } 66 if (sz < BUF_LEN) break; 67 } 68 in.close(); 69 int tot = bufCount * BUF_LEN + sz; 70 char[] a = new char[tot]; 71 int pos = 0; 72 for (CharBuf buf = root; ; buf = buf.next) { 73 if (buf.next == null) { 74 if (sz > 0) System.arraycopy(buf.cbuf, 0, a, pos, sz); 75 break; 76 } else { 77 System.arraycopy(buf.cbuf, 0, a, pos, BUF_LEN); 78 pos += BUF_LEN; 79 } 80 } 81 return new String (a); 82 } 83 84 87 public static String readAll(InputStream in, String encoding) throws IOException { 88 ByteBuf root = new ByteBuf(); 89 int sz = in.read(root.bbuf); 90 if (sz < 0) { 91 in.close(); 92 return ""; 93 } 94 if (sz < BUF_LEN) { 95 in.close(); 96 return new String (root.bbuf, 0, sz, encoding); 97 } 98 int bufCount = 0; 99 for (ByteBuf buf = root;;) { 100 buf = buf.next = new ByteBuf(); 101 ++bufCount; 102 sz = in.read(buf.bbuf); 103 if (sz < 0) { 104 sz = 0; 105 break; 106 } 107 if (sz < BUF_LEN) break; 108 } 109 in.close(); 110 int tot = bufCount * BUF_LEN + sz; 111 byte[] a = new byte[tot]; 112 int pos = 0; 113 for (ByteBuf buf = root; ; buf = buf.next) { 114 if (buf.next == null) { 115 if (sz > 0) System.arraycopy(buf.bbuf, 0, a, pos, sz); 116 break; 117 } else { 118 System.arraycopy(buf.bbuf, 0, a, pos, BUF_LEN); 119 pos += BUF_LEN; 120 } 121 } 122 return new String (a, encoding); 123 } 124 125 128 public static byte[] readAll(InputStream in) throws IOException { 129 ByteBuf root = new ByteBuf(); 130 int sz = in.read(root.bbuf); 131 if (sz < 0) { 132 in.close(); 133 return new byte[0]; 134 } 135 if (sz < BUF_LEN) { 136 in.close(); 137 byte[] a = new byte[sz]; 138 System.arraycopy(root.bbuf, 0, a, 0, sz); 139 return a; 140 } 141 int bufCount = 0; 142 for (ByteBuf buf = root;;) { 143 buf = buf.next = new ByteBuf(); 144 ++bufCount; 145 sz = in.read(buf.bbuf); 146 if (sz < 0) { 147 sz = 0; 148 break; 149 } 150 if (sz < BUF_LEN) break; 151 } 152 in.close(); 153 int tot = bufCount * BUF_LEN + sz; 154 byte[] a = new byte[tot]; 155 int pos = 0; 156 for (ByteBuf buf = root; ; buf = buf.next) { 157 if (buf.next == null) { 158 if (sz > 0) System.arraycopy(buf.bbuf, 0, a, pos, sz); 159 break; 160 } else { 161 System.arraycopy(buf.bbuf, 0, a, pos, BUF_LEN); 162 pos += BUF_LEN; 163 } 164 } 165 return a; 166 } 167 168 } 169 170 171 | Popular Tags |