Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.io.InputStream;

public class Main {

    public static final void skipLinesUntil(InputStream br, String ln) throws Exception {
        String s = readLine(br);
        while ((s != null) && !s.equals(ln)) {
            s = readLine(br);
        }
    }

    public static final String readLine(InputStream in) throws IOException {
        return readLine(in, null);
    }

    public static final String readLine(InputStream in, String charset) throws IOException {
        int rd = in.read();
        if (rd == -1)
            return null;
        byte r = (byte) rd;
        int i = 0;
        int l = 50;
        byte[] buf = new byte[l];
        while (r != '\n') {
            if (i >= l - 1) {
                l += 50;
                byte[] old = buf;
                buf = new byte[l];
                System.arraycopy(old, 0, buf, 0, old.length);
            }
            if (r != '\r')
                buf[i++] = r;
            rd = in.read();
            if (rd == -1)
                break;
            r = (byte) rd;
        }
        if (charset == null) {
            return new String(buf, 0, i);
        }
        return new String(buf, 0, i, charset);
    }
}