KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > client > CRLFDetectInputStream


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.client;
12
13 import java.io.*;
14
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.team.internal.ccvs.core.*;
19
20 /**
21  * Stream which detects CRLF in text file contents recieved from the server
22  */

23 public class CRLFDetectInputStream extends FilterInputStream {
24
25     private boolean previousCR;
26     private String JavaDoc filename;
27     private boolean reported = false;
28
29     protected CRLFDetectInputStream(InputStream in, ICVSStorage file) {
30         super(in);
31         try {
32             this.filename = getFileName(file);
33         } catch (CVSException e) {
34             this.filename = file.getName();
35         }
36     }
37
38     private String JavaDoc getFileName(ICVSStorage storage) throws CVSException {
39         String JavaDoc fileName;
40         if (storage instanceof ICVSFile) {
41             ICVSFile file = (ICVSFile)storage;
42             fileName = file.getRepositoryRelativePath();
43             if (fileName == null) {
44                 IResource resource = file.getIResource();
45                 if (resource == null) {
46                     fileName = file.getName();
47                 } else {
48                     // Use the resource path if there is one since the remote pat
49
fileName = file.getIResource().getFullPath().toString();
50                 }
51             }
52         } else {
53             fileName = storage.getName();
54         }
55         return fileName;
56     }
57
58     /**
59      * Wraps the underlying stream's method.
60      * Translates CR/LF sequences to LFs transparently.
61      * @throws InterruptedIOException if the operation was interrupted before all of the
62      * bytes specified have been skipped, bytesTransferred will be zero
63      * @throws IOException if an i/o error occurs
64      */

65     public int read() throws IOException {
66         int next = in.read();
67         if (next != -1) {
68             testForCRLF((byte)next);
69         }
70         return next;
71     }
72
73     /**
74      * Wraps the underlying stream's method.
75      * Translates CR/LF sequences to LFs transparently.
76      * @throws InterruptedIOException if the operation was interrupted before all of the
77      * bytes specified have been skipped, bytesTransferred may be non-zero
78      * @throws IOException if an i/o error occurs
79      */

80     public int read(byte[] buffer, int off, int len) throws IOException {
81         int count = super.read(buffer, off, len);
82         for (int i = off; i < count; i++) {
83             testForCRLF(buffer[i]);
84         }
85         return count;
86     }
87     
88     /**
89      * Test the byte to see if a CRLF sequence was read
90      */

91     private void testForCRLF(byte next) {
92         if (reported) return;
93         if (previousCR && next == '\n') {
94             CVSProviderPlugin.log(IStatus.WARNING, NLS.bind(CVSMessages.CRLFDetectInputStream_0, new String JavaDoc[] { filename }), null);
95             reported = true;
96         }
97         previousCR = (next == '\r');
98     }
99 }
100
Popular Tags