Java Code Examples for com.helger.commons.ValueEnforcer#isArrayOfsLen()
The following examples show how to use
com.helger.commons.ValueEnforcer#isArrayOfsLen() .
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 | 6 votes |
/** * Write bytes to an {@link OutputStream}. * * @param aOS * The output stream to write to. May not be <code>null</code>. Is * closed independent of error or success. * @param aBuf * The byte array from which is to be written. May not be * <code>null</code>. * @param nOfs * The 0-based index to the first byte in the array to be written. May * not be < 0. * @param nLen * The non-negative amount of bytes to be written. May not be < 0. * @return {@link ESuccess} */ @Nonnull public static ESuccess writeStream (@WillClose @Nonnull final OutputStream aOS, @Nonnull final byte [] aBuf, @Nonnegative final int nOfs, @Nonnegative final int nLen) { try { ValueEnforcer.notNull (aOS, "OutputStream"); ValueEnforcer.isArrayOfsLen (aBuf, nOfs, nLen); aOS.write (aBuf, nOfs, nLen); aOS.flush (); return ESuccess.SUCCESS; } catch (final IOException ex) { LOGGER.error ("Failed to write to output stream", _propagate (ex)); return ESuccess.FAILURE; } finally { close (aOS); } }
Example 2
Source File: StreamHelper.java From ph-commons with Apache License 2.0 | 6 votes |
/** * Read the whole buffer from the input stream. * * @param aIS * The input stream to read from. May not be <code>null</code>. * @param aBuffer * The buffer to write to. May not be <code>null</code>. Must be ≥ * than the content to be read. * @param nOfs * The offset into the destination buffer to use. May not be < 0. * @param nLen * The number of bytes to read into the destination buffer to use. May * not be < 0. * @return The number of read bytes * @throws IOException * In case reading fails */ @Nonnegative public static int readFully (@Nonnull @WillNotClose final InputStream aIS, @Nonnull final byte [] aBuffer, @Nonnegative final int nOfs, @Nonnegative final int nLen) throws IOException { ValueEnforcer.notNull (aIS, "InputStream"); ValueEnforcer.isArrayOfsLen (aBuffer, nOfs, nLen); int nTotalBytesRead = 0; while (nTotalBytesRead < nLen) { // Potentially blocking read final int nBytesRead = aIS.read (aBuffer, nOfs + nTotalBytesRead, nLen - nTotalBytesRead); if (nBytesRead < 0) throw new EOFException ("Failed to read a total of " + nLen + " bytes from input stream. Only read " + nTotalBytesRead + " bytes so far."); nTotalBytesRead += nBytesRead; } return nTotalBytesRead; }
Example 3
Source File: StringHelper.java From ph-commons with Apache License 2.0 | 6 votes |
/** * Get a concatenated String from all elements of the passed array, separated * by the specified separator string. * * @param sSep * The separator to use. May not be <code>null</code>. * @param aElements * The container to convert. May be <code>null</code> or empty. * @param nOfs * The offset to start from. * @param nLen * The number of elements to implode. * @param aMapper * The mapping function to convert from ELEMENTTYPE to String. May not * be <code>null</code>. * @return The concatenated string. * @param <ELEMENTTYPE> * The type of elements to be imploded. * @since 8.5.6 */ @Nonnull public static <ELEMENTTYPE> String getImplodedMapped (@Nonnull final String sSep, @Nullable final ELEMENTTYPE [] aElements, @Nonnegative final int nOfs, @Nonnegative final int nLen, @Nonnull final Function <? super ELEMENTTYPE, String> aMapper) { ValueEnforcer.notNull (sSep, "Separator"); if (aElements != null) ValueEnforcer.isArrayOfsLen (aElements, nOfs, nLen); ValueEnforcer.notNull (aMapper, "Mapper"); final StringBuilder aSB = new StringBuilder (); if (aElements != null) { for (int i = nOfs; i < nOfs + nLen; ++i) { final ELEMENTTYPE sElement = aElements[i]; if (i > nOfs) aSB.append (sSep); aSB.append (aMapper.apply (sElement)); } } return aSB.toString (); }
Example 4
Source File: StringHelper.java From ph-commons with Apache License 2.0 | 6 votes |
@Nonnull @ReturnsMutableCopy public static byte [] getHexDecoded (@Nonnull final char [] aInput, @Nonnegative final int nOfs, @Nonnegative final int nLen) { ValueEnforcer.isArrayOfsLen (aInput, nOfs, nLen); ValueEnforcer.isTrue ((nLen % 2) == 0, () -> "Passed chars have no even length: " + nLen); final byte [] ret = new byte [nLen / 2]; int nRetIdx = 0; for (int i = 0; i < nLen; i += 2) { final char c0 = aInput[nOfs + i]; final char c1 = aInput[nOfs + i + 1]; final int nHexByte = getHexByte (c0, c1); if (nHexByte == -1) throw new IllegalArgumentException ("Failed to convert '" + c0 + "' and '" + c1 + "' to a hex value!"); ret[nRetIdx++] = (byte) nHexByte; } return ret; }
Example 5
Source File: NonBlockingStringReader.java From ph-commons with Apache License 2.0 | 6 votes |
/** * Reads characters into a portion of an array. * * @param aBuf * Destination buffer * @param nOfs * Offset at which to start writing characters * @param nLen * Maximum number of characters to read * @return The number of characters read, or -1 if the end of the stream has * been reached * @exception IOException * If an I/O error occurs */ @Override @CheckForSigned public int read (@Nonnull final char [] aBuf, @Nonnegative final int nOfs, @Nonnegative final int nLen) throws IOException { _ensureOpen (); ValueEnforcer.isArrayOfsLen (aBuf, nOfs, nLen); if (nLen == 0) return 0; if (m_nNext >= m_nLength) return -1; final int nChars = Math.min (m_nLength - m_nNext, nLen); System.arraycopy (m_aChars, m_nNext, aBuf, nOfs, nChars); m_nNext += nChars; return nChars; }
Example 6
Source File: NonBlockingPushbackReader.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Reads characters into a portion of an array. * * @param aBuf * Destination buffer * @param nOfs * Offset at which to start writing characters * @param nLen * Maximum number of characters to read * @return The number of characters read, or -1 if the end of the stream has * been reached * @exception IOException * If an I/O error occurs */ @Override public int read (@Nonnull final char [] aBuf, @Nonnegative final int nOfs, @Nonnegative final int nLen) throws IOException { ValueEnforcer.isArrayOfsLen (aBuf, nOfs, nLen); _ensureOpen (); if (nLen == 0) return 0; try { int nRealOfs = nOfs; int nRealLen = nLen; int nBufAvail = m_aBuf.length - m_nBufPos; if (nBufAvail > 0) { if (nRealLen < nBufAvail) nBufAvail = nRealLen; System.arraycopy (m_aBuf, m_nBufPos, aBuf, nRealOfs, nBufAvail); m_nBufPos += nBufAvail; nRealOfs += nBufAvail; nRealLen -= nBufAvail; } if (nRealLen > 0) { nRealLen = super.read (aBuf, nRealOfs, nRealLen); if (nRealLen == -1) return (nBufAvail == 0) ? -1 : nBufAvail; return nBufAvail + nRealLen; } return nBufAvail; } catch (final ArrayIndexOutOfBoundsException e) { throw new IndexOutOfBoundsException (); } }
Example 7
Source File: ByteBufferInputStream.java From ph-commons with Apache License 2.0 | 5 votes |
@Override public int read (@Nonnull final byte [] aBuffer, @Nonnegative final int nOfs, @Nonnegative final int nLen) { ValueEnforcer.isArrayOfsLen (aBuffer, nOfs, nLen); if (!m_aBuffer.hasRemaining ()) return -1; if (nLen == 0 || aBuffer.length == 0) return isAnythingAvailable () ? 0 : -1; final int nMaxRead = Math.min (m_aBuffer.remaining (), nLen); m_aBuffer.get (aBuffer, nOfs, nMaxRead); return nMaxRead; }
Example 8
Source File: StringHelper.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Encode a char array to a byte array using the provided charset. This does * the same as <code>new String (aCharArray).getBytes (aCharset)</code> just * without the intermediate objects. * * @param aCharset * Charset to be used. May not be <code>null</code>. * @param aCharArray * The char array to be encoded. May not be <code>null</code>. * @param nOfs * Offset into char array. Must be ≥ 0. * @param nLen * Chars to encode. Must be ≥ 0. * @return The created byte array. Never <code>null</code>. * @since 8.6.4 */ @Nonnull @ReturnsMutableCopy public static byte [] encodeCharToBytes (@Nonnull final char [] aCharArray, @Nonnegative final int nOfs, @Nonnegative final int nLen, @Nonnull final Charset aCharset) { ValueEnforcer.isArrayOfsLen (aCharArray, nOfs, nLen); final CharsetEncoder aEncoder = aCharset.newEncoder (); // We need to perform double, not float, arithmetic; otherwise // we lose low order bits when nLen is larger than 2^24. final int nEncodedLen = (int) (nLen * (double) aEncoder.maxBytesPerChar ()); final byte [] aByteArray = new byte [nEncodedLen]; if (nLen == 0) return aByteArray; aEncoder.onMalformedInput (CodingErrorAction.REPLACE).onUnmappableCharacter (CodingErrorAction.REPLACE).reset (); final CharBuffer aSrcBuf = CharBuffer.wrap (aCharArray, nOfs, nLen); final ByteBuffer aDstBuf = ByteBuffer.wrap (aByteArray); try { CoderResult aRes = aEncoder.encode (aSrcBuf, aDstBuf, true); if (!aRes.isUnderflow ()) aRes.throwException (); aRes = aEncoder.flush (aDstBuf); if (!aRes.isUnderflow ()) aRes.throwException (); } catch (final CharacterCodingException x) { throw new IllegalStateException (x); } final int nDstLen = aDstBuf.position (); if (nDstLen == aByteArray.length) return aByteArray; return Arrays.copyOf (aByteArray, nDstLen); }
Example 9
Source File: StringHelper.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Get a concatenated String from all elements of the passed array, separated * by the specified separator string. This the very generic version of * {@link #getConcatenatedOnDemand(String, String, String)} for an arbitrary * number of elements. * * @param sSep * The separator to use. May not be <code>null</code>. * @param aElements * The container to convert. May be <code>null</code> or empty. * @param nOfs * The offset to start from. * @param nLen * The number of elements to implode. * @param aMapper * The mapping function to convert from ELEMENTTYPE to String. May not * be <code>null</code>. * @return The concatenated string. * @param <ELEMENTTYPE> * Array component type * @since 8.5.6 */ @Nonnull public static <ELEMENTTYPE> String getImplodedMappedNonEmpty (@Nonnull final String sSep, @Nullable final ELEMENTTYPE [] aElements, @Nonnegative final int nOfs, @Nonnegative final int nLen, @Nonnull final Function <? super ELEMENTTYPE, String> aMapper) { ValueEnforcer.notNull (sSep, "Separator"); if (aElements != null) ValueEnforcer.isArrayOfsLen (aElements, nOfs, nLen); ValueEnforcer.notNull (aMapper, "Mapper"); final StringBuilder aSB = new StringBuilder (); if (aElements != null) { int nElementsAdded = 0; for (int i = nOfs; i < nOfs + nLen; ++i) { final String sElement = aMapper.apply (aElements[i]); if (hasText (sElement)) { if (nElementsAdded > 0) aSB.append (sSep); nElementsAdded++; aSB.append (sElement); } } } return aSB.toString (); }
Example 10
Source File: NonBlockingPushbackInputStream.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Reads up to <code>len</code> bytes of data from this input stream into an * array of bytes. This method first reads any pushed-back bytes; after that, * if fewer than <code>len</code> bytes have been read then it reads from the * underlying input stream. If <code>len</code> is not zero, the method blocks * until at least 1 byte of input is available; otherwise, no bytes are read * and <code>0</code> is returned. * * @param aBuf * the buffer into which the data is read. * @param nOfs * the start offset in the destination array <code>b</code> * @param nLen * the maximum number of bytes read. * @return the total number of bytes read into the buffer, or <code>-1</code> * if there is no more data because the end of the stream has been * reached. * @exception NullPointerException * If <code>b</code> is <code>null</code>. * @exception IndexOutOfBoundsException * If <code>off</code> is negative, <code>len</code> is negative, * or <code>len</code> is greater than <code>b.length - off</code> * @exception IOException * if this input stream has been closed by invoking its * {@link #close()} method, or an I/O error occurs. * @see java.io.InputStream#read(byte[], int, int) */ @Override public int read (@Nonnull final byte [] aBuf, @Nonnegative final int nOfs, @Nonnegative final int nLen) throws IOException { ValueEnforcer.isArrayOfsLen (aBuf, nOfs, nLen); _ensureOpen (); if (nLen == 0) return 0; int nRealOfs = nOfs; int nRealLen = nLen; int nBufAvail = m_aBuf.length - m_nBufPos; if (nBufAvail > 0) { if (nRealLen < nBufAvail) nBufAvail = nRealLen; System.arraycopy (m_aBuf, m_nBufPos, aBuf, nRealOfs, nBufAvail); m_nBufPos += nBufAvail; nRealOfs += nBufAvail; nRealLen -= nBufAvail; } if (nRealLen > 0) { nRealLen = super.read (aBuf, nRealOfs, nRealLen); if (nRealLen == -1) return nBufAvail == 0 ? -1 : nBufAvail; return nBufAvail + nRealLen; } return nBufAvail; }
Example 11
Source File: WrappedOutputStream.java From ph-commons with Apache License 2.0 | 4 votes |
@Override public void write (@Nonnull final byte [] aBuf, final int nOfs, final int nLen) throws IOException { ValueEnforcer.isArrayOfsLen (aBuf, nOfs, nLen); out.write (aBuf, nOfs, nLen); }
Example 12
Source File: MicroDataAware.java From ph-commons with Apache License 2.0 | 4 votes |
public MicroDataAware (@Nonnull final char [] aChars, @Nonnegative final int nOfs, @Nonnegative final int nLen) { ValueEnforcer.isArrayOfsLen (aChars, nOfs, nLen); m_aSB = new StringBuilder (nLen + 16).append (aChars, nOfs, nLen); }
Example 13
Source File: StringHelper.java From ph-commons with Apache License 2.0 | 4 votes |
/** * Get a concatenated String from all elements of the passed array, without a * separator. * * @param aElements * The container to convert. May be <code>null</code> or empty. * @param nOfs * The offset to start from. * @param nLen * The number of elements to implode. * @param aMapper * The mapping function to convert from ELEMENTTYPE to String. May not * be <code>null</code>. * @return The concatenated string. * @param <ELEMENTTYPE> * The type of elements to be imploded. * @since 8.5.6 */ @Nonnull public static <ELEMENTTYPE> String getImplodedMapped (@Nullable final ELEMENTTYPE [] aElements, @Nonnegative final int nOfs, @Nonnegative final int nLen, @Nonnull final Function <? super ELEMENTTYPE, String> aMapper) { if (aElements != null) ValueEnforcer.isArrayOfsLen (aElements, nOfs, nLen); ValueEnforcer.notNull (aMapper, "Mapper"); final StringBuilder aSB = new StringBuilder (); if (aElements != null) for (int i = nOfs; i < nOfs + nLen; ++i) aSB.append (aMapper.apply (aElements[i])); return aSB.toString (); }
Example 14
Source File: NonBlockingBufferedInputStream.java From ph-commons with Apache License 2.0 | 4 votes |
/** * Reads bytes from this byte-input stream into the specified byte array, * starting at the given offset. * <p> * This method implements the general contract of the corresponding * <code>{@link InputStream#read(byte[], int, int) read}</code> method of the * <code>{@link InputStream}</code> class. As an additional convenience, it * attempts to read as many bytes as possible by repeatedly invoking the * <code>read</code> method of the underlying stream. This iterated * <code>read</code> continues until one of the following conditions becomes * true: * <ul> * <li>The specified number of bytes have been read, * <li>The <code>read</code> method of the underlying stream returns * <code>-1</code>, indicating end-of-file, or * <li>The <code>available</code> method of the underlying stream returns * zero, indicating that further input requests would block. * </ul> * If the first <code>read</code> on the underlying stream returns * <code>-1</code> to indicate end-of-file then this method returns * <code>-1</code>. Otherwise this method returns the number of bytes actually * read. * <p> * Subclasses of this class are encouraged, but not required, to attempt to * read as many bytes as possible in the same fashion. * * @param aBuf * destination buffer. * @param nOfs * offset at which to start storing bytes. * @param nLen * maximum number of bytes to read. * @return the number of bytes read, or <code>-1</code> if the end of the * stream has been reached. * @exception IOException * if this input stream has been closed by invoking its * {@link #close()} method, or an I/O error occurs. */ @Override public int read (final byte [] aBuf, final int nOfs, final int nLen) throws IOException { ValueEnforcer.isArrayOfsLen (aBuf, nOfs, nLen); // Check for closed stream _getBufIfOpen (); if (nLen == 0) return 0; int nTotal = 0; for (;;) { final int nRead = _read1 (aBuf, nOfs + nTotal, nLen - nTotal); if (nRead <= 0) return nTotal == 0 ? nRead : nTotal; nTotal += nRead; if (nTotal >= nLen) return nTotal; // if not closed but no bytes available, return final InputStream aIS = in; if (aIS != null && aIS.available () <= 0) return nTotal; } }
Example 15
Source File: Base64.java From ph-commons with Apache License 2.0 | 4 votes |
/** * Low-level access to decoding ASCII characters in the form of a byte array. * <strong>Ignores GUNZIP option, if it's set.</strong> This is not generally * a recommended method, although it is used internally as part of the * decoding process. Special case: if len = 0, an empty array is returned. * Still, if you need more speed and reduced memory footprint (and aren't * gzipping), consider this method. * * @param aSource * The Base64 encoded data * @param nOfs * The offset of where to begin decoding * @param nLen * The length of characters to decode * @param nOptions * Can specify options such as alphabet type to use * @return decoded data * @throws IOException * If bogus characters exist in source data * @since 1.3 */ @Nonnull @ReturnsMutableCopy public static byte [] decode (@Nonnull final byte [] aSource, final int nOfs, final int nLen, final int nOptions) throws IOException { // Lots of error checking and exception throwing ValueEnforcer.isArrayOfsLen (aSource, nOfs, nLen); if (nLen == 0) return ArrayHelper.EMPTY_BYTE_ARRAY; ValueEnforcer.isTrue (nLen >= 4, () -> "Base64-encoded string must have at least four characters, but length specified was " + nLen); final byte [] aDecodabet = _getDecodabet (nOptions); // Estimate on array size final int len34 = nLen * 3 / 4; // Upper limit on size of output final byte [] outBuff = new byte [len34]; // Keep track of where we're writing int outBuffPosn = 0; // Four byte buffer from source, eliminating white space final byte [] b4 = new byte [4]; // Keep track of four byte input buffer int b4Posn = 0; // Source array counter int i; // Special value from DECODABET byte sbiDecode; for (i = nOfs; i < nOfs + nLen; i++) { // Loop through source sbiDecode = aDecodabet[aSource[i] & 0xFF]; // White space, Equals sign, or legit Base64 character // Note the values such as -5 and -9 in the // DECODABETs at the top of the file. if (sbiDecode >= WHITE_SPACE_ENC) { if (sbiDecode >= EQUALS_SIGN_ENC) { b4[b4Posn++] = aSource[i]; // Save non-whitespace if (b4Posn > 3) { // Time to decode? outBuffPosn += _decode4to3 (b4, 0, outBuff, outBuffPosn, nOptions); b4Posn = 0; // If that was the equals sign, break out of 'for' loop if (aSource[i] == EQUALS_SIGN) break; } } } else { // There's a bad input character in the Base64 stream. throw new IOException ("Bad Base64 input character decimal " + (aSource[i] & 0xFF) + " in array position " + i); } } final byte [] aOut = new byte [outBuffPosn]; System.arraycopy (outBuff, 0, aOut, 0, outBuffPosn); return aOut; }
Example 16
Source File: ArrayIteratorShort.java From ph-commons with Apache License 2.0 | 3 votes |
/** * constructor with offset and length * * @param aArray * Source array * @param nOfs * Offset. Must be ≥ 0. * @param nLength * Length. Must be ≥ 0. */ public ArrayIteratorShort (@Nonnull final short [] aArray, @Nonnegative final int nOfs, @Nonnegative final int nLength) { ValueEnforcer.isArrayOfsLen (aArray, nOfs, nLength); m_aArray = ArrayHelper.getCopy (aArray, nOfs, nLength); }
Example 17
Source File: ByteArrayWrapper.java From ph-commons with Apache License 2.0 | 3 votes |
/** * Wrap the passed byte array or just parts of it. * * @param aBytes * The byte array to be wrapped. May not be <code>null</code>. * @param nOfs * Offset. Must be ≥ 0. * @param nLength * Length. Must be ≥ 0. * @param bCopyNeeded * <code>true</code> to copy it, <code>false</code> to reuse the * instance. */ public ByteArrayWrapper (@Nonnull final byte [] aBytes, @Nonnegative final int nOfs, @Nonnegative final int nLength, final boolean bCopyNeeded) { ValueEnforcer.isArrayOfsLen (aBytes, nOfs, nLength); m_aBytes = bCopyNeeded ? ArrayHelper.getCopy (aBytes, nOfs, nLength) : aBytes; m_nOffset = bCopyNeeded ? 0 : nOfs; m_nLength = nLength; m_bIsCopy = bCopyNeeded; }
Example 18
Source File: ArrayIteratorBoolean.java From ph-commons with Apache License 2.0 | 3 votes |
/** * constructor with offset and length * * @param aArray * Source array * @param nOfs * Offset. Must be ≥ 0. * @param nLength * Length. Must be ≥ 0. */ public ArrayIteratorBoolean (@Nonnull final boolean [] aArray, @Nonnegative final int nOfs, @Nonnegative final int nLength) { ValueEnforcer.isArrayOfsLen (aArray, nOfs, nLength); m_aArray = ArrayHelper.getCopy (aArray, nOfs, nLength); }
Example 19
Source File: ArrayIteratorLong.java From ph-commons with Apache License 2.0 | 2 votes |
/** * constructor with offset and length * * @param aArray * Source array * @param nOfs * Offset. Must be ≥ 0. * @param nLength * Length. Must be ≥ 0. */ public ArrayIteratorLong (@Nonnull final long [] aArray, @Nonnegative final int nOfs, @Nonnegative final int nLength) { ValueEnforcer.isArrayOfsLen (aArray, nOfs, nLength); m_aArray = ArrayHelper.getCopy (aArray, nOfs, nLength); }
Example 20
Source File: ArrayIteratorChar.java From ph-commons with Apache License 2.0 | 2 votes |
/** * constructor with offset and length * * @param aArray * Source array * @param nOfs * Offset. Must be ≥ 0. * @param nLength * Length. Must be ≥ 0. */ public ArrayIteratorChar (@Nonnull final char [] aArray, @Nonnegative final int nOfs, @Nonnegative final int nLength) { ValueEnforcer.isArrayOfsLen (aArray, nOfs, nLength); m_aArray = ArrayHelper.getCopy (aArray, nOfs, nLength); }