1 22 23 28 29 package org.xquark.mapper.util; 30 31 import java.io.IOException ; 32 import java.io.Reader ; 33 import java.util.ArrayList ; 34 35 36 42 public class CharArraysReader extends Reader 43 { 44 private static final String RCSRevision = "$Revision: 1.1 $"; 45 private static final String RCSName = "$Name: $"; 46 47 private ArrayList buffers; 48 private int current = 0; 49 private int offset = 0; 50 private int totalLength = 0; 51 52 53 public CharArraysReader() 54 { 55 buffers = new ArrayList (); 56 } 57 58 public CharArraysReader(char[] str, int offset, int len) 59 { 60 this(); 61 append(str, offset, len); 62 } 63 64 public void append(char[] str, int offset, int len) 65 { 66 buffers.add(new CharBuffer(str, offset, len)); 68 totalLength += len; 69 } 70 71 public int getLength() 72 { 73 return totalLength; 74 } 75 76 public void clear() 77 { 78 buffers.clear(); 79 current = 0; 80 totalLength = 0; 81 offset = 0; 82 } 83 84 public void close() throws IOException 88 { 89 clear(); 90 } 91 92 public int read(char[] values, int off, int len) throws IOException 93 { 94 if (current < buffers.size()) 95 { 96 CharBuffer cb; 97 int remaining = len; 98 int currentRemaining; 99 int toRead; 100 while (remaining > 0) 101 { 102 cb = (CharBuffer)buffers.get(current); 103 currentRemaining = cb.len - offset; 104 if (remaining >= currentRemaining) 105 { 106 toRead = currentRemaining; 107 current++; 108 offset = 0; 109 if (current == buffers.size()) 110 break; 111 } 112 else 113 { 114 toRead = remaining; 115 offset += toRead; 116 } 117 System.arraycopy(cb.chars, cb.off + offset, values, off + (len - remaining), toRead); 118 remaining -= toRead; 119 } 120 return (len - remaining); 121 } 122 else 123 return -1; 124 } 125 126 public void reset() throws IOException 127 { 128 current = 0; 129 offset = 0; 130 } 131 private class CharBuffer 135 { 136 char[] chars; 137 int off; 138 int len; 139 CharBuffer(char[] str, int offset, int len) 140 { 141 this.chars = str; 142 this.off = offset; 143 this.len = len; 144 } 145 } 146 } 147 | Popular Tags |