Java Code Examples for com.helger.commons.CGlobal#ILLEGAL_UINT

The following examples show how to use com.helger.commons.CGlobal#ILLEGAL_UINT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StreamHelper.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Read the content of the passed stream line by line and invoking a callback
 * on all matching lines.
 *
 * @param aIS
 *        The input stream to read from. May be <code>null</code>.
 * @param aCharset
 *        The character set to use. May not be <code>null</code>.
 * @param nLinesToSkip
 *        The 0-based index of the first line to read. Pass in 0 to indicate
 *        to read everything.
 * @param nLinesToRead
 *        The number of lines to read. Pass in {@link CGlobal#ILLEGAL_UINT} to
 *        indicate that all lines should be read. If the number passed here
 *        exceeds the number of lines in the file, nothing happens.
 * @param aLineCallback
 *        The callback that is invoked for all read lines. Each passed line
 *        does NOT contain the line delimiter! Note: it is not invoked for
 *        skipped lines!
 */
public static void readStreamLines (@WillClose @Nullable final InputStream aIS,
                                    @Nonnull @Nonempty final Charset aCharset,
                                    @Nonnegative final int nLinesToSkip,
                                    final int nLinesToRead,
                                    @Nonnull final Consumer <? super String> aLineCallback)
{
  try
  {
    ValueEnforcer.notNull (aCharset, "Charset");
    ValueEnforcer.isGE0 (nLinesToSkip, "LinesToSkip");
    final boolean bReadAllLines = nLinesToRead == CGlobal.ILLEGAL_UINT;
    ValueEnforcer.isTrue (bReadAllLines || nLinesToRead >= 0,
                          () -> "Line count may not be that negative: " + nLinesToRead);
    ValueEnforcer.notNull (aLineCallback, "LineCallback");

    // Start the action only if there is something to read
    if (aIS != null)
      if (bReadAllLines || nLinesToRead > 0)
      {
        try (final NonBlockingBufferedReader aBR = new NonBlockingBufferedReader (createReader (aIS, aCharset)))
        {
          // read with the passed charset
          _readFromReader (nLinesToSkip, nLinesToRead, aLineCallback, bReadAllLines, aBR);
        }
        catch (final IOException ex)
        {
          LOGGER.error ("Failed to read from reader", _propagate (ex));
        }
      }
  }
  finally
  {
    // Close input stream anyway
    close (aIS);
  }
}
 
Example 2
Source File: CSSSourceLocation.java    From ph-css with Apache License 2.0 4 votes vote down vote up
/**
 * @return The column number where the first token ends (incl.). May be -1 if
 *         not such token is available.
 */
@CheckForSigned
public int getFirstTokenEndColumnNumber ()
{
  return m_aFirstTokenArea == null ? CGlobal.ILLEGAL_UINT : m_aFirstTokenArea.getTokenEndColumnNumber ();
}
 
Example 3
Source File: ASCIIHexCodec.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
public void decode (@Nullable final byte [] aEncodedBuffer,
                    @Nonnegative final int nOfs,
                    @Nonnegative final int nLen,
                    @Nonnull @WillNotClose final OutputStream aOS)
{
  if (aEncodedBuffer == null || nLen == 0)
    return;

  try
  {
    boolean bFirstByte = true;
    int nFirstByte = 0;
    for (int i = 0; i < nLen; ++i)
    {
      final byte nEncByte = aEncodedBuffer[nOfs + i];
      if (nEncByte == '>')
        break;

      // Ignore whitespaces
      if (Character.isWhitespace (nEncByte))
        continue;

      final byte nDecByte = (byte) StringHelper.getHexValue ((char) nEncByte);
      if (nDecByte == CGlobal.ILLEGAL_UINT)
        throw new DecodeException ("Failed to convert byte '" +
                                   nEncByte +
                                   "/" +
                                   ((char) nEncByte) +
                                   "' to hex value in ASCIIHexDecode");
      if (bFirstByte)
        nFirstByte = nDecByte;
      else
        aOS.write ((byte) (nFirstByte << 4 | nDecByte & 0xff));
      bFirstByte = !bFirstByte;
    }

    // Write trailing byte
    if (!bFirstByte)
      aOS.write ((byte) (nFirstByte << 4));
  }
  catch (final IOException ex)
  {
    throw new DecodeException ("Failed to decode ASCII Hex", ex);
  }
}
 
Example 4
Source File: CSSSourceLocation.java    From ph-css with Apache License 2.0 4 votes vote down vote up
/**
 * @return The line number where the last token begins (incl.). May be -1 if
 *         not such token is available.
 */
@CheckForSigned
public int getLastTokenBeginLineNumber ()
{
  return m_aLastTokenArea == null ? CGlobal.ILLEGAL_UINT : m_aLastTokenArea.getTokenBeginLineNumber ();
}
 
Example 5
Source File: CSSSourceLocation.java    From ph-css with Apache License 2.0 4 votes vote down vote up
/**
 * @return The column number where the last token ends (incl.). May be -1 if
 *         not such token is available.
 */
@CheckForSigned
public int getLastTokenEndColumnNumber ()
{
  return m_aLastTokenArea == null ? CGlobal.ILLEGAL_UINT : m_aLastTokenArea.getTokenEndColumnNumber ();
}
 
Example 6
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the index of the passed search value in the passed value array.
 *
 * @param aValues
 *        The value array to be searched. May be <code>null</code>.
 * @param aSearchValue
 *        The value to be searched. May be <code>null</code>.
 * @return <code>-1</code> if the searched value is not contained, a value
 *         &ge; 0 otherwise.
 */
public static int getLastIndex (@Nullable final int [] aValues, final int aSearchValue)
{
  final int nLength = getSize (aValues);
  if (nLength > 0)
    for (int nIndex = nLength - 1; nIndex >= 0; --nIndex)
      if (EqualsHelper.equals (aValues[nIndex], aSearchValue))
        return nIndex;
  return CGlobal.ILLEGAL_UINT;
}
 
Example 7
Source File: FilenameHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the extension of the passed filename.
 *
 * @param sFilename
 *        The filename to extract the extension from. May be <code>null</code>
 *        or empty.
 * @return An empty string if no extension was found, the extension without
 *         the leading dot otherwise. Never <code>null</code>.
 * @see #getIndexOfExtension(String)
 */
@Nonnull
public static String getExtension (@Nullable final String sFilename)
{
  final int nIndex = getIndexOfExtension (sFilename);
  if (nIndex == CGlobal.ILLEGAL_UINT)
    return "";
  return sFilename.substring (nIndex + 1);
}
 
Example 8
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the index of the passed search value in the passed value array.
 *
 * @param aValues
 *        The value array to be searched. May be <code>null</code>.
 * @param aSearchValue
 *        The value to be searched. May be <code>null</code>.
 * @return <code>-1</code> if the searched value is not contained, a value
 *         &ge; 0 otherwise.
 */
public static int getLastIndex (@Nullable final float [] aValues, final float aSearchValue)
{
  final int nLength = getSize (aValues);
  if (nLength > 0)
    for (int nIndex = nLength - 1; nIndex >= 0; --nIndex)
      if (EqualsHelper.equals (aValues[nIndex], aSearchValue))
        return nIndex;
  return CGlobal.ILLEGAL_UINT;
}
 
Example 9
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the index of the passed search value in the passed value array.
 *
 * @param aValues
 *        The value array to be searched. May be <code>null</code>.
 * @param aSearchValue
 *        The value to be searched. May be <code>null</code>.
 * @return <code>-1</code> if the searched value is not contained, a value
 *         &ge; 0 otherwise.
 */
public static int getLastIndex (@Nullable final double [] aValues, final double aSearchValue)
{
  final int nLength = getSize (aValues);
  if (nLength > 0)
    for (int nIndex = nLength - 1; nIndex >= 0; --nIndex)
      if (EqualsHelper.equals (aValues[nIndex], aSearchValue))
        return nIndex;
  return CGlobal.ILLEGAL_UINT;
}
 
Example 10
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the index of the passed search value in the passed value array.
 *
 * @param aValues
 *        The value array to be searched. May be <code>null</code>.
 * @param aSearchValue
 *        The value to be searched. May be <code>null</code>.
 * @return <code>-1</code> if the searched value is not contained, a value
 *         &ge; 0 otherwise.
 */
public static int getLastIndex (@Nullable final boolean [] aValues, final boolean aSearchValue)
{
  final int nLength = getSize (aValues);
  if (nLength > 0)
    for (int nIndex = nLength - 1; nIndex >= 0; --nIndex)
      if (EqualsHelper.equals (aValues[nIndex], aSearchValue))
        return nIndex;
  return CGlobal.ILLEGAL_UINT;
}
 
Example 11
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the index of the passed search value in the passed value array.
 *
 * @param <ELEMENTTYPE>
 *        Array element type
 * @param aValues
 *        The value array to be searched. May be <code>null</code>.
 * @param aSearchValue
 *        The value to be searched. May be <code>null</code>.
 * @return <code>-1</code> if the searched value is not contained, a value
 *         &ge; 0 otherwise.
 */
public static <ELEMENTTYPE> int getLastIndex (@Nullable final ELEMENTTYPE [] aValues,
                                              @Nullable final ELEMENTTYPE aSearchValue)
{
  final int nLength = getSize (aValues);
  if (nLength > 0)
    for (int nIndex = nLength - 1; nIndex >= 0; --nIndex)
      if (EqualsHelper.equals (aValues[nIndex], aSearchValue))
        return nIndex;
  return CGlobal.ILLEGAL_UINT;
}
 
Example 12
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the index of the passed search value in the passed value array.
 *
 * @param aValues
 *        The value array to be searched. May be <code>null</code>.
 * @param aSearchValue
 *        The value to be searched. May be <code>null</code>.
 * @return <code>-1</code> if the searched value is not contained, a value
 *         &ge; 0 otherwise.
 */
public static int getFirstIndex (@Nullable final short [] aValues, final short aSearchValue)
{
  final int nLength = getSize (aValues);
  if (nLength > 0)
    for (int nIndex = 0; nIndex < nLength; ++nIndex)
      if (EqualsHelper.equals (aValues[nIndex], aSearchValue))
        return nIndex;
  return CGlobal.ILLEGAL_UINT;
}
 
Example 13
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the index of the passed search value in the passed value array.
 *
 * @param aValues
 *        The value array to be searched. May be <code>null</code>.
 * @param aSearchValue
 *        The value to be searched. May be <code>null</code>.
 * @return <code>-1</code> if the searched value is not contained, a value
 *         &ge; 0 otherwise.
 */
public static int getFirstIndex (@Nullable final long [] aValues, final long aSearchValue)
{
  final int nLength = getSize (aValues);
  if (nLength > 0)
    for (int nIndex = 0; nIndex < nLength; ++nIndex)
      if (EqualsHelper.equals (aValues[nIndex], aSearchValue))
        return nIndex;
  return CGlobal.ILLEGAL_UINT;
}
 
Example 14
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the index of the passed search value in the passed value array.
 *
 * @param aValues
 *        The value array to be searched. May be <code>null</code>.
 * @param aSearchValue
 *        The value to be searched. May be <code>null</code>.
 * @return <code>-1</code> if the searched value is not contained, a value
 *         &ge; 0 otherwise.
 */
public static int getLastIndex (@Nullable final short [] aValues, final short aSearchValue)
{
  final int nLength = getSize (aValues);
  if (nLength > 0)
    for (int nIndex = nLength - 1; nIndex >= 0; --nIndex)
      if (EqualsHelper.equals (aValues[nIndex], aSearchValue))
        return nIndex;
  return CGlobal.ILLEGAL_UINT;
}
 
Example 15
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the index of the passed search value in the passed value array.
 *
 * @param aValues
 *        The value array to be searched. May be <code>null</code>.
 * @param aSearchValue
 *        The value to be searched. May be <code>null</code>.
 * @return <code>-1</code> if the searched value is not contained, a value
 *         &ge; 0 otherwise.
 */
public static int getFirstIndex (@Nullable final double [] aValues, final double aSearchValue)
{
  final int nLength = getSize (aValues);
  if (nLength > 0)
    for (int nIndex = 0; nIndex < nLength; ++nIndex)
      if (EqualsHelper.equals (aValues[nIndex], aSearchValue))
        return nIndex;
  return CGlobal.ILLEGAL_UINT;
}
 
Example 16
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the index of the passed search value in the passed value array.
 *
 * @param aValues
 *        The value array to be searched. May be <code>null</code>.
 * @param aSearchValue
 *        The value to be searched. May be <code>null</code>.
 * @return <code>-1</code> if the searched value is not contained, a value
 *         &ge; 0 otherwise.
 */
public static int getFirstIndex (@Nullable final char [] aValues, final char aSearchValue)
{
  final int nLength = getSize (aValues);
  if (nLength > 0)
    for (int nIndex = 0; nIndex < nLength; ++nIndex)
      if (EqualsHelper.equals (aValues[nIndex], aSearchValue))
        return nIndex;
  return CGlobal.ILLEGAL_UINT;
}
 
Example 17
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the index of the passed search value in the passed value array.
 *
 * @param aValues
 *        The value array to be searched. May be <code>null</code>.
 * @param aSearchValue
 *        The value to be searched. May be <code>null</code>.
 * @return <code>-1</code> if the searched value is not contained, a value
 *         &ge; 0 otherwise.
 */
public static int getFirstIndex (@Nullable final byte [] aValues, final byte aSearchValue)
{
  final int nLength = getSize (aValues);
  if (nLength > 0)
    for (int nIndex = 0; nIndex < nLength; ++nIndex)
      if (EqualsHelper.equals (aValues[nIndex], aSearchValue))
        return nIndex;
  return CGlobal.ILLEGAL_UINT;
}
 
Example 18
Source File: ArrayHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Get the index of the passed search value in the passed value array.
 *
 * @param aValues
 *        The value array to be searched. May be <code>null</code>.
 * @param aSearchValue
 *        The value to be searched. May be <code>null</code>.
 * @return <code>-1</code> if the searched value is not contained, a value
 *         &ge; 0 otherwise.
 */
public static int getFirstIndex (@Nullable final boolean [] aValues, final boolean aSearchValue)
{
  final int nLength = getSize (aValues);
  if (nLength > 0)
    for (int nIndex = 0; nIndex < nLength; ++nIndex)
      if (EqualsHelper.equals (aValues[nIndex], aSearchValue))
        return nIndex;
  return CGlobal.ILLEGAL_UINT;
}
 
Example 19
Source File: FilenameHelper.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the index of the last extension separator character, which is a
 * dot.
 * <p>
 * This method also checks that there is no directory separator after the last
 * dot. To do this it uses {@link #getIndexOfLastSeparator(String)} which will
 * handle a file in either Unix or Windows format.
 * <p>
 * The output will be the same irrespective of the machine that the code is
 * running on.
 *
 * @param sFilename
 *        The filename to find the last path separator in. May be
 *        <code>null</code>.
 * @return the index of the last separator character, or
 *         {@link CGlobal#ILLEGAL_UINT} if there is no such character or the
 *         input parameter is <code>null</code>.
 * @see #getIndexOfLastSeparator(String)
 */
public static int getIndexOfExtension (@Nullable final String sFilename)
{
  if (sFilename == null)
    return CGlobal.ILLEGAL_UINT;

  final int nExtensionIndex = sFilename.lastIndexOf (EXTENSION_SEPARATOR);
  final int nLastSepIndex = getIndexOfLastSeparator (sFilename);
  return nLastSepIndex > nExtensionIndex ? CGlobal.ILLEGAL_UINT : nExtensionIndex;
}
 
Example 20
Source File: FilenameHelper.java    From ph-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the index of the last directory separator character. This method
 * will handle a file in either Unix or Windows format. The position of the
 * last forward or backslash is returned. The output will be the same
 * irrespective of the machine that the code is running on.
 *
 * @param sFilename
 *        The filename to find the last path separator in, <code>null</code>
 *        returns {@link CGlobal#ILLEGAL_UINT}.
 * @return The index of the last separator character, or
 *         {@link CGlobal#ILLEGAL_UINT} if there is no such character
 */
public static int getIndexOfLastSeparator (@Nullable final String sFilename)
{
  return sFilename == null ? CGlobal.ILLEGAL_UINT : Math.max (sFilename.lastIndexOf (UNIX_SEPARATOR),
                                                              sFilename.lastIndexOf (WINDOWS_SEPARATOR));
}