Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*  Copyright (c) 2010 Xiaoyun Zhu
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy  
 *  of this software and associated documentation files (the "Software"), to deal  
 *  in the Software without restriction, including without limitation the rights  
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 *  copies of the Software, and to permit persons to whom the Software is  
 *  furnished to do so, subject to the following conditions:
 *  
 *  The above copyright notice and this permission notice shall be included in  
 *  all copies or substantial portions of the Software.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
 *  THE SOFTWARE.  
 */

import java.nio.ByteBuffer;

public class Main {
    public final static String substringBetweenLast(final byte[] text, final int offset, final int limit,
            final byte[] start, final byte[] end) {
        return substringBetweenLast(text, offset, limit, start, end, true);
    }

    public final static String substringBetweenLast(final byte[] text, final int offset, final int limit,
            final byte[] start, final byte[] end, final boolean trim) {
        int nEnd = lastIndexOf(text, offset, limit, end);
        int nStart = -1;
        if (nEnd > start.length) {
            nStart = lastIndexOf(text, offset, nEnd - 1, start);
        } else {
            return null;
        }
        if (nStart < nEnd && nStart != -1 && nEnd != -1) {
            nStart += start.length;
            String str = new String(text, nStart, nEnd - nStart);
            if (trim) {
                return str.trim();
            } else {
                return str;
            }
        } else {
            return null;
        }
    }

    public final static int substringBetweenLast(final byte[] text, final int offset, final int limit,
            final byte[] start, final byte[] end, final boolean trim, ByteBuffer bb) {
        int nEnd = lastIndexOf(text, offset, limit, end);
        int nStart = -1;
        if (nEnd > start.length) {
            nStart = lastIndexOf(text, offset, nEnd - 1, start);
            if (nStart < nEnd && nStart != -1 && nEnd != -1) {
                nStart += start.length;
                if (trim) {
                    byte c;
                    int i;
                    for (i = nStart; i < nEnd; i++) {
                        c = text[i];
                        if (c != ' ' && c != '\t' && c != '\r') {
                            break;
                        }
                    }
                    nStart = i;
                    for (i = nEnd; i >= nStart; i--) {
                        c = text[i];
                        if (c != ' ' && c != '\t' && c != '\r') {
                            break;
                        }
                    }
                    nEnd = i;
                }
                if (nEnd > nStart) {
                    int len = nEnd - nStart;
                    System.arraycopy(text, nStart, bb.array(), 0, len);
                    bb.limit(len);
                } else {
                    bb.limit(0);
                }
            } else {
                bb.limit(0);
            }
        } else {
            bb.limit(0);
        }
        return bb.limit();
    }

    public final static int substringBetweenLast(final byte[] text, final int offset, final int limit,
            final byte[] start, final byte[] end, ByteBuffer bb) {
        return substringBetweenLast(text, offset, limit, start, end, true, bb);
    }

    public final static int lastIndexOf(final byte[] text, final int offset, final int limit, final byte[] s) {
        final int len = s.length;
        if (limit >= len) {
            final int size = limit - len + 1;
            for (int i = size - 1; i >= offset; i--) {
                if (equals(text, i, s, 0, len)) {
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * 
     * @param array
     * @param offset
     *            absolute
     * @param limit
     *            absolute
     * @param b
     * @return absolute
     */
    public final static int lastIndexOf(final byte[] array, final int offset, final int limit, final byte b) {
        for (int i = limit - 1; i >= offset; i--) {
            if (array[i] == b) {
                return i;
            }
        }
        return -1;
    }

    public final static boolean equals(final byte[] array1, final int start1, final byte[] array2, final int start2,
            final int len) {
        if (start1 + len > array1.length || start2 + len > array2.length) {
            return false;
        }
        for (int i = 0; i < len; i++) {
            if (array1[start1 + i] != array2[start2 + i]) {
                return false;
            }
        }
        return true;
    }

    public final static boolean equals(final byte[] array1, final int offset1, final int end1, final byte[] array2,
            final int offset2, final int end2) {
        final int len1 = end1 - offset1;
        final int len2 = end2 - offset2;
        if (len1 != len2) {
            return false;
        } else {
            return equals(array1, offset1, array2, offset2, len1);
        }
    }

    public final static boolean equals(final byte[] array1, final int offset1, final byte[] array2) {
        return equals(array1, offset1, array2, 0, array2.length);
    }

    public final static boolean equals(final ByteBuffer bb1, final ByteBuffer bb2) {
        final byte[] array1 = bb1.array();
        final byte[] array2 = bb2.array();
        final int end1 = bb1.limit();
        final int end2 = bb2.limit();
        return equals(array1, 0, end1, array2, 0, end2);
    }
}