Removes spaces (char <= 32) from both start and ends of this bytes array
import java.io.File;
import java.io.FileFilter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
/**
* Various string manipulation methods that are more efficient then chaining
* string operations: all is done in the same buffer without creating a bunch of
* string objects.
*
* @author <a href="mailto:dev@labs.apache.org">Dungeon Project</a>
*/
public class Main {
/**
* <p>
* Removes spaces (char <= 32) from both start and ends of this bytes
* array, handling <code>null</code> by returning <code>null</code>.
* </p>
* Trim removes start and end characters <= 32.
*
* <pre>
* StringUtils.trim(null) = null
* StringUtils.trim("") = ""
* StringUtils.trim(" ") = ""
* StringUtils.trim("abc") = "abc"
* StringUtils.trim(" abc ") = "abc"
* </pre>
*
* @param bytes the byte array to be trimmed, may be null
*
* @return the trimmed byte array
*/
public static final byte[] trim( byte[] bytes )
{
if ( isEmpty( bytes ) )
{
return new byte[0];
}
int start = trimLeft( bytes, 0 );
int end = trimRight( bytes, bytes.length - 1 );
int length = end - start + 1;
if ( length != 0 )
{
byte[] newBytes = new byte[end - start + 1];
System.arraycopy( bytes, start, newBytes, 0, length );
return newBytes;
}
else
{
return new byte[0];
}
}
/**
* <p>
* Removes spaces (char <= 32) from start of this array, handling
* <code>null</code> by returning <code>null</code>.
* </p>
* Trim removes start characters <= 32.
*
* <pre>
* StringUtils.trimLeft(null) = null
* StringUtils.trimLeft("") = ""
* StringUtils.trimLeft(" ") = ""
* StringUtils.trimLeft("abc") = "abc"
* StringUtils.trimLeft(" abc ") = "abc "
* </pre>
*
* @param bytes
* the byte array to be trimmed, may be null
* @return the position of the first byte which is not a space, or the last
* position of the array.
*/
public static final int trimLeft( byte[] bytes, int pos )
{
if ( bytes == null )
{
return pos;
}
while ( ( pos < bytes.length ) && ( bytes[pos] == ' ' ) )
{
pos++;
}
return pos;
}
/**
* <p>
* Removes spaces (char <= 32) from end of this array, handling
* <code>null</code> by returning <code>null</code>.
* </p>
* Trim removes start characters <= 32.
*
* <pre>
* StringUtils.trimRight(null) = null
* StringUtils.trimRight("") = ""
* StringUtils.trimRight(" ") = ""
* StringUtils.trimRight("abc") = "abc"
* StringUtils.trimRight(" abc ") = " abc"
* </pre>
*
* @param bytes
* the chars array to be trimmed, may be null
* @return the position of the first char which is not a space, or the last
* position of the array.
*/
public static final int trimRight( byte[] bytes, int pos )
{
if ( bytes == null )
{
return pos;
}
while ( ( pos >= 0 ) && ( bytes[pos] == ' ' ) )
{
pos--;
}
return pos;
}
/**
* Checks if a bytes array is empty or null.
*
* @param bytes
* The bytes array to check, may be null
* @return <code>true</code> if the bytes array is empty or null
*/
public static final boolean isEmpty( byte[] bytes )
{
return bytes == null || bytes.length == 0;
}
}
Related examples in the same category