1 23 24 package org.apache.slide.content; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.CharArrayReader ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 import java.io.Reader ; 32 import java.io.Serializable ; 33 import java.util.ArrayList ; 34 import java.util.List ; 35 36 import org.apache.slide.common.ObjectValidationFailedException; 37 import org.apache.slide.util.Messages; 38 39 44 public final class NodeRevisionContent implements Serializable { 45 46 47 49 50 private static final int CHUNK = 1024*4; 51 52 53 55 56 59 private char[] content = null; 60 private byte[] contentBytes = null; 61 62 63 66 private transient Reader reader = null; 67 68 69 72 private transient InputStream inputStream = null; 73 74 75 77 78 83 public char[] getContent() { 84 char[] result = null; 85 if (content != null) { 86 result = content; 87 inputStream = null; 88 reader = null; 89 } 90 else if (reader != null) { 91 try { 92 content = read(reader); 93 } catch (IOException e) { 94 e.printStackTrace(); 95 } 96 result = content; 97 inputStream = null; 98 reader = null; 99 } 100 else if (contentBytes != null) { 101 content = new String (contentBytes).toCharArray(); 102 result = content; 103 inputStream = null; 104 reader = null; 105 } 106 else if (inputStream != null) { 107 try { 108 contentBytes = read(inputStream); 109 } catch (IOException e) { 110 e.printStackTrace(); 111 } finally { 112 try { 113 inputStream.close(); 114 } catch (IOException e) { 115 } 116 } 117 content = new String (contentBytes).toCharArray(); 118 result = content; 119 inputStream = null; 120 reader = null; 121 } 122 return result; 123 } 124 125 130 public byte[] getContentBytes() { 131 byte[] result = null; 132 if (contentBytes != null) { 133 result = contentBytes; 134 inputStream = null; 135 reader = null; 136 } 137 else if (inputStream != null) { 138 try { 139 contentBytes = read(inputStream); 140 } catch (IOException e) { 141 e.printStackTrace(); 142 } finally { 143 try { 144 inputStream.close(); 145 } catch (IOException e) { 146 } 147 } 148 result = contentBytes; 149 inputStream = null; 150 reader = null; 151 } 152 else if (content != null) { 153 contentBytes = new String (content).getBytes(); 154 result = contentBytes; 155 inputStream = null; 156 reader = null; 157 } 158 else if (reader != null) { 159 try { 160 content = read(reader); 161 } catch (IOException e) { 162 e.printStackTrace(); 163 } 164 contentBytes = new String (content).getBytes(); 165 result = contentBytes; 166 inputStream = null; 167 reader = null; 168 } 169 return result; 170 } 171 172 173 174 179 public Reader readContent() 180 throws IOException { 181 Reader result = null; 182 if (reader != null) { 183 result = reader; 184 inputStream = null; 185 } 186 else if (content != null) { 187 result = new CharArrayReader (content); 188 inputStream = null; 189 reader = null; 190 } 191 else if (inputStream != null) { 192 result = new InputStreamReader (inputStream); 193 reader = null; 194 } 195 else if (contentBytes != null) { 196 result = new CharArrayReader (new String (contentBytes).toCharArray()); 197 inputStream = null; 198 reader = null; 199 } 200 return result; 201 } 202 203 204 209 public InputStream streamContent() 210 throws IOException { 211 InputStream result = null; 212 if (inputStream != null) { 213 result = inputStream; 214 content = null; 215 reader = null; 216 } 217 else if (contentBytes != null) { 218 result = new ByteArrayInputStream ( contentBytes ); 219 reader = null; 220 inputStream = null; 221 } 222 else if (content != null) { 227 result = new ByteArrayInputStream ( new String (content).getBytes() ); 230 reader = null; 231 inputStream = null; 232 } 233 return result; 234 } 235 236 237 242 public void setContent(byte[] contentBytes) { 243 this.contentBytes = contentBytes; 244 this.reader = null; 245 this.inputStream = null; 246 this.content = null; 247 } 248 249 254 public void setContent(char[] content) { 255 this.content = content; 256 this.reader = null; 257 this.inputStream = null; 258 this.contentBytes = null; 259 } 260 261 262 267 public void setContent(Reader reader) { 268 this.reader = reader; 269 this.inputStream = null; 270 this.content = null; 271 this.contentBytes = null; 272 } 273 274 275 280 public void setContent(InputStream inputStream) { 281 this.inputStream = inputStream; 282 this.reader = null; 283 this.content = null; 284 this.contentBytes = null; 285 } 286 287 288 290 291 294 public void validate() { 295 296 if ((content == null) && (contentBytes == null) && (reader == null) && (inputStream == null)) 297 throw new ObjectValidationFailedException 298 (Messages.message 299 (NodeRevisionContent.class.getName() + ".noContent")); 300 301 } 302 303 304 306 307 311 public static byte[] read(InputStream inputStream) throws IOException { 312 byte[] chunk; 313 byte[] all; 314 int len; 315 List chunks; 316 int i; 317 int last; 318 319 chunks = new ArrayList (); 320 do { 321 chunk = new byte[CHUNK]; 322 chunks.add(chunk); 323 len = read(inputStream, chunk); 324 } while (len == CHUNK); 325 last = chunks.size() - 1; 326 all = new byte[last * CHUNK + len ]; 327 for (i = 0; i <= last; i++) { 328 chunk = (byte[]) chunks.get(i); 329 System.arraycopy(chunk, 0, all, CHUNK * i, (i == last)? len : CHUNK); 330 } 331 return all; 332 } 333 334 339 private static int read(InputStream stream, byte[] buffer) throws IOException { 340 int ofs; 341 int len; 342 343 ofs = 0; 344 while (true) { 345 len = stream.read(buffer, ofs, buffer.length - ofs); 346 if (len == -1) { 347 return ofs; 348 } 349 ofs += len; 350 if (ofs == buffer.length) { 351 return ofs; 352 } 353 } 354 } 355 356 360 public static char[] read(Reader reader) throws IOException { 361 char[] chunk; 362 char[] all; 363 int len; 364 List chunks; 365 int i; 366 int last; 367 368 chunks = new ArrayList (); 369 do { 370 chunk = new char[CHUNK]; 371 chunks.add(chunk); 372 len = read(reader, chunk); 373 } while (len == CHUNK); 374 last = chunks.size() - 1; 375 all = new char[last * CHUNK + len]; 376 for (i = 0; i <= last; i++) { 377 System.arraycopy(chunks.get(i), 0, all, CHUNK * i, (i == last)? len : CHUNK); 378 } 379 return all; 380 } 381 382 383 384 389 private static int read(Reader dest, char[] buffer) throws IOException { 390 int ofs; 391 int len; 392 393 for (ofs = 0; ofs < buffer.length; ofs += len) { 394 len = dest.read(buffer, ofs, buffer.length - ofs); 395 if (len == -1) { 396 break; 397 } 398 } 399 return ofs; 400 } 401 402 403 } 404 | Popular Tags |