1 11 package org.eclipse.team.internal.core.streams; 12 13 import java.io.FilterInputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.InterruptedIOException ; 17 18 25 public class LFtoCRLFInputStream extends FilterInputStream { 26 private boolean mustReturnLF = false; 27 28 32 public LFtoCRLFInputStream(InputStream in) { 33 super(in); 34 } 35 36 43 public int read() throws IOException { 44 if (mustReturnLF) { 45 mustReturnLF = false; 46 return '\n'; 47 } 48 int b = in.read(); if (b == '\n') { 50 mustReturnLF = true; 51 b = '\r'; 52 } 53 return b; 54 } 55 56 63 public int read(byte[] buffer, int off, int len) throws IOException { 64 if (len == 0) { 66 return 0; 67 } else if (len == 1) { 68 int b = read(); 69 if (b == -1) return -1; 70 buffer[off] = (byte) b; 71 return 1; 72 } 73 int count = 0; 75 if (mustReturnLF) { 76 mustReturnLF = false; 77 buffer[off++] = '\n'; 78 --len; 79 count = 1; 80 if (len < 2) return count; } 82 len /= 2; 85 int j = off + len; 86 InterruptedIOException iioe = null; 87 try { 88 len = in.read(buffer, j, len); 89 if (len == -1) { 90 return (count == 0) ? -1 : count; 91 } 92 } catch (InterruptedIOException e) { 93 len = e.bytesTransferred; 94 iioe = e; 95 } 96 count += len; 97 while (len-- > 0) { 99 byte b = buffer[j++]; 100 if (b == '\n') { 101 buffer[off++] = '\r'; 102 count++; 103 } 104 buffer[off++] = b; 105 } 106 if (iioe != null) { 107 iioe.bytesTransferred = count; 108 throw iioe; 109 } 110 return count; 111 } 112 113 119 public long skip(long count) throws IOException { 120 int actualCount = 0; try { 122 while (count-- > 0 && read() != -1) actualCount++; return actualCount; 124 } catch (InterruptedIOException e) { 125 e.bytesTransferred = actualCount; 126 throw e; 127 } 128 } 129 130 136 public int available() throws IOException { 137 return in.available(); } 139 140 143 public boolean markSupported() { 144 return false; 145 } 146 } 147 | Popular Tags |