01: /*
02: * ====================================================================
03: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
04: *
05: * This software is licensed as described in the file COPYING, which
06: * you should have received as part of this distribution. The terms
07: * are also available at http://svnkit.com/license.html
08: * If newer versions of this license are posted there, you may use a
09: * newer version instead, at your option.
10: * ====================================================================
11: */
12: package org.tmatesoft.svn.core.internal.wc.admin;
13:
14: import java.io.IOException;
15: import java.io.InputStream;
16: import java.nio.ByteBuffer;
17: import java.util.Map;
18:
19: import org.tmatesoft.svn.core.internal.wc.SVNSubstitutor;
20:
21: /**
22: * @version 1.1.1
23: * @author TMate Software Ltd.
24: */
25: public class SVNTranslatorInputStream extends InputStream {
26:
27: private InputStream mySource;
28: private ByteBuffer myTranslatedBuffer;
29: private SVNSubstitutor mySubstitutor;
30: private byte[] mySourceBuffer;
31:
32: public SVNTranslatorInputStream(InputStream source, byte[] eols,
33: boolean repair, Map keywords, boolean expand) {
34: mySource = source;
35: mySubstitutor = new SVNSubstitutor(eols, repair, keywords,
36: expand);
37: myTranslatedBuffer = ByteBuffer.allocate(2048);
38: mySourceBuffer = new byte[2048];
39: }
40:
41: public int read() throws IOException {
42: byte[] b = new byte[1];
43: int r = read(b);
44: if (r <= 0) {
45: return -1;
46: }
47: return b[0];
48: }
49:
50: public int read(byte[] b) throws IOException {
51: return read(b, 0, b.length);
52: }
53:
54: public int read(byte[] b, int off, int len) throws IOException {
55: int available = myTranslatedBuffer.position();
56: while (available < len) {
57: int read = mySource.read(mySourceBuffer, 0,
58: mySourceBuffer.length);
59: if (read <= 0) {
60: myTranslatedBuffer = mySubstitutor.translateChunk(null,
61: myTranslatedBuffer);
62: break;
63: }
64: myTranslatedBuffer = mySubstitutor.translateChunk(
65: ByteBuffer.wrap(mySourceBuffer, 0, read),
66: myTranslatedBuffer);
67: available = myTranslatedBuffer.position();
68: }
69: myTranslatedBuffer.flip();
70: len = Math.min(myTranslatedBuffer.remaining(), len);
71: myTranslatedBuffer.get(b, off, len);
72: myTranslatedBuffer.compact();
73: return len;
74: }
75:
76: public void close() throws IOException {
77: mySource.close();
78: }
79: }
|