mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Moving to root below branch label
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2005 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
847
source/java/org/alfresco/filesys/util/DataBuffer.java
Normal file
847
source/java/org/alfresco/filesys/util/DataBuffer.java
Normal file
@@ -0,0 +1,847 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.alfresco.filesys.util;
|
||||
|
||||
/**
|
||||
* Data Buffer Class
|
||||
* <p>
|
||||
* Dynamic buffer for getting/setting data blocks.
|
||||
*/
|
||||
public class DataBuffer
|
||||
{
|
||||
|
||||
// Constants
|
||||
|
||||
private static final int DefaultBufferSize = 256;
|
||||
|
||||
// Data buffer, current position and offset
|
||||
|
||||
private byte[] m_data;
|
||||
private int m_pos;
|
||||
private int m_endpos;
|
||||
private int m_offset;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*/
|
||||
public DataBuffer()
|
||||
{
|
||||
m_data = new byte[DefaultBufferSize];
|
||||
m_pos = 0;
|
||||
m_offset = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a data buffer to write data to
|
||||
*
|
||||
* @param siz int
|
||||
*/
|
||||
public DataBuffer(int siz)
|
||||
{
|
||||
m_data = new byte[siz];
|
||||
m_pos = 0;
|
||||
m_offset = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a data buffer to read data from
|
||||
*
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param len int
|
||||
*/
|
||||
public DataBuffer(byte[] buf, int off, int len)
|
||||
{
|
||||
m_data = buf;
|
||||
m_offset = off;
|
||||
m_pos = off;
|
||||
m_endpos = off + len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the data buffer
|
||||
*
|
||||
* @return byte[]
|
||||
*/
|
||||
public final byte[] getBuffer()
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the data length
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getLength()
|
||||
{
|
||||
if (m_endpos != 0)
|
||||
return m_endpos - m_offset;
|
||||
return m_pos - m_offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the data length in words
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getLengthInWords()
|
||||
{
|
||||
return getLength() / 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the available data length
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getAvailableLength()
|
||||
{
|
||||
if (m_endpos == 0)
|
||||
return -1;
|
||||
return m_endpos - m_pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the displacement from the start of the buffer to the current buffer position
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getDisplacement()
|
||||
{
|
||||
return m_pos - m_offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the buffer base offset
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getOffset()
|
||||
{
|
||||
return m_offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a byte from the buffer
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getByte()
|
||||
{
|
||||
|
||||
// Check if there is enough data in the buffer
|
||||
|
||||
if (m_data.length - m_pos < 1)
|
||||
throw new ArrayIndexOutOfBoundsException("End of data buffer");
|
||||
|
||||
// Unpack the byte value
|
||||
|
||||
int bval = (int) (m_data[m_pos] & 0xFF);
|
||||
m_pos++;
|
||||
return bval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a short from the buffer
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getShort()
|
||||
{
|
||||
|
||||
// Check if there is enough data in the buffer
|
||||
|
||||
if (m_data.length - m_pos < 2)
|
||||
throw new ArrayIndexOutOfBoundsException("End of data buffer");
|
||||
|
||||
// Unpack the integer value
|
||||
|
||||
int sval = (int) DataPacker.getIntelShort(m_data, m_pos);
|
||||
m_pos += 2;
|
||||
return sval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an integer from the buffer
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getInt()
|
||||
{
|
||||
|
||||
// Check if there is enough data in the buffer
|
||||
|
||||
if (m_data.length - m_pos < 4)
|
||||
throw new ArrayIndexOutOfBoundsException("End of data buffer");
|
||||
|
||||
// Unpack the integer value
|
||||
|
||||
int ival = DataPacker.getIntelInt(m_data, m_pos);
|
||||
m_pos += 4;
|
||||
return ival;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a long (64 bit) value from the buffer
|
||||
*
|
||||
* @return long
|
||||
*/
|
||||
public final long getLong()
|
||||
{
|
||||
|
||||
// Check if there is enough data in the buffer
|
||||
|
||||
if (m_data.length - m_pos < 8)
|
||||
throw new ArrayIndexOutOfBoundsException("End of data buffer");
|
||||
|
||||
// Unpack the long value
|
||||
|
||||
long lval = DataPacker.getIntelLong(m_data, m_pos);
|
||||
m_pos += 8;
|
||||
return lval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string from the buffer
|
||||
*
|
||||
* @param uni boolean
|
||||
* @return String
|
||||
*/
|
||||
public final String getString(boolean uni)
|
||||
{
|
||||
return getString(255, uni);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a string from the buffer
|
||||
*
|
||||
* @param maxlen int
|
||||
* @param uni boolean
|
||||
* @return String
|
||||
*/
|
||||
public final String getString(int maxlen, boolean uni)
|
||||
{
|
||||
|
||||
// Check for Unicode or ASCII
|
||||
|
||||
String ret = null;
|
||||
int availLen = -1;
|
||||
|
||||
if (uni)
|
||||
{
|
||||
|
||||
// Word align the current buffer position, calculate the available
|
||||
// length
|
||||
|
||||
m_pos = DataPacker.wordAlign(m_pos);
|
||||
availLen = (m_endpos - m_pos) / 2;
|
||||
if (availLen < maxlen)
|
||||
maxlen = availLen;
|
||||
|
||||
ret = DataPacker.getUnicodeString(m_data, m_pos, maxlen);
|
||||
if (ret != null)
|
||||
m_pos += (ret.length() * 2) + 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// Calculate the available length
|
||||
|
||||
availLen = m_endpos - m_pos;
|
||||
if (availLen < maxlen)
|
||||
maxlen = availLen;
|
||||
|
||||
// Unpack the ASCII string
|
||||
|
||||
ret = DataPacker.getString(m_data, m_pos, maxlen);
|
||||
if (ret != null)
|
||||
m_pos += ret.length() + 1;
|
||||
}
|
||||
|
||||
// Return the string
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a short from the buffer at the specified index
|
||||
*
|
||||
* @param idx int
|
||||
* @return int
|
||||
*/
|
||||
public final int getShortAt(int idx)
|
||||
{
|
||||
|
||||
// Check if there is enough data in the buffer
|
||||
|
||||
int pos = m_offset + (idx * 2);
|
||||
if (m_data.length - pos < 2)
|
||||
throw new ArrayIndexOutOfBoundsException("End of data buffer");
|
||||
|
||||
// Unpack the integer value
|
||||
|
||||
int sval = (int) DataPacker.getIntelShort(m_data, pos) & 0xFFFF;
|
||||
return sval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an integer from the buffer at the specified index
|
||||
*
|
||||
* @param idx int
|
||||
* @return int
|
||||
*/
|
||||
public final int getIntAt(int idx)
|
||||
{
|
||||
|
||||
// Check if there is enough data in the buffer
|
||||
|
||||
int pos = m_offset + (idx * 2);
|
||||
if (m_data.length - pos < 4)
|
||||
throw new ArrayIndexOutOfBoundsException("End of data buffer");
|
||||
|
||||
// Unpack the integer value
|
||||
|
||||
int ival = DataPacker.getIntelInt(m_data, pos);
|
||||
return ival;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a long (64 bit) value from the buffer at the specified index
|
||||
*
|
||||
* @param idx int
|
||||
* @return long
|
||||
*/
|
||||
public final long getLongAt(int idx)
|
||||
{
|
||||
|
||||
// Check if there is enough data in the buffer
|
||||
|
||||
int pos = m_offset + (idx * 2);
|
||||
if (m_data.length - pos < 8)
|
||||
throw new ArrayIndexOutOfBoundsException("End of data buffer");
|
||||
|
||||
// Unpack the long value
|
||||
|
||||
long lval = DataPacker.getIntelLong(m_data, pos);
|
||||
return lval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Skip over a number of bytes
|
||||
*
|
||||
* @param cnt int
|
||||
*/
|
||||
public final void skipBytes(int cnt)
|
||||
{
|
||||
|
||||
// Check if there is enough data in the buffer
|
||||
|
||||
if (m_data.length - m_pos < cnt)
|
||||
throw new ArrayIndexOutOfBoundsException("End of data buffer");
|
||||
|
||||
// Skip bytes
|
||||
|
||||
m_pos += cnt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the data position
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getPosition()
|
||||
{
|
||||
return m_pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the read/write buffer position
|
||||
*
|
||||
* @param pos int
|
||||
*/
|
||||
public final void setPosition(int pos)
|
||||
{
|
||||
m_pos = pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the end of buffer position, and reset the read position to the beginning of the buffer
|
||||
*/
|
||||
public final void setEndOfBuffer()
|
||||
{
|
||||
m_endpos = m_pos;
|
||||
m_pos = m_offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the data length
|
||||
*
|
||||
* @param len int
|
||||
*/
|
||||
public final void setLength(int len)
|
||||
{
|
||||
m_pos = m_offset + len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a byte value to the buffer
|
||||
*
|
||||
* @param bval int
|
||||
*/
|
||||
public final void putByte(int bval)
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
if (m_data.length - m_pos < 1)
|
||||
extendBuffer();
|
||||
|
||||
// Pack the byte value
|
||||
|
||||
m_data[m_pos++] = (byte) (bval & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a short value to the buffer
|
||||
*
|
||||
* @param sval int
|
||||
*/
|
||||
public final void putShort(int sval)
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
if (m_data.length - m_pos < 2)
|
||||
extendBuffer();
|
||||
|
||||
// Pack the short value
|
||||
|
||||
DataPacker.putIntelShort(sval, m_data, m_pos);
|
||||
m_pos += 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append an integer to the buffer
|
||||
*
|
||||
* @param ival int
|
||||
*/
|
||||
public final void putInt(int ival)
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
if (m_data.length - m_pos < 4)
|
||||
extendBuffer();
|
||||
|
||||
// Pack the integer value
|
||||
|
||||
DataPacker.putIntelInt(ival, m_data, m_pos);
|
||||
m_pos += 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a long to the buffer
|
||||
*
|
||||
* @param lval long
|
||||
*/
|
||||
public final void putLong(long lval)
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
if (m_data.length - m_pos < 8)
|
||||
extendBuffer();
|
||||
|
||||
// Pack the long value
|
||||
|
||||
DataPacker.putIntelLong(lval, m_data, m_pos);
|
||||
m_pos += 8;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a short value to the buffer at the specified index
|
||||
*
|
||||
* @param idx int
|
||||
* @param sval int
|
||||
*/
|
||||
public final void putShortAt(int idx, int sval)
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
int pos = m_offset + (idx * 2);
|
||||
if (m_data.length - pos < 2)
|
||||
extendBuffer();
|
||||
|
||||
// Pack the short value
|
||||
|
||||
DataPacker.putIntelShort(sval, m_data, pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append an integer to the buffer at the specified index
|
||||
*
|
||||
* @param idx int
|
||||
* @param ival int
|
||||
*/
|
||||
public final void putIntAt(int idx, int ival)
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
int pos = m_offset = (idx * 2);
|
||||
if (m_data.length - pos < 4)
|
||||
extendBuffer();
|
||||
|
||||
// Pack the integer value
|
||||
|
||||
DataPacker.putIntelInt(ival, m_data, pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a long to the buffer at the specified index
|
||||
*
|
||||
* @param idx int
|
||||
* @param lval long
|
||||
*/
|
||||
public final void putLongAt(int idx, int lval)
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
int pos = m_offset = (idx * 2);
|
||||
if (m_data.length - pos < 8)
|
||||
extendBuffer();
|
||||
|
||||
// Pack the long value
|
||||
|
||||
DataPacker.putIntelLong(lval, m_data, pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a string to the buffer
|
||||
*
|
||||
* @param str String
|
||||
* @param uni boolean
|
||||
*/
|
||||
public final void putString(String str, boolean uni)
|
||||
{
|
||||
putString(str, uni, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a string to the buffer
|
||||
*
|
||||
* @param str String
|
||||
* @param uni boolean
|
||||
* @param nulTerm boolean
|
||||
*/
|
||||
public final void putString(String str, boolean uni, boolean nulTerm)
|
||||
{
|
||||
|
||||
// Check for Unicode or ASCII
|
||||
|
||||
if (uni)
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
int bytLen = str.length() * 2;
|
||||
if (m_data.length - m_pos < bytLen)
|
||||
extendBuffer(bytLen + 4);
|
||||
|
||||
// Word align the buffer position, pack the Unicode string
|
||||
|
||||
m_pos = DataPacker.wordAlign(m_pos);
|
||||
DataPacker.putUnicodeString(str, m_data, m_pos, nulTerm);
|
||||
m_pos += (str.length() * 2);
|
||||
if (nulTerm)
|
||||
m_pos += 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
if (m_data.length - m_pos < str.length())
|
||||
extendBuffer(str.length() + 2);
|
||||
|
||||
// Pack the ASCII string
|
||||
|
||||
DataPacker.putString(str, m_data, m_pos, nulTerm);
|
||||
m_pos += str.length();
|
||||
if (nulTerm)
|
||||
m_pos++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a fixed length string to the buffer
|
||||
*
|
||||
* @param str String
|
||||
* @param len int
|
||||
*/
|
||||
public final void putFixedString(String str, int len)
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
if (m_data.length - m_pos < str.length())
|
||||
extendBuffer(str.length() + 2);
|
||||
|
||||
// Pack the ASCII string
|
||||
|
||||
DataPacker.putString(str, len, m_data, m_pos);
|
||||
m_pos += len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a string to the buffer at the specified buffer position
|
||||
*
|
||||
* @param str String
|
||||
* @param pos int
|
||||
* @param uni boolean
|
||||
* @param nulTerm boolean
|
||||
* @return int
|
||||
*/
|
||||
public final int putStringAt(String str, int pos, boolean uni, boolean nulTerm)
|
||||
{
|
||||
|
||||
// Check for Unicode or ASCII
|
||||
|
||||
int retPos = -1;
|
||||
|
||||
if (uni)
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
int bytLen = str.length() * 2;
|
||||
if (m_data.length - pos < bytLen)
|
||||
extendBuffer(bytLen + 4);
|
||||
|
||||
// Word align the buffer position, pack the Unicode string
|
||||
|
||||
pos = DataPacker.wordAlign(pos);
|
||||
retPos = DataPacker.putUnicodeString(str, m_data, pos, nulTerm);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
if (m_data.length - pos < str.length())
|
||||
extendBuffer(str.length() + 2);
|
||||
|
||||
// Pack the ASCII string
|
||||
|
||||
retPos = DataPacker.putString(str, m_data, pos, nulTerm);
|
||||
}
|
||||
|
||||
// Return the end of string buffer position
|
||||
|
||||
return retPos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a fixed length string to the buffer at the specified position
|
||||
*
|
||||
* @param str String
|
||||
* @param len int
|
||||
* @param pos int
|
||||
* @return int
|
||||
*/
|
||||
public final int putFixedStringAt(String str, int len, int pos)
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
if (m_data.length - pos < str.length())
|
||||
extendBuffer(str.length() + 2);
|
||||
|
||||
// Pack the ASCII string
|
||||
|
||||
return DataPacker.putString(str, len, m_data, pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a string pointer to the specified buffer offset
|
||||
*
|
||||
* @param off int
|
||||
*/
|
||||
public final void putStringPointer(int off)
|
||||
{
|
||||
|
||||
// Calculate the offset from the start of the data buffer to the string
|
||||
// position
|
||||
|
||||
DataPacker.putIntelInt(off - m_offset, m_data, m_pos);
|
||||
m_pos += 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append zero bytes to the buffer
|
||||
*
|
||||
* @param cnt int
|
||||
*/
|
||||
public final void putZeros(int cnt)
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
if (m_data.length - m_pos < cnt)
|
||||
extendBuffer(cnt);
|
||||
|
||||
// Pack the zero bytes
|
||||
|
||||
for (int i = 0; i < cnt; i++)
|
||||
m_data[m_pos++] = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Word align the buffer position
|
||||
*/
|
||||
public final void wordAlign()
|
||||
{
|
||||
m_pos = DataPacker.wordAlign(m_pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Longword align the buffer position
|
||||
*/
|
||||
public final void longwordAlign()
|
||||
{
|
||||
m_pos = DataPacker.longwordAlign(m_pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a raw data block to the data buffer
|
||||
*
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param len int
|
||||
*/
|
||||
public final void appendData(byte[] buf, int off, int len)
|
||||
{
|
||||
|
||||
// Check if there is enough space in the buffer
|
||||
|
||||
if (m_data.length - m_pos < len)
|
||||
extendBuffer(len);
|
||||
|
||||
// Copy the data to the buffer and update the current write position
|
||||
|
||||
System.arraycopy(buf, off, m_data, m_pos, len);
|
||||
m_pos += len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy all data from the data buffer to the user buffer, and update the read position
|
||||
*
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @return int
|
||||
*/
|
||||
public final int copyData(byte[] buf, int off)
|
||||
{
|
||||
return copyData(buf, off, getLength());
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy data from the data buffer to the user buffer, and update the current read position.
|
||||
*
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param cnt int
|
||||
* @return int
|
||||
*/
|
||||
public final int copyData(byte[] buf, int off, int cnt)
|
||||
{
|
||||
|
||||
// Check if there is any more data to copy
|
||||
|
||||
if (m_pos == m_endpos)
|
||||
return 0;
|
||||
|
||||
// Calculate the amount of data to copy
|
||||
|
||||
int siz = m_endpos - m_pos;
|
||||
if (siz > cnt)
|
||||
siz = cnt;
|
||||
|
||||
// Copy the data to the user buffer and update the current read position
|
||||
|
||||
System.arraycopy(m_data, m_pos, buf, off, siz);
|
||||
m_pos += siz;
|
||||
|
||||
// Return the amount of data copied
|
||||
|
||||
return siz;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend the data buffer by the specified amount
|
||||
*
|
||||
* @param ext int
|
||||
*/
|
||||
private final void extendBuffer(int ext)
|
||||
{
|
||||
|
||||
// Create a new buffer of the required size
|
||||
|
||||
byte[] newBuf = new byte[m_data.length + ext];
|
||||
|
||||
// Copy the data from the current buffer to the new buffer
|
||||
|
||||
System.arraycopy(m_data, 0, newBuf, 0, m_data.length);
|
||||
|
||||
// Set the new buffer to be the main buffer
|
||||
|
||||
m_data = newBuf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend the data buffer, double the currently allocated buffer size
|
||||
*/
|
||||
private final void extendBuffer()
|
||||
{
|
||||
extendBuffer(m_data.length * 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the data buffer details as a string
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
StringBuffer str = new StringBuffer();
|
||||
|
||||
str.append("[data=");
|
||||
str.append(m_data);
|
||||
str.append(",");
|
||||
str.append(m_pos);
|
||||
str.append("/");
|
||||
str.append(m_offset);
|
||||
str.append("/");
|
||||
str.append(getLength());
|
||||
str.append("]");
|
||||
|
||||
return str.toString();
|
||||
}
|
||||
}
|
778
source/java/org/alfresco/filesys/util/DataPacker.java
Normal file
778
source/java/org/alfresco/filesys/util/DataPacker.java
Normal file
@@ -0,0 +1,778 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.alfresco.filesys.util;
|
||||
|
||||
/**
|
||||
* The data packing class is a static class that is used to pack and unpack basic data types to/from
|
||||
* network byte order and Intel byte order.
|
||||
*/
|
||||
public final class DataPacker
|
||||
{
|
||||
|
||||
// Flag to indicate the byte order of the platform that we are currently
|
||||
// running on.
|
||||
|
||||
private static boolean bigEndian = false;
|
||||
|
||||
/**
|
||||
* Return the current endian setting.
|
||||
*
|
||||
* @return true if the system is big endian, else false.
|
||||
*/
|
||||
public final static boolean isBigEndian()
|
||||
{
|
||||
return bigEndian;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack a null terminated data string from the data buffer.
|
||||
*
|
||||
* @param typ Data type, as specified by SMBDataType.
|
||||
* @param bytarray Byte array to unpack the string value from.
|
||||
* @param pos Offset to start unpacking the string value.
|
||||
* @param maxlen Maximum length of data to be searched for a null character.
|
||||
* @param uni String is Unicode if true, else ASCII
|
||||
* @return String, else null if the terminating null character was not found.
|
||||
*/
|
||||
public final static String getDataString(char typ, byte[] bytarray, int pos, int maxlen, boolean uni)
|
||||
{
|
||||
|
||||
// Check if the data string has the required data type
|
||||
|
||||
if (bytarray[pos++] == (byte) typ)
|
||||
{
|
||||
|
||||
// Extract the null terminated string
|
||||
|
||||
if (uni == true)
|
||||
return getUnicodeString(bytarray, wordAlign(pos), maxlen / 2);
|
||||
else
|
||||
return getString(bytarray, pos, maxlen - 1);
|
||||
}
|
||||
|
||||
// Invalid data type
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack a 32-bit integer.
|
||||
*
|
||||
* @param buf Byte buffer containing the integer to be unpacked.
|
||||
* @param pos Position within the buffer that the integer is stored.
|
||||
* @return The unpacked 32-bit integer value.
|
||||
* @exception java.lang.IndexOutOfBoundsException If there is not enough data in the buffer.
|
||||
*/
|
||||
public final static int getInt(byte[] buf, int pos) throws java.lang.IndexOutOfBoundsException
|
||||
{
|
||||
|
||||
// Check if the byte array is long enough
|
||||
|
||||
if (buf.length < pos + 3)
|
||||
throw new java.lang.IndexOutOfBoundsException();
|
||||
|
||||
// Unpack the 32-bit value
|
||||
|
||||
int i1 = (int) buf[pos] & 0xFF;
|
||||
int i2 = (int) buf[pos + 1] & 0xFF;
|
||||
int i3 = (int) buf[pos + 2] & 0xFF;
|
||||
int i4 = (int) buf[pos + 3] & 0xFF;
|
||||
|
||||
int iVal = (i1 << 24) + (i2 << 16) + (i3 << 8) + i4;
|
||||
|
||||
// Return the unpacked value
|
||||
|
||||
return iVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack a 32-bit integer that is stored in Intel format.
|
||||
*
|
||||
* @param bytarray Byte array containing the Intel integer to be unpacked.
|
||||
* @param pos Offset that the Intel integer is stored within the byte array.
|
||||
* @return Unpacked integer value.
|
||||
* @exception java.lang.IndexOutOfBoundsException If there is not enough data in the buffer.
|
||||
*/
|
||||
public final static int getIntelInt(byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException
|
||||
{
|
||||
|
||||
// Check if the byte array is long enough to restore the int
|
||||
|
||||
if (bytarray.length < pos + 3)
|
||||
throw new java.lang.IndexOutOfBoundsException();
|
||||
|
||||
// Determine the byte ordering for this platform, and restore the int
|
||||
|
||||
int iVal = 0;
|
||||
|
||||
// Restore the int value from the byte array
|
||||
|
||||
int i1 = (int) bytarray[pos + 3] & 0xFF;
|
||||
int i2 = (int) bytarray[pos + 2] & 0xFF;
|
||||
int i3 = (int) bytarray[pos + 1] & 0xFF;
|
||||
int i4 = (int) bytarray[pos] & 0xFF;
|
||||
|
||||
iVal = (i1 << 24) + (i2 << 16) + (i3 << 8) + i4;
|
||||
|
||||
// Return the int value
|
||||
|
||||
return iVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack a 64-bit long.
|
||||
*
|
||||
* @param buf Byte buffer containing the integer to be unpacked.
|
||||
* @param pos Position within the buffer that the integer is stored.
|
||||
* @return The unpacked 64-bit long value.
|
||||
* @exception java.lang.IndexOutOfBoundsException If there is not enough data in the buffer.
|
||||
*/
|
||||
public final static long getLong(byte[] buf, int pos) throws java.lang.IndexOutOfBoundsException
|
||||
{
|
||||
|
||||
// Check if the byte array is long enough to restore the long
|
||||
|
||||
if (buf.length < pos + 7)
|
||||
throw new java.lang.IndexOutOfBoundsException();
|
||||
|
||||
// Restore the long value from the byte array
|
||||
|
||||
long lVal = 0L;
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
|
||||
// Get the current byte, shift the value and add to the return value
|
||||
|
||||
long curVal = (long) buf[pos + i] & 0xFF;
|
||||
curVal = curVal << ((7 - i) * 8);
|
||||
lVal += curVal;
|
||||
}
|
||||
|
||||
// Return the long value
|
||||
|
||||
return lVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack a 64-bit integer that is stored in Intel format.
|
||||
*
|
||||
* @param bytarray Byte array containing the Intel long to be unpacked.
|
||||
* @param pos Offset that the Intel integer is stored within the byte array.
|
||||
* @return Unpacked long value.
|
||||
* @exception java.lang.IndexOutOfBoundsException If there is not enough data in the buffer.
|
||||
*/
|
||||
public final static long getIntelLong(byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException
|
||||
{
|
||||
|
||||
// Check if the byte array is long enough to restore the long
|
||||
|
||||
if (bytarray.length < pos + 7)
|
||||
throw new java.lang.IndexOutOfBoundsException();
|
||||
|
||||
// Restore the long value from the byte array
|
||||
|
||||
long lVal = 0L;
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
|
||||
// Get the current byte, shift the value and add to the return value
|
||||
|
||||
long curVal = (long) bytarray[pos + i] & 0xFF;
|
||||
curVal = curVal << (i * 8);
|
||||
lVal += curVal;
|
||||
}
|
||||
|
||||
// Return the long value
|
||||
|
||||
return lVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack a 16-bit value that is stored in Intel format.
|
||||
*
|
||||
* @param bytarray Byte array containing the short value to be unpacked.
|
||||
* @param pos Offset to start unpacking the short value.
|
||||
* @return Unpacked short value.
|
||||
* @exception java.lang.IndexOutOfBoiundsException If there is not enough data in the buffer.
|
||||
*/
|
||||
public final static int getIntelShort(byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException
|
||||
{
|
||||
|
||||
// Check if the byte array is long enough to restore the int
|
||||
|
||||
if (bytarray.length < pos)
|
||||
throw new java.lang.IndexOutOfBoundsException();
|
||||
|
||||
// Restore the short value from the byte array
|
||||
|
||||
int sVal = (((int) bytarray[pos + 1] << 8) + ((int) bytarray[pos] & 0xFF));
|
||||
|
||||
// Return the short value
|
||||
|
||||
return sVal & 0xFFFF;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack a 16-bit value.
|
||||
*
|
||||
* @param bytarray Byte array containing the short to be unpacked.
|
||||
* @param pos Offset within the byte array that the short is stored.
|
||||
* @return Unpacked short value.
|
||||
* @exception java.lang.IndexOutOfBoundsException If there is not enough data in the buffer.
|
||||
*/
|
||||
public final static int getShort(byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException
|
||||
{
|
||||
|
||||
// Check if the byte array is long enough to restore the int
|
||||
|
||||
if (bytarray.length < pos)
|
||||
throw new java.lang.IndexOutOfBoundsException();
|
||||
|
||||
// Determine the byte ordering for this platform, and restore the short
|
||||
|
||||
int sVal = 0;
|
||||
|
||||
if (bigEndian == true)
|
||||
{
|
||||
|
||||
// Big endian
|
||||
|
||||
sVal = ((((int) bytarray[pos + 1]) << 8) + ((int) bytarray[pos] & 0xFF));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// Little endian
|
||||
|
||||
sVal = ((((int) bytarray[pos]) << 8) + ((int) bytarray[pos + 1] & 0xFF));
|
||||
}
|
||||
|
||||
// Return the short value
|
||||
|
||||
return sVal & 0xFFFF;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack a null terminated string from the data buffer.
|
||||
*
|
||||
* @param bytarray Byte array to unpack the string value from.
|
||||
* @param pos Offset to start unpacking the string value.
|
||||
* @param maxlen Maximum length of data to be searched for a null character.
|
||||
* @return String, else null if the terminating null character was not found.
|
||||
*/
|
||||
public final static String getString(byte[] bytarray, int pos, int maxlen)
|
||||
{
|
||||
|
||||
// Search for the trailing null
|
||||
|
||||
int maxpos = pos + maxlen;
|
||||
int endpos = pos;
|
||||
|
||||
while (bytarray[endpos] != 0x00 && endpos < maxpos)
|
||||
endpos++;
|
||||
|
||||
// Check if we reached the end of the buffer
|
||||
|
||||
if (endpos <= maxpos)
|
||||
return new String(bytarray, pos, endpos - pos);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack a null terminated string from the data buffer. The string may be ASCII or Unicode.
|
||||
*
|
||||
* @param bytarray Byte array to unpack the string value from.
|
||||
* @param pos Offset to start unpacking the string value.
|
||||
* @param maxlen Maximum length of data to be searched for a null character.
|
||||
* @param isUni Unicode string if true, else ASCII string
|
||||
* @return String, else null if the terminating null character was not found.
|
||||
*/
|
||||
public final static String getString(byte[] bytarray, int pos, int maxlen, boolean isUni)
|
||||
{
|
||||
|
||||
// Get a string from the buffer
|
||||
|
||||
String str = null;
|
||||
|
||||
if (isUni)
|
||||
str = getUnicodeString(bytarray, pos, maxlen);
|
||||
else
|
||||
str = getString(bytarray, pos, maxlen);
|
||||
|
||||
// return the string
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpack a null terminated Unicode string from the data buffer.
|
||||
*
|
||||
* @param byt Byte array to unpack the string value from.
|
||||
* @param pos Offset to start unpacking the string value.
|
||||
* @param maxlen Maximum length of data to be searched for a null character.
|
||||
* @return String, else null if the terminating null character was not found.
|
||||
*/
|
||||
public final static String getUnicodeString(byte[] byt, int pos, int maxlen)
|
||||
{
|
||||
|
||||
// Check for an empty string
|
||||
|
||||
if (maxlen == 0)
|
||||
return "";
|
||||
|
||||
// Search for the trailing null
|
||||
|
||||
int maxpos = pos + (maxlen * 2);
|
||||
int endpos = pos;
|
||||
char[] chars = new char[maxlen];
|
||||
int cpos = 0;
|
||||
char curChar;
|
||||
|
||||
do
|
||||
{
|
||||
|
||||
// Get a Unicode character from the buffer
|
||||
|
||||
curChar = (char) (((byt[endpos + 1] & 0xFF) << 8) + (byt[endpos] & 0xFF));
|
||||
|
||||
// Add the character to the array
|
||||
|
||||
chars[cpos++] = curChar;
|
||||
|
||||
// Update the buffer pointer
|
||||
|
||||
endpos += 2;
|
||||
|
||||
} while (curChar != 0 && endpos < maxpos);
|
||||
|
||||
// Check if we reached the end of the buffer
|
||||
|
||||
if (endpos <= maxpos)
|
||||
{
|
||||
if (curChar == 0)
|
||||
cpos--;
|
||||
return new String(chars, 0, cpos);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack a 32-bit integer into the supplied byte buffer.
|
||||
*
|
||||
* @param val Integer value to be packed.
|
||||
* @param bytarray Byte buffer to pack the integer value into.
|
||||
* @param pos Offset to start packing the integer value.
|
||||
* @exception java.lang.IndexOutOfBoundsException If the buffer does not have enough space.
|
||||
*/
|
||||
public final static void putInt(int val, byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException
|
||||
{
|
||||
|
||||
// Check if the byte array is long enough to store the int
|
||||
|
||||
if (bytarray.length < pos + 3)
|
||||
throw new java.lang.IndexOutOfBoundsException();
|
||||
|
||||
// Pack the integer value
|
||||
|
||||
bytarray[pos] = (byte) ((val >> 24) & 0xFF);
|
||||
bytarray[pos + 1] = (byte) ((val >> 16) & 0xFF);
|
||||
bytarray[pos + 2] = (byte) ((val >> 8) & 0xFF);
|
||||
bytarray[pos + 3] = (byte) (val & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack an 32-bit integer value in Intel format.
|
||||
*
|
||||
* @param val Integer value to be packed.
|
||||
* @param bytarray Byte array to pack the value into.
|
||||
* @param pos Offset to start packing the integer value.
|
||||
* @exception java.lang.IndexOutOfBoundsException If there is not enough data in the buffer.
|
||||
*/
|
||||
public final static void putIntelInt(int val, byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException
|
||||
{
|
||||
|
||||
// Check if the byte array is long enough to store the int
|
||||
|
||||
if (bytarray.length < pos + 3)
|
||||
throw new java.lang.IndexOutOfBoundsException();
|
||||
|
||||
// Store the int value in the byte array
|
||||
|
||||
bytarray[pos + 3] = (byte) ((val >> 24) & 0xFF);
|
||||
bytarray[pos + 2] = (byte) ((val >> 16) & 0xFF);
|
||||
bytarray[pos + 1] = (byte) ((val >> 8) & 0xFF);
|
||||
bytarray[pos] = (byte) (val & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack a 64-bit integer value into the buffer
|
||||
*
|
||||
* @param val Integer value to be packed.
|
||||
* @param bytarray Byte array to pack the value into.
|
||||
* @param pos Offset to start packing the integer value.
|
||||
* @exception java.lang.IndexOutOfBoundsException If there is not enough data in the buffer.
|
||||
*/
|
||||
public final static void putLong(long val, byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException
|
||||
{
|
||||
|
||||
// Check if the byte array is long enough to store the int
|
||||
|
||||
if (bytarray.length < pos + 7)
|
||||
throw new java.lang.IndexOutOfBoundsException();
|
||||
|
||||
// Store the long value in the byte array
|
||||
|
||||
bytarray[pos] = (byte) ((val >> 56) & 0xFF);
|
||||
bytarray[pos + 1] = (byte) ((val >> 48) & 0xFF);
|
||||
bytarray[pos + 2] = (byte) ((val >> 40) & 0xFF);
|
||||
bytarray[pos + 3] = (byte) ((val >> 32) & 0xFF);
|
||||
bytarray[pos + 4] = (byte) ((val >> 24) & 0xFF);
|
||||
bytarray[pos + 5] = (byte) ((val >> 16) & 0xFF);
|
||||
bytarray[pos + 6] = (byte) ((val >> 8) & 0xFF);
|
||||
bytarray[pos + 7] = (byte) (val & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack a 64-bit integer value in Intel format.
|
||||
*
|
||||
* @param val Integer value to be packed.
|
||||
* @param bytarray Byte array to pack the value into.
|
||||
* @param pos Offset to start packing the integer value.
|
||||
* @exception java.lang.IndexOutOfBoundsException If there is not enough data in the buffer.
|
||||
*/
|
||||
public final static void putIntelLong(long val, byte[] bytarray, int pos)
|
||||
throws java.lang.IndexOutOfBoundsException
|
||||
{
|
||||
|
||||
// Check if the byte array is long enough to store the int
|
||||
|
||||
if (bytarray.length < pos + 7)
|
||||
throw new java.lang.IndexOutOfBoundsException();
|
||||
|
||||
// Store the long value in the byte array
|
||||
|
||||
bytarray[pos + 7] = (byte) ((val >> 56) & 0xFF);
|
||||
bytarray[pos + 6] = (byte) ((val >> 48) & 0xFF);
|
||||
bytarray[pos + 5] = (byte) ((val >> 40) & 0xFF);
|
||||
bytarray[pos + 4] = (byte) ((val >> 32) & 0xFF);
|
||||
bytarray[pos + 3] = (byte) ((val >> 24) & 0xFF);
|
||||
bytarray[pos + 2] = (byte) ((val >> 16) & 0xFF);
|
||||
bytarray[pos + 1] = (byte) ((val >> 8) & 0xFF);
|
||||
bytarray[pos] = (byte) (val & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack a 64-bit integer value in Intel format.
|
||||
*
|
||||
* @param val Integer value to be packed.
|
||||
* @param bytarray Byte array to pack the value into.
|
||||
* @param pos Offset to start packing the integer value.
|
||||
* @exception java.lang.IndexOutOfBoundsException If there is not enough data in the buffer.
|
||||
*/
|
||||
public final static void putIntelLong(int val, byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException
|
||||
{
|
||||
|
||||
// Check if the byte array is long enough to store the int
|
||||
|
||||
if (bytarray.length < pos + 7)
|
||||
throw new java.lang.IndexOutOfBoundsException();
|
||||
|
||||
// Store the int value in the byte array
|
||||
|
||||
bytarray[pos + 7] = (byte) 0;
|
||||
bytarray[pos + 6] = (byte) 0;
|
||||
bytarray[pos + 5] = (byte) 0;
|
||||
bytarray[pos + 4] = (byte) 0;
|
||||
bytarray[pos + 3] = (byte) ((val >> 24) & 0xFF);
|
||||
bytarray[pos + 2] = (byte) ((val >> 16) & 0xFF);
|
||||
bytarray[pos + 1] = (byte) ((val >> 8) & 0xFF);
|
||||
bytarray[pos] = (byte) (val & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack a 16 bit value in Intel byte order.
|
||||
*
|
||||
* @param val Short value to be packed.
|
||||
* @param bytarray Byte array to pack the short value into.
|
||||
* @param pos Offset to start packing the short value.
|
||||
* @exception java.lang.IndexOutOfBoundsException If there is not enough data in the buffer.
|
||||
*/
|
||||
public final static void putIntelShort(int val, byte[] bytarray, int pos)
|
||||
throws java.lang.IndexOutOfBoundsException
|
||||
{
|
||||
|
||||
// Check if the byte array is long enough to store the short
|
||||
|
||||
if (bytarray.length < pos)
|
||||
throw new java.lang.IndexOutOfBoundsException();
|
||||
|
||||
// Pack the short value
|
||||
|
||||
bytarray[pos + 1] = (byte) ((val >> 8) & 0xFF);
|
||||
bytarray[pos] = (byte) (val & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack a 16-bit value into the supplied byte buffer.
|
||||
*
|
||||
* @param val Short value to be packed.
|
||||
* @param bytarray Byte array to pack the short value into.
|
||||
* @param pos Offset to start packing the short value.
|
||||
* @exception java.lang.IndexOutOfBoundsException If there is not enough data in the buffer.
|
||||
*/
|
||||
public final static void putShort(int val, byte[] bytarray, int pos) throws java.lang.IndexOutOfBoundsException
|
||||
{
|
||||
|
||||
// Check if the byte array is long enough to store the short
|
||||
|
||||
if (bytarray.length < pos)
|
||||
throw new java.lang.IndexOutOfBoundsException();
|
||||
|
||||
// Pack the short value
|
||||
|
||||
bytarray[pos] = (byte) ((val >> 8) & 0xFF);
|
||||
bytarray[pos + 1] = (byte) (val & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack a string into a data buffer
|
||||
*
|
||||
* @param str String to be packed into the buffer
|
||||
* @param bytarray Byte array to pack the string into
|
||||
* @param pos Position to start packing the string
|
||||
* @param nullterm true if the string should be null terminated, else false
|
||||
* @return The ending buffer position
|
||||
*/
|
||||
public final static int putString(String str, byte[] bytarray, int pos, boolean nullterm)
|
||||
{
|
||||
|
||||
// Get the string as a byte array
|
||||
|
||||
byte[] byts = str.getBytes();
|
||||
|
||||
// Pack the data bytes
|
||||
|
||||
int bufpos = pos;
|
||||
|
||||
for (int i = 0; i < byts.length; i++)
|
||||
bytarray[bufpos++] = byts[i];
|
||||
|
||||
// Null terminate the string, if required
|
||||
|
||||
if (nullterm == true)
|
||||
bytarray[bufpos++] = 0;
|
||||
|
||||
// Return the next free buffer position
|
||||
|
||||
return bufpos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack a string into a data buffer
|
||||
*
|
||||
* @param str String to be packed into the buffer
|
||||
* @param fldLen Field length, will be space padded if short
|
||||
* @param bytarray Byte array to pack the string into
|
||||
* @param pos Position to start packing the string
|
||||
* @return The ending buffer position
|
||||
*/
|
||||
public final static int putString(String str, int fldLen, byte[] bytarray, int pos)
|
||||
{
|
||||
|
||||
// Get the string as a byte array
|
||||
|
||||
byte[] byts = str.getBytes();
|
||||
|
||||
// Pack the data bytes
|
||||
|
||||
int bufpos = pos;
|
||||
int idx = 0;
|
||||
|
||||
while (idx < fldLen)
|
||||
{
|
||||
if (idx < byts.length)
|
||||
bytarray[bufpos++] = byts[idx];
|
||||
else
|
||||
bytarray[bufpos++] = (byte) 0;
|
||||
idx++;
|
||||
}
|
||||
|
||||
// Return the next free buffer position
|
||||
|
||||
return bufpos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack a string into a data buffer. The string may be ASCII or Unicode.
|
||||
*
|
||||
* @param str String to be packed into the buffer
|
||||
* @param bytarray Byte array to pack the string into
|
||||
* @param pos Position to start packing the string
|
||||
* @param nullterm true if the string should be null terminated, else false
|
||||
* @param isUni true if the string should be packed as Unicode, false to pack as ASCII
|
||||
* @return The ending buffer position
|
||||
*/
|
||||
public final static int putString(String str, byte[] bytarray, int pos, boolean nullterm, boolean isUni)
|
||||
{
|
||||
|
||||
// Pack the string
|
||||
|
||||
int newpos = -1;
|
||||
|
||||
if (isUni)
|
||||
newpos = putUnicodeString(str, bytarray, pos, nullterm);
|
||||
else
|
||||
newpos = putString(str, bytarray, pos, nullterm);
|
||||
|
||||
// Return the end of string buffer position
|
||||
|
||||
return newpos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack a Unicode string into a data buffer
|
||||
*
|
||||
* @param str String to be packed into the buffer
|
||||
* @param bytarray Byte array to pack the string into
|
||||
* @param pos Position to start packing the string
|
||||
* @param nullterm true if the string should be null terminated, else false
|
||||
* @return The ending buffer position
|
||||
*/
|
||||
public final static int putUnicodeString(String str, byte[] bytarray, int pos, boolean nullterm)
|
||||
{
|
||||
|
||||
// Pack the data bytes
|
||||
|
||||
int bufpos = pos;
|
||||
|
||||
for (int i = 0; i < str.length(); i++)
|
||||
{
|
||||
|
||||
// Get the current character from the string
|
||||
|
||||
char ch = str.charAt(i);
|
||||
|
||||
// Pack the unicode character
|
||||
|
||||
bytarray[bufpos++] = (byte) (ch & 0xFF);
|
||||
bytarray[bufpos++] = (byte) ((ch & 0xFF00) >> 8);
|
||||
}
|
||||
|
||||
// Null terminate the string, if required
|
||||
|
||||
if (nullterm == true)
|
||||
{
|
||||
bytarray[bufpos++] = 0;
|
||||
bytarray[bufpos++] = 0;
|
||||
}
|
||||
|
||||
// Return the next free buffer position
|
||||
|
||||
return bufpos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack nulls into the buffer.
|
||||
*
|
||||
* @param buf Buffer to pack data into.
|
||||
* @param pos Position to start packing.
|
||||
* @param cnt Number of nulls to pack.
|
||||
* @exception java.lang.ArrayIndexOutOfBoundsException If the buffer does not have enough space.
|
||||
*/
|
||||
public final static void putZeros(byte[] buf, int pos, int cnt) throws java.lang.ArrayIndexOutOfBoundsException
|
||||
{
|
||||
|
||||
// Check if the buffer is big enough
|
||||
|
||||
if (buf.length < (pos + cnt))
|
||||
throw new java.lang.ArrayIndexOutOfBoundsException();
|
||||
|
||||
// Pack the nulls
|
||||
|
||||
for (int i = 0; i < cnt; i++)
|
||||
buf[pos + i] = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Align a buffer offset on a word boundary
|
||||
*
|
||||
* @param pos int
|
||||
* @return int
|
||||
*/
|
||||
public final static int wordAlign(int pos)
|
||||
{
|
||||
return (pos + 1) & 0xFFFFFFFE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Align a buffer offset on a longword boundary
|
||||
*
|
||||
* @param pos int
|
||||
* @return int
|
||||
*/
|
||||
public final static int longwordAlign(int pos)
|
||||
{
|
||||
return (pos + 3) & 0xFFFFFFFC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the string length in bytes
|
||||
*
|
||||
* @param str String
|
||||
* @param uni boolean
|
||||
* @param nul boolean
|
||||
* @return int
|
||||
*/
|
||||
public final static int getStringLength(String str, boolean uni, boolean nul)
|
||||
{
|
||||
|
||||
// Calculate the string length in bytes
|
||||
|
||||
int len = str.length();
|
||||
if (nul)
|
||||
len += 1;
|
||||
if (uni)
|
||||
len *= 2;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the new buffer position after the specified string and encoding (ASCII or Unicode)
|
||||
*
|
||||
* @param pos int
|
||||
* @param str String
|
||||
* @param uni boolean
|
||||
* @param nul boolean
|
||||
* @return int
|
||||
*/
|
||||
public final static int getBufferPosition(int pos, String str, boolean uni, boolean nul)
|
||||
{
|
||||
|
||||
// Calculate the new buffer position
|
||||
|
||||
int len = str.length();
|
||||
if (nul)
|
||||
len += 1;
|
||||
if (uni)
|
||||
len *= 2;
|
||||
|
||||
return pos + len;
|
||||
}
|
||||
}
|
259
source/java/org/alfresco/filesys/util/HexDump.java
Normal file
259
source/java/org/alfresco/filesys/util/HexDump.java
Normal file
@@ -0,0 +1,259 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.alfresco.filesys.util;
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
/**
|
||||
* Hex dump class.
|
||||
*/
|
||||
public final class HexDump
|
||||
{
|
||||
|
||||
/**
|
||||
* Hex dump a byte array
|
||||
*
|
||||
* @param byt Byte array to dump
|
||||
* @param len Length of data to dump
|
||||
* @param offset Offset to start data dump
|
||||
*/
|
||||
|
||||
public static final void Dump(byte[] byt, int len, int offset)
|
||||
{
|
||||
Dump(byt, len, offset, System.out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hex dump a byte array
|
||||
*
|
||||
* @param byt Byte array to dump
|
||||
* @param len Length of data to dump
|
||||
* @param offset Offset to start data dump
|
||||
* @param stream Output stream to dump the output to.
|
||||
*/
|
||||
|
||||
public static final void Dump(byte[] byt, int len, int offset, PrintStream stream)
|
||||
{
|
||||
|
||||
// Create buffers for the ASCII and Hex output
|
||||
|
||||
StringBuffer ascBuf = new StringBuffer();
|
||||
StringBuffer hexBuf = new StringBuffer();
|
||||
|
||||
// Dump 16 byte blocks from the array until the length has been
|
||||
// reached
|
||||
|
||||
int dlen = 0;
|
||||
int doff = offset;
|
||||
String posStr = null;
|
||||
|
||||
while (dlen < len)
|
||||
{
|
||||
|
||||
// Reset the ASCII/Hex buffers
|
||||
|
||||
ascBuf.setLength(0);
|
||||
hexBuf.setLength(0);
|
||||
|
||||
posStr = generatePositionString(doff);
|
||||
|
||||
// Dump a block of data, update the data offset
|
||||
|
||||
doff = generateLine(byt, doff, ascBuf, hexBuf);
|
||||
|
||||
// Output the current record
|
||||
|
||||
stream.print(posStr);
|
||||
stream.print(hexBuf.toString());
|
||||
stream.println(ascBuf.toString());
|
||||
|
||||
// Update the dump length
|
||||
|
||||
dlen += 16;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a hex string for the specified string
|
||||
*
|
||||
* @param str String
|
||||
* @return String
|
||||
*/
|
||||
public static final String hexString(String str)
|
||||
{
|
||||
if (str != null)
|
||||
return hexString(str.getBytes());
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a hex string for the specified string
|
||||
*
|
||||
* @param str String
|
||||
* @param gap String
|
||||
* @return String
|
||||
*/
|
||||
public static final String hexString(String str, String gap)
|
||||
{
|
||||
if (str != null)
|
||||
return hexString(str.getBytes(), gap);
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a hex string for the specified bytes
|
||||
*
|
||||
* @param buf byte[]
|
||||
* @return String
|
||||
*/
|
||||
public static final String hexString(byte[] buf)
|
||||
{
|
||||
return hexString(buf, buf.length, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a hex string for the specified bytes
|
||||
*
|
||||
* @param buf byte[]
|
||||
* @param gap String
|
||||
* @return String
|
||||
*/
|
||||
public static final String hexString(byte[] buf, String gap)
|
||||
{
|
||||
return hexString(buf, buf.length, gap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a hex string for the specified bytes
|
||||
*
|
||||
* @param buf byte[]
|
||||
* @param len int
|
||||
* @param gap String
|
||||
* @return String
|
||||
*/
|
||||
public static final String hexString(byte[] buf, int len, String gap)
|
||||
{
|
||||
|
||||
// Check if the buffer is valid
|
||||
|
||||
if (buf == null)
|
||||
return "";
|
||||
|
||||
// Create a string buffer for the hex string
|
||||
|
||||
int buflen = buf.length * 2;
|
||||
if (gap != null)
|
||||
buflen += buf.length * gap.length();
|
||||
|
||||
StringBuffer hex = new StringBuffer(buflen);
|
||||
|
||||
// Convert the bytes to hex-ASCII
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
|
||||
// Get the current byte
|
||||
|
||||
int curbyt = (int) (buf[i] & 0x00FF);
|
||||
|
||||
// Output the hex string
|
||||
|
||||
hex.append(Integer.toHexString((curbyt & 0xF0) >> 4));
|
||||
hex.append(Integer.toHexString(curbyt & 0x0F));
|
||||
|
||||
// Add the gap string, if specified
|
||||
|
||||
if (gap != null && i < (len - 1))
|
||||
hex.append(gap);
|
||||
}
|
||||
|
||||
// Return the hex-ASCII string
|
||||
|
||||
return hex.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a buffer position string
|
||||
*
|
||||
* @param off int
|
||||
* @return String
|
||||
*/
|
||||
private static final String generatePositionString(int off)
|
||||
{
|
||||
|
||||
// Create a buffer position string
|
||||
|
||||
StringBuffer posStr = new StringBuffer("" + off + " - ");
|
||||
while (posStr.length() < 8)
|
||||
posStr.insert(0, " ");
|
||||
|
||||
// Return the string
|
||||
|
||||
return posStr.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Output a single line of the hex dump to a debug device
|
||||
*
|
||||
* @param byt Byte array to dump
|
||||
* @param off Offset to start data dump
|
||||
* @param ascBuf Buffer for ASCII output
|
||||
* @param hexBuf Buffer for Hex output
|
||||
* @return New offset value
|
||||
*/
|
||||
|
||||
private static final int generateLine(byte[] byt, int off, StringBuffer ascBuf, StringBuffer hexBuf)
|
||||
{
|
||||
|
||||
// Check if there is enough buffer space to dump 16 bytes
|
||||
|
||||
int dumplen = byt.length - off;
|
||||
if (dumplen > 16)
|
||||
dumplen = 16;
|
||||
|
||||
// Dump a 16 byte block of data
|
||||
|
||||
for (int i = 0; i < dumplen; i++)
|
||||
{
|
||||
|
||||
// Get the current byte
|
||||
|
||||
int curbyt = (int) (byt[off++] & 0x00FF);
|
||||
|
||||
// Output the hex string
|
||||
|
||||
hexBuf.append(Integer.toHexString((curbyt & 0xF0) >> 4));
|
||||
hexBuf.append(Integer.toHexString(curbyt & 0x0F));
|
||||
hexBuf.append(" ");
|
||||
|
||||
// Output the character equivalent, if printable
|
||||
|
||||
if (Character.isLetterOrDigit((char) curbyt) || Character.getType((char) curbyt) != Character.CONTROL)
|
||||
ascBuf.append((char) curbyt);
|
||||
else
|
||||
ascBuf.append(".");
|
||||
}
|
||||
|
||||
// Output the hex dump line
|
||||
|
||||
hexBuf.append(" - ");
|
||||
|
||||
// Return the new data offset
|
||||
|
||||
return off;
|
||||
}
|
||||
}
|
206
source/java/org/alfresco/filesys/util/IPAddress.java
Normal file
206
source/java/org/alfresco/filesys/util/IPAddress.java
Normal file
@@ -0,0 +1,206 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.alfresco.filesys.util;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
/**
|
||||
* TCP/IP Address Utility Class
|
||||
*/
|
||||
public class IPAddress
|
||||
{
|
||||
|
||||
/**
|
||||
* Check if the specified address is a valid numeric TCP/IP address
|
||||
*
|
||||
* @param ipaddr String
|
||||
* @return boolean
|
||||
*/
|
||||
public final static boolean isNumericAddress(String ipaddr)
|
||||
{
|
||||
|
||||
// Check if the string is valid
|
||||
|
||||
if (ipaddr == null || ipaddr.length() < 7 || ipaddr.length() > 15)
|
||||
return false;
|
||||
|
||||
// Check the address string, should be n.n.n.n format
|
||||
|
||||
StringTokenizer token = new StringTokenizer(ipaddr, ".");
|
||||
if (token.countTokens() != 4)
|
||||
return false;
|
||||
|
||||
while (token.hasMoreTokens())
|
||||
{
|
||||
|
||||
// Get the current token and convert to an integer value
|
||||
|
||||
String ipNum = token.nextToken();
|
||||
|
||||
try
|
||||
{
|
||||
int ipVal = Integer.valueOf(ipNum).intValue();
|
||||
if (ipVal < 0 || ipVal > 255)
|
||||
return false;
|
||||
}
|
||||
catch (NumberFormatException ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Looks like a valid IP address
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the specified address is a valid numeric TCP/IP address and return as an integer
|
||||
* value
|
||||
*
|
||||
* @param ipaddr String
|
||||
* @return int
|
||||
*/
|
||||
public final static int parseNumericAddress(String ipaddr)
|
||||
{
|
||||
|
||||
// Check if the string is valid
|
||||
|
||||
if (ipaddr == null || ipaddr.length() < 7 || ipaddr.length() > 15)
|
||||
return 0;
|
||||
|
||||
// Check the address string, should be n.n.n.n format
|
||||
|
||||
StringTokenizer token = new StringTokenizer(ipaddr, ".");
|
||||
if (token.countTokens() != 4)
|
||||
return 0;
|
||||
|
||||
int ipInt = 0;
|
||||
|
||||
while (token.hasMoreTokens())
|
||||
{
|
||||
|
||||
// Get the current token and convert to an integer value
|
||||
|
||||
String ipNum = token.nextToken();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
// Validate the current address part
|
||||
|
||||
int ipVal = Integer.valueOf(ipNum).intValue();
|
||||
if (ipVal < 0 || ipVal > 255)
|
||||
return 0;
|
||||
|
||||
// Add to the integer address
|
||||
|
||||
ipInt = (ipInt << 8) + ipVal;
|
||||
}
|
||||
catch (NumberFormatException ex)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the integer address
|
||||
|
||||
return ipInt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an IP address into an integer value
|
||||
*
|
||||
* @param ipaddr InetAddress
|
||||
* @return int
|
||||
*/
|
||||
public final static int asInteger(InetAddress ipaddr)
|
||||
{
|
||||
|
||||
// Get the address as an array of bytes
|
||||
|
||||
byte[] addrBytes = ipaddr.getAddress();
|
||||
|
||||
// Build an integer value from the bytes
|
||||
|
||||
return DataPacker.getInt(addrBytes, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the specified address is within the required subnet
|
||||
*
|
||||
* @param ipaddr String
|
||||
* @param subnet String
|
||||
* @param mask String
|
||||
* @return boolean
|
||||
*/
|
||||
public final static boolean isInSubnet(String ipaddr, String subnet, String mask)
|
||||
{
|
||||
|
||||
// Convert the addresses to integer values
|
||||
|
||||
int ipaddrInt = parseNumericAddress(ipaddr);
|
||||
if (ipaddrInt == 0)
|
||||
return false;
|
||||
|
||||
int subnetInt = parseNumericAddress(subnet);
|
||||
if (subnetInt == 0)
|
||||
return false;
|
||||
|
||||
int maskInt = parseNumericAddress(mask);
|
||||
if (maskInt == 0)
|
||||
return false;
|
||||
|
||||
// Check if the address is part of the subnet
|
||||
|
||||
if ((ipaddrInt & maskInt) == subnetInt)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a raw IP address array as a String
|
||||
*
|
||||
* @param ipaddr byte[]
|
||||
* @return String
|
||||
*/
|
||||
public final static String asString(byte[] ipaddr)
|
||||
{
|
||||
|
||||
// Check if the address is valid
|
||||
|
||||
if (ipaddr == null || ipaddr.length != 4)
|
||||
return null;
|
||||
|
||||
// Convert the raw IP address to a string
|
||||
|
||||
StringBuffer str = new StringBuffer();
|
||||
|
||||
str.append((int) (ipaddr[0] & 0xFF));
|
||||
str.append(".");
|
||||
str.append((int) (ipaddr[1] & 0xFF));
|
||||
str.append(".");
|
||||
str.append((int) (ipaddr[2] & 0xFF));
|
||||
str.append(".");
|
||||
str.append((int) (ipaddr[3] & 0xFF));
|
||||
|
||||
// Return the address string
|
||||
|
||||
return str.toString();
|
||||
}
|
||||
}
|
214
source/java/org/alfresco/filesys/util/MemorySize.java
Normal file
214
source/java/org/alfresco/filesys/util/MemorySize.java
Normal file
@@ -0,0 +1,214 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.alfresco.filesys.util;
|
||||
|
||||
/**
|
||||
* Memory Size Class
|
||||
* <p>
|
||||
* Convenience class to convert memory size value specified as 'nK' for kilobytes, 'nM' for
|
||||
* megabytes and 'nG' for gigabytes, to an absolute value.
|
||||
*/
|
||||
public class MemorySize
|
||||
{
|
||||
// Convertor constants
|
||||
|
||||
public static final long KILOBYTE = 1024L;
|
||||
public static final long MEGABYTE = 1024L * KILOBYTE;
|
||||
public static final long GIGABYTE = 1024L * MEGABYTE;
|
||||
public static final long TERABYTE = 1024L * GIGABYTE;
|
||||
|
||||
/**
|
||||
* Convert a memory size to an integer byte value.
|
||||
*
|
||||
* @param memSize String
|
||||
* @return int
|
||||
* @exception NumberFormatException
|
||||
*/
|
||||
public static final int getByteValueInt(String memSize)
|
||||
{
|
||||
return (int) (getByteValue(memSize) & 0xFFFFFFFFL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a memory size to a byte value
|
||||
*
|
||||
* @param memSize String
|
||||
* @return long
|
||||
* @exception NumberFormatException
|
||||
*/
|
||||
public static final long getByteValue(String memSize)
|
||||
{
|
||||
|
||||
// Check if the string is valid
|
||||
|
||||
if (memSize == null || memSize.length() == 0)
|
||||
return -1L;
|
||||
|
||||
// Check for a kilobyte value
|
||||
|
||||
String sizeStr = memSize.toUpperCase();
|
||||
long mult = 1;
|
||||
long val = 0;
|
||||
|
||||
if (sizeStr.endsWith("K"))
|
||||
{
|
||||
|
||||
// Use the kilobyte multiplier
|
||||
|
||||
mult = KILOBYTE;
|
||||
val = getValue(sizeStr);
|
||||
}
|
||||
else if (sizeStr.endsWith("M"))
|
||||
{
|
||||
|
||||
// Use the megabyte nultiplier
|
||||
|
||||
mult = MEGABYTE;
|
||||
val = getValue(sizeStr);
|
||||
}
|
||||
else if (sizeStr.endsWith("G"))
|
||||
{
|
||||
|
||||
// Use the gigabyte multiplier
|
||||
|
||||
mult = GIGABYTE;
|
||||
val = getValue(sizeStr);
|
||||
}
|
||||
else if (sizeStr.endsWith("T"))
|
||||
{
|
||||
|
||||
// Use the terabyte multiplier
|
||||
|
||||
mult = TERABYTE;
|
||||
val = getValue(sizeStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// Convert a numeric byte value
|
||||
|
||||
val = Long.valueOf(sizeStr).longValue();
|
||||
}
|
||||
|
||||
// Apply the multiplier
|
||||
|
||||
return val * mult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the size value from a string and return the numeric value
|
||||
*
|
||||
* @param val String
|
||||
* @return long
|
||||
* @exception NumberFormatException
|
||||
*/
|
||||
private final static long getValue(String val)
|
||||
{
|
||||
|
||||
// Strip the trailing size indicator
|
||||
|
||||
String sizStr = val.substring(0, val.length() - 1);
|
||||
return Long.valueOf(sizStr).longValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a byte value as a kilobyte string
|
||||
*
|
||||
* @param val long
|
||||
* @return String
|
||||
*/
|
||||
public final static String asKilobyteString(long val)
|
||||
{
|
||||
|
||||
// Calculate the kilobyte value
|
||||
|
||||
long mbVal = val / KILOBYTE;
|
||||
return "" + mbVal + "Kb";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a byte value as a megabyte string
|
||||
*
|
||||
* @param val long
|
||||
* @return String
|
||||
*/
|
||||
public final static String asMegabyteString(long val)
|
||||
{
|
||||
|
||||
// Calculate the megabyte value
|
||||
|
||||
long mbVal = val / MEGABYTE;
|
||||
return "" + mbVal + "Mb";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a byte value as a gigabyte string
|
||||
*
|
||||
* @param val long
|
||||
* @return String
|
||||
*/
|
||||
public final static String asGigabyteString(long val)
|
||||
{
|
||||
|
||||
// Calculate the gigabyte value
|
||||
|
||||
long mbVal = val / GIGABYTE;
|
||||
return "" + mbVal + "Gb";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a byte value as a terabyte string
|
||||
*
|
||||
* @param val long
|
||||
* @return String
|
||||
*/
|
||||
public final static String asTerabyteString(long val)
|
||||
{
|
||||
|
||||
// Calculate the terabyte value
|
||||
|
||||
long mbVal = val / TERABYTE;
|
||||
return "" + mbVal + "Tb";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a byte value as a scaled string
|
||||
*
|
||||
* @param val long
|
||||
* @return String
|
||||
*/
|
||||
public final static String asScaledString(long val)
|
||||
{
|
||||
|
||||
// Determine the scaling to apply
|
||||
|
||||
String ret = null;
|
||||
|
||||
if (val < (KILOBYTE * 2L))
|
||||
ret = Long.toString(val);
|
||||
else if (val < (MEGABYTE * 2L))
|
||||
ret = asKilobyteString(val);
|
||||
else if (val < (GIGABYTE * 2L))
|
||||
ret = asMegabyteString(val);
|
||||
else if (val < (TERABYTE * 2L))
|
||||
ret = asGigabyteString(val);
|
||||
else
|
||||
ret = asTerabyteString(val);
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
172
source/java/org/alfresco/filesys/util/StringList.java
Normal file
172
source/java/org/alfresco/filesys/util/StringList.java
Normal file
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.alfresco.filesys.util;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
* String List Class
|
||||
*/
|
||||
public class StringList
|
||||
{
|
||||
|
||||
// List of strings
|
||||
|
||||
private Vector<String> m_list;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*/
|
||||
public StringList()
|
||||
{
|
||||
m_list = new Vector<String>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of strings in the list
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int numberOfStrings()
|
||||
{
|
||||
return m_list.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a string to the list
|
||||
*
|
||||
* @param str String
|
||||
*/
|
||||
public final void addString(String str)
|
||||
{
|
||||
m_list.add(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a list of strings to this list
|
||||
*
|
||||
* @param list StringList
|
||||
*/
|
||||
public final void addStrings(StringList list)
|
||||
{
|
||||
if (list != null && list.numberOfStrings() > 0)
|
||||
for (int i = 0; i < list.numberOfStrings(); m_list.add(list.getStringAt(i++)))
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the string at the specified index
|
||||
*
|
||||
* @param idx int
|
||||
* @return String
|
||||
*/
|
||||
public final String getStringAt(int idx)
|
||||
{
|
||||
if (idx < 0 || idx >= m_list.size())
|
||||
return null;
|
||||
return (String) m_list.elementAt(idx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the list contains the specified string
|
||||
*
|
||||
* @param str String
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean containsString(String str)
|
||||
{
|
||||
return m_list.contains(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the index of the specified string, or -1 if not in the list
|
||||
*
|
||||
* @param str String
|
||||
* @return int
|
||||
*/
|
||||
public final int findString(String str)
|
||||
{
|
||||
return m_list.indexOf(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified string from the list
|
||||
*
|
||||
* @param str String
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean removeString(String str)
|
||||
{
|
||||
return m_list.removeElement(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the string at the specified index within the list
|
||||
*
|
||||
* @param idx int
|
||||
* @return String
|
||||
*/
|
||||
public final String removeStringAt(int idx)
|
||||
{
|
||||
if (idx < 0 || idx >= m_list.size())
|
||||
return null;
|
||||
String ret = (String) m_list.elementAt(idx);
|
||||
m_list.removeElementAt(idx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the strings from the list
|
||||
*/
|
||||
public final void remoteAllStrings()
|
||||
{
|
||||
m_list.removeAllElements();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the string list as a string
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
|
||||
// Check if the list is empty
|
||||
|
||||
if (numberOfStrings() == 0)
|
||||
return "";
|
||||
|
||||
// Build the string
|
||||
|
||||
StringBuffer str = new StringBuffer();
|
||||
|
||||
for (int i = 0; i < m_list.size(); i++)
|
||||
{
|
||||
str.append(getStringAt(i));
|
||||
str.append(",");
|
||||
}
|
||||
|
||||
// Remove the trailing comma
|
||||
|
||||
if (str.length() > 0)
|
||||
str.setLength(str.length() - 1);
|
||||
|
||||
// Return the string
|
||||
|
||||
return str.toString();
|
||||
}
|
||||
}
|
640
source/java/org/alfresco/filesys/util/WildCard.java
Normal file
640
source/java/org/alfresco/filesys/util/WildCard.java
Normal file
@@ -0,0 +1,640 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package org.alfresco.filesys.util;
|
||||
|
||||
/**
|
||||
* Wildcard Utility Class.
|
||||
* <p>
|
||||
* The WildCard class may be used to check Strings against a wildcard pattern using the SMB/CIFS
|
||||
* wildcard rules.
|
||||
* <p>
|
||||
* A number of static convenience methods are also provided.
|
||||
*
|
||||
* @author GKSpencer
|
||||
*/
|
||||
public final class WildCard
|
||||
{
|
||||
|
||||
// Multiple character wildcard
|
||||
|
||||
public static final int MULTICHAR_WILDCARD = '*';
|
||||
|
||||
// Single character wildcard
|
||||
|
||||
public static final int SINGLECHAR_WILDCARD = '?';
|
||||
|
||||
// Unicode wildcards
|
||||
//
|
||||
// The spec states :-
|
||||
// translate '?' to '>'
|
||||
// translate '.' to '"' if followed by a '?' or '*'
|
||||
// translate '*' to '<' if followed by a '.'
|
||||
|
||||
public static final int SINGLECHAR_UNICODE_WILDCARD = '>';
|
||||
public static final int DOT_UNICODE_WILDCARD = '"';
|
||||
public static final int MULTICHAR_UNICODE_WILDCARD = '<';
|
||||
|
||||
// Wildcard types
|
||||
|
||||
public static final int WILDCARD_NONE = 0; // no wildcard characters present in pattern
|
||||
public static final int WILDCARD_ALL = 1; // '*.*' and '*'
|
||||
public static final int WILDCARD_NAME = 2; // '*.ext'
|
||||
public static final int WILDCARD_EXT = 3; // 'name.*'
|
||||
public static final int WILDCARD_COMPLEX = 4; // complex wildcard
|
||||
|
||||
public static final int WILDCARD_INVALID = -1;
|
||||
|
||||
// Wildcard pattern and type
|
||||
|
||||
private String m_pattern;
|
||||
private int m_type;
|
||||
|
||||
// Start/end string to match for name/extension matching
|
||||
|
||||
private String m_matchPart;
|
||||
private boolean m_caseSensitive;
|
||||
|
||||
// Complex wildcard pattern
|
||||
|
||||
private char[] m_patternChars;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*/
|
||||
public WildCard()
|
||||
{
|
||||
setType(WILDCARD_INVALID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param pattern String
|
||||
* @param caseSensitive boolean
|
||||
*/
|
||||
public WildCard(String pattern, boolean caseSensitive)
|
||||
{
|
||||
setPattern(pattern, caseSensitive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the wildcard pattern type
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int isType()
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if case sensitive matching is enabled
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean isCaseSensitive()
|
||||
{
|
||||
return m_caseSensitive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the wildcard pattern string
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public final String getPattern()
|
||||
{
|
||||
return m_pattern;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the match part for wildcard name and wildcard extension type patterns
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public final String getMatchPart()
|
||||
{
|
||||
return m_matchPart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the string matches the wildcard pattern
|
||||
*
|
||||
* @param str String
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean matchesPattern(String str)
|
||||
{
|
||||
|
||||
// Check the pattern type and compare the string
|
||||
|
||||
boolean sts = false;
|
||||
|
||||
switch (isType())
|
||||
{
|
||||
|
||||
// Match all wildcard
|
||||
|
||||
case WILDCARD_ALL:
|
||||
sts = true;
|
||||
break;
|
||||
|
||||
// Match any name
|
||||
|
||||
case WILDCARD_NAME:
|
||||
if (isCaseSensitive())
|
||||
{
|
||||
|
||||
// Check if the string ends with the required file extension
|
||||
|
||||
sts = str.endsWith(m_matchPart);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// Normalize the string and compare
|
||||
|
||||
String upStr = str.toUpperCase();
|
||||
sts = upStr.endsWith(m_matchPart);
|
||||
}
|
||||
break;
|
||||
|
||||
// Match any file extension
|
||||
|
||||
case WILDCARD_EXT:
|
||||
if (isCaseSensitive())
|
||||
{
|
||||
|
||||
// Check if the string starts with the required file name
|
||||
|
||||
sts = str.startsWith(m_matchPart);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// Normalize the string and compare
|
||||
|
||||
String upStr = str.toUpperCase();
|
||||
sts = upStr.startsWith(m_matchPart);
|
||||
}
|
||||
break;
|
||||
|
||||
// Complex wildcard matching
|
||||
|
||||
case WILDCARD_COMPLEX:
|
||||
if (isCaseSensitive())
|
||||
sts = matchComplexWildcard(str);
|
||||
else
|
||||
{
|
||||
|
||||
// Normalize the string and compare
|
||||
|
||||
String upStr = str.toUpperCase();
|
||||
sts = matchComplexWildcard(upStr);
|
||||
}
|
||||
break;
|
||||
|
||||
// No wildcard characters in pattern, compare strings
|
||||
|
||||
case WILDCARD_NONE:
|
||||
if (isCaseSensitive())
|
||||
{
|
||||
if (str.compareTo(m_pattern) == 0)
|
||||
sts = true;
|
||||
}
|
||||
else if (str.equalsIgnoreCase(m_pattern))
|
||||
sts = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// Return the wildcard match status
|
||||
|
||||
return sts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Match a complex wildcard pattern with the specified string
|
||||
*
|
||||
* @param str String
|
||||
* @return boolean
|
||||
*/
|
||||
protected final boolean matchComplexWildcard(String str)
|
||||
{
|
||||
|
||||
// Convert the string to a char array for matching
|
||||
|
||||
char[] strChars = str.toCharArray();
|
||||
|
||||
// Compare the string to the wildcard pattern
|
||||
|
||||
int wpos = 0;
|
||||
int wlen = m_patternChars.length;
|
||||
|
||||
int spos = 0;
|
||||
int slen = strChars.length;
|
||||
|
||||
char patChar;
|
||||
boolean matchFailed = false;
|
||||
|
||||
while (matchFailed == false && wpos < m_patternChars.length)
|
||||
{
|
||||
|
||||
// Match the current pattern character
|
||||
|
||||
patChar = m_patternChars[wpos++];
|
||||
|
||||
switch (patChar)
|
||||
{
|
||||
|
||||
// Match single character
|
||||
|
||||
case SINGLECHAR_WILDCARD:
|
||||
if (spos < slen)
|
||||
spos++;
|
||||
else
|
||||
matchFailed = true;
|
||||
break;
|
||||
|
||||
// Match zero or more characters
|
||||
|
||||
case MULTICHAR_WILDCARD:
|
||||
|
||||
// Check if there is another character in the wildcard pattern
|
||||
|
||||
if (wpos < wlen)
|
||||
{
|
||||
|
||||
// Check if the character is not a wildcard character
|
||||
|
||||
patChar = m_patternChars[wpos];
|
||||
if (patChar != SINGLECHAR_WILDCARD && patChar != MULTICHAR_WILDCARD)
|
||||
{
|
||||
|
||||
// Find the required character in the string
|
||||
|
||||
while (spos < slen && strChars[spos] != patChar)
|
||||
spos++;
|
||||
if (spos >= slen)
|
||||
matchFailed = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// Multi character wildcard at the end of the pattern, match all remaining
|
||||
// characters
|
||||
|
||||
spos = slen;
|
||||
}
|
||||
break;
|
||||
|
||||
// Match the pattern and string character
|
||||
|
||||
default:
|
||||
if (spos >= slen || strChars[spos] != patChar)
|
||||
matchFailed = true;
|
||||
else
|
||||
spos++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the match was successul and return status
|
||||
|
||||
if (matchFailed == false && spos == slen)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the wildcard pattern string
|
||||
*
|
||||
* @param pattern String
|
||||
* @param caseSensitive boolean
|
||||
*/
|
||||
public final void setPattern(String pattern, boolean caseSensitive)
|
||||
{
|
||||
|
||||
// Save the pattern string and case sensitive flag
|
||||
|
||||
m_pattern = pattern;
|
||||
m_caseSensitive = caseSensitive;
|
||||
|
||||
setType(WILDCARD_INVALID);
|
||||
|
||||
// Check if the pattern string is valid
|
||||
|
||||
if (pattern == null || pattern.length() == 0)
|
||||
return;
|
||||
|
||||
// Check for the match all wildcard
|
||||
|
||||
if (pattern.compareTo("*.*") == 0 || pattern.compareTo("*") == 0)
|
||||
{
|
||||
setType(WILDCARD_ALL);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for a name wildcard, ie. '*.ext'
|
||||
|
||||
if (pattern.startsWith("*."))
|
||||
{
|
||||
|
||||
// Split the string to get the extension string
|
||||
|
||||
if (pattern.length() > 2)
|
||||
m_matchPart = pattern.substring(1);
|
||||
else
|
||||
m_matchPart = "";
|
||||
|
||||
// If matching is case insensitive then normalize the string
|
||||
|
||||
if (isCaseSensitive() == false)
|
||||
m_matchPart = m_matchPart.toUpperCase();
|
||||
|
||||
// If the file extension contains wildcards we will need to use a regular expression
|
||||
|
||||
if (containsWildcards(m_matchPart) == false)
|
||||
{
|
||||
setType(WILDCARD_NAME);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for a file extension wildcard
|
||||
|
||||
if (pattern.endsWith(".*"))
|
||||
{
|
||||
|
||||
// Split the string to get the name string
|
||||
|
||||
if (pattern.length() > 2)
|
||||
m_matchPart = pattern.substring(0, pattern.length() - 2);
|
||||
else
|
||||
m_matchPart = "";
|
||||
|
||||
// If matching is case insensitive then normalize the string
|
||||
|
||||
if (isCaseSensitive() == false)
|
||||
m_matchPart = m_matchPart.toUpperCase();
|
||||
|
||||
// If the file name contains wildcards we will need to use a regular expression
|
||||
|
||||
if (containsWildcards(m_matchPart) == false)
|
||||
{
|
||||
setType(WILDCARD_EXT);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Save the complex wildcard pattern as a char array for later pattern matching
|
||||
|
||||
if (isCaseSensitive() == false)
|
||||
m_patternChars = m_pattern.toUpperCase().toCharArray();
|
||||
else
|
||||
m_patternChars = m_pattern.toCharArray();
|
||||
|
||||
setType(WILDCARD_COMPLEX);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the wildcard type
|
||||
*
|
||||
* @param typ int
|
||||
*/
|
||||
private final void setType(int typ)
|
||||
{
|
||||
m_type = typ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the wildcard as a string
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
StringBuffer str = new StringBuffer();
|
||||
str.append("[");
|
||||
str.append(getPattern());
|
||||
str.append(",");
|
||||
str.append(isType());
|
||||
str.append(",");
|
||||
|
||||
if (m_matchPart != null)
|
||||
str.append(m_matchPart);
|
||||
|
||||
if (isCaseSensitive())
|
||||
str.append(",Case");
|
||||
else
|
||||
str.append(",NoCase");
|
||||
str.append("]");
|
||||
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the string contains any wildcard characters.
|
||||
*
|
||||
* @return boolean
|
||||
* @param str java.lang.String
|
||||
*/
|
||||
public final static boolean containsWildcards(String str)
|
||||
{
|
||||
|
||||
// Check the string for wildcard characters
|
||||
|
||||
if (str.indexOf(MULTICHAR_WILDCARD) != -1)
|
||||
return true;
|
||||
|
||||
if (str.indexOf(SINGLECHAR_WILDCARD) != -1)
|
||||
return true;
|
||||
|
||||
// No wildcards found in the string
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a string contains any of the Unicode wildcard characters
|
||||
*
|
||||
* @param str String
|
||||
* @return boolean
|
||||
*/
|
||||
public final static boolean containsUnicodeWildcard(String str)
|
||||
{
|
||||
|
||||
// Check if the string contains any of the Unicode wildcards
|
||||
|
||||
if (str.indexOf(SINGLECHAR_UNICODE_WILDCARD) != -1 || str.indexOf(MULTICHAR_UNICODE_WILDCARD) != -1
|
||||
|| str.indexOf(DOT_UNICODE_WILDCARD) != -1)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the Unicode wildcard string to a standard DOS wildcard string
|
||||
*
|
||||
* @param str String
|
||||
* @return String
|
||||
*/
|
||||
public final static String convertUnicodeWildcardToDOS(String str)
|
||||
{
|
||||
|
||||
// Create a buffer for the new wildcard string
|
||||
|
||||
StringBuffer newStr = new StringBuffer(str.length());
|
||||
|
||||
// Convert the Unicode wildcard string to a DOS wildcard string
|
||||
|
||||
for (int i = 0; i < str.length(); i++)
|
||||
{
|
||||
|
||||
// Get the current character
|
||||
|
||||
char ch = str.charAt(i);
|
||||
|
||||
// Check for a Unicode wildcard character
|
||||
|
||||
if (ch == SINGLECHAR_UNICODE_WILDCARD)
|
||||
{
|
||||
|
||||
// Translate to the DOS single character wildcard character
|
||||
|
||||
ch = SINGLECHAR_WILDCARD;
|
||||
}
|
||||
else if (ch == MULTICHAR_UNICODE_WILDCARD)
|
||||
{
|
||||
|
||||
// Check if the current character is followed by a '.', if so then translate to the
|
||||
// DOS multi character
|
||||
// wildcard
|
||||
|
||||
if (i < (str.length() - 1) && str.charAt(i + 1) == '.')
|
||||
ch = MULTICHAR_WILDCARD;
|
||||
}
|
||||
else if (ch == DOT_UNICODE_WILDCARD)
|
||||
{
|
||||
|
||||
// Check if the current character is followed by a DOS single/multi character
|
||||
// wildcard
|
||||
|
||||
if (i < (str.length() - 1))
|
||||
{
|
||||
char nextCh = str.charAt(i + 1);
|
||||
if (nextCh == SINGLECHAR_WILDCARD || nextCh == MULTICHAR_WILDCARD
|
||||
|| nextCh == SINGLECHAR_UNICODE_WILDCARD)
|
||||
ch = '.';
|
||||
}
|
||||
}
|
||||
|
||||
// Append the character to the translated wildcard string
|
||||
|
||||
newStr.append(ch);
|
||||
}
|
||||
|
||||
// Return the translated wildcard string
|
||||
|
||||
return newStr.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a wildcard string to a regular expression
|
||||
*
|
||||
* @param path String
|
||||
* @return String
|
||||
*/
|
||||
public final static String convertToRegexp(String path)
|
||||
{
|
||||
|
||||
// Convert the path to characters, check if the wildcard string ends with a single character
|
||||
// wildcard
|
||||
|
||||
char[] smbPattern = path.toCharArray();
|
||||
boolean endsWithQ = smbPattern[smbPattern.length - 1] == '?';
|
||||
|
||||
// Build up the regular expression
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append('^');
|
||||
|
||||
for (int i = 0; i < smbPattern.length; i++)
|
||||
{
|
||||
|
||||
// Process the current character
|
||||
|
||||
switch (smbPattern[i])
|
||||
{
|
||||
|
||||
// Multi character wildcard
|
||||
|
||||
case '*':
|
||||
sb.append(".*");
|
||||
break;
|
||||
|
||||
// Single character wildcard
|
||||
|
||||
case '?':
|
||||
if (endsWithQ)
|
||||
{
|
||||
boolean restQ = true;
|
||||
for (int j = i + 1; j < smbPattern.length; j++)
|
||||
{
|
||||
if (smbPattern[j] != '?')
|
||||
{
|
||||
restQ = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (restQ)
|
||||
sb.append(".?");
|
||||
else
|
||||
sb.append('.');
|
||||
}
|
||||
else
|
||||
sb.append('.');
|
||||
break;
|
||||
|
||||
// Escape regular expression special characters
|
||||
|
||||
case '.':
|
||||
case '+':
|
||||
case '\\':
|
||||
case '[':
|
||||
case ']':
|
||||
case '^':
|
||||
case '$':
|
||||
case '(':
|
||||
case ')':
|
||||
sb.append('\\');
|
||||
sb.append(smbPattern[i]);
|
||||
break;
|
||||
|
||||
// Normal characters, just pass through
|
||||
|
||||
default:
|
||||
sb.append(smbPattern[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
sb.append('$');
|
||||
|
||||
// Return the regular expression string
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user