|
| 1 | +package biz.aQute.foreign.python.provider; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.io.InterruptedIOException; |
| 6 | + |
| 7 | +public class Exchange extends InputStream implements Appendable { |
| 8 | + final static int MASK = 0x1FFF; |
| 9 | + char buffer[] = new char[MASK + 1]; |
| 10 | + int available = buffer.length; |
| 11 | + int in, out; |
| 12 | + int count = 0; |
| 13 | + int codepoint; |
| 14 | + final String name; |
| 15 | + |
| 16 | + public Exchange(String name) { |
| 17 | + this.name = name; |
| 18 | + } |
| 19 | + |
| 20 | + @Override |
| 21 | + public Appendable append(CharSequence csq) throws IOException { |
| 22 | + return append(csq, 0, csq.length()); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public Appendable append(CharSequence csq, int start, int end) throws IOException { |
| 27 | + for (int i = start; i < end; i++) |
| 28 | + append(csq.charAt(i)); |
| 29 | + return this; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public synchronized Appendable append(char c) throws IOException { |
| 34 | + while (available == 0) { |
| 35 | + try { |
| 36 | + wait(); |
| 37 | + } catch (InterruptedException e) { |
| 38 | + throw new InterruptedIOException(); |
| 39 | + } |
| 40 | + } |
| 41 | + available--; |
| 42 | + buffer[in] = c; |
| 43 | + in = (in + 1) & MASK; |
| 44 | + notifyAll(); |
| 45 | + return this; |
| 46 | + } |
| 47 | + |
| 48 | + public synchronized char readChar() throws InterruptedIOException { |
| 49 | + while (in == out) { |
| 50 | + try { |
| 51 | + wait(); |
| 52 | + } catch (InterruptedException e) { |
| 53 | + throw new InterruptedIOException(); |
| 54 | + } |
| 55 | + } |
| 56 | + char c = buffer[out]; |
| 57 | + out = (out + 1) & MASK; |
| 58 | + available++; |
| 59 | + return c; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public int read() throws IOException { |
| 64 | + if (count-- > 0) { |
| 65 | + int r = codepoint >> (count * 6); |
| 66 | + r &= 0b0011_1111; |
| 67 | + r |= 0b1000_0000; |
| 68 | + return r; |
| 69 | + } |
| 70 | + |
| 71 | + codepoint = readChar(); |
| 72 | + if (codepoint < 0) |
| 73 | + return -1; |
| 74 | + if (codepoint > 0xFFFF) |
| 75 | + return 0; |
| 76 | + |
| 77 | + if (codepoint < 0x80) |
| 78 | + return codepoint; |
| 79 | + |
| 80 | + if (codepoint < 0x800) { |
| 81 | + count = 1; |
| 82 | + int v = (codepoint >> 6) | 0b1100_0000; |
| 83 | + return v; |
| 84 | + } |
| 85 | + count = 2; |
| 86 | + int v = (codepoint >> 12) | 0b1110_0000; |
| 87 | + return v; |
| 88 | + } |
| 89 | + |
| 90 | + public synchronized int available() { |
| 91 | + return available; |
| 92 | + } |
| 93 | + |
| 94 | + public synchronized String toString() { |
| 95 | + StringBuilder sb = new StringBuilder(name).append(": "); |
| 96 | + int i=out; |
| 97 | + while ( i!=out) { |
| 98 | + sb.append(buffer[i]); |
| 99 | + i = (i+1) & MASK; |
| 100 | + } |
| 101 | + return sb.toString(); |
| 102 | + } |
| 103 | +} |
0 commit comments