Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {
    public static byte[] prepareDataToTransmit(String text) {
        if (text.length() == 0)
            return new byte[0];

        byte[] origData = text.getBytes();
        int len = origData.length;

        if (origData[len - 2] == 0x0D && origData[len - 1] == 0x0A) {
            return origData;
        } else {
            int newLen = len;
            if (origData[len - 2] == 0x0A && origData[len - 1] == 0x0D) {
                newLen = len - 2;
            } else if (origData[len - 1] == 0x0D || origData[len - 1] == 0x0A) {
                newLen = len - 1;
            }
            byte[] nData = new byte[newLen + 2];
            System.arraycopy(origData, 0, nData, 0, newLen);
            nData[newLen + 0] = 0x0D;
            nData[newLen + 1] = 0x0A;
            return nData;
        }
    }
}