Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.IOException;

import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.io.UnsupportedEncodingException;
import java.io.Writer;

import javax.xml.transform.stream.StreamResult;

public class Main {
    /**
     * Creates a {@link StreamResult} by wrapping the given outputStream in an
     * {@link OutputStreamWriter} that transforms Windows line endings (\r\n) 
     * into Unix line endings (\n) on Windows for consistency with Roo's templates.  
     * 
     * @param outputStream
     * @return StreamResult 
     * @throws UnsupportedEncodingException 
     */
    private static StreamResult createUnixStreamResultForEntry(OutputStream outputStream)
            throws UnsupportedEncodingException {
        final Writer writer;
        if (System.getProperty("line.separator").equals("\r\n")) {
            writer = new OutputStreamWriter(outputStream, "ISO-8859-1") {
                public void write(char[] cbuf, int off, int len) throws IOException {
                    for (int i = off; i < off + len; i++) {
                        if (cbuf[i] != '\r' || (i < cbuf.length - 1 && cbuf[i + 1] != '\n')) {
                            super.write(cbuf[i]);
                        }
                    }
                }

                public void write(int c) throws IOException {
                    if (c != '\r')
                        super.write(c);
                }

                public void write(String str, int off, int len) throws IOException {
                    String orig = str.substring(off, off + len);
                    String filtered = orig.replace("\r\n", "\n");
                    int lengthDiff = orig.length() - filtered.length();
                    if (filtered.endsWith("\r")) {
                        super.write(filtered.substring(0, filtered.length() - 1), 0, len - lengthDiff - 1);
                    } else {
                        super.write(filtered, 0, len - lengthDiff);
                    }
                }
            };
        } else {
            writer = new OutputStreamWriter(outputStream, "ISO-8859-1");
        }
        return new StreamResult(writer);
    }
}