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 CRLFtoLFInputStream extends FilterInputStream { 26 private boolean pendingByte = false; 27 private int lastByte = -1; 28 29 33 public CRLFtoLFInputStream(InputStream in) { 34 super(in); 35 } 36 37 44 public int read() throws IOException { 45 if (! pendingByte) { 46 lastByte = in.read(); pendingByte = true; } 49 if (lastByte == '\r') { 50 lastByte = in.read(); if (lastByte != '\n') { 52 if (lastByte == -1) pendingByte = false; 53 return '\r'; } 55 } 56 pendingByte = false; 57 return lastByte; 58 } 59 60 67 public int read(byte[] buffer, int off, int len) throws IOException { 68 if (len == 0) { 70 return 0; 71 } else if (len == 1) { 72 int b = read(); 73 if (b == -1) return -1; 74 buffer[off] = (byte) b; 75 return 1; 76 } 77 int count = 0; 80 if (pendingByte) { 81 buffer[off] = (byte) lastByte; 82 pendingByte = false; 83 count = 1; 84 } 85 InterruptedIOException iioe = null; 86 try { 87 len = in.read(buffer, off + count, len - count); 88 if (len == -1) { 89 return (count == 0) ? -1 : count; 90 } 91 } catch (InterruptedIOException e) { 92 len = e.bytesTransferred; 93 iioe = e; 94 } 95 count += len; 96 int j = off; 99 for (int i = off; i < off + count; ++i) { lastByte = buffer[i]; 101 if (lastByte == '\r') { 102 if (pendingByte) { 103 buffer[j++] = '\r'; } else { 105 pendingByte = true; 106 } 107 } else { 108 if (pendingByte) { 109 if (lastByte != '\n') buffer[j++] = '\r'; pendingByte = false; 111 } 112 buffer[j++] = (byte) lastByte; 113 } 114 } 115 if (iioe != null) { 116 iioe.bytesTransferred = j - off; 117 throw iioe; 118 } 119 return j - off; 120 } 121 122 128 public long skip(long count) throws IOException { 129 int actualCount = 0; try { 131 while (count-- > 0 && read() != -1) actualCount++; return actualCount; 133 } catch (InterruptedIOException e) { 134 e.bytesTransferred = actualCount; 135 throw e; 136 } 137 } 138 139 145 public int available() throws IOException { 146 return in.available() / 2; } 148 149 152 public boolean markSupported() { 153 return false; 154 } 155 } 156 | Popular Tags |