Java Code Examples for com.helger.commons.CGlobal#BITS_PER_BYTE
The following examples show how to use
com.helger.commons.CGlobal#BITS_PER_BYTE .
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: NonBlockingBitInputStream.java From ph-commons with Apache License 2.0 | 6 votes |
/** * Read the next bit from the stream. * * @return 0 if the bit is 0, 1 if the bit is 1. * @throws IOException * In case EOF is reached */ public int readBit () throws IOException { if (m_aIS == null) throw new IOException ("BitInputStream is already closed"); if (m_nNextBitIndex == CGlobal.BITS_PER_BYTE) { m_nBuffer = m_aIS.read (); if (m_nBuffer == -1) throw new EOFException (); m_nNextBitIndex = 0; } final int nSelectorBit = m_bHighOrderBitFirst ? (1 << (CGlobal.BITS_PER_BYTE - 1 - m_nNextBitIndex)) : (1 << m_nNextBitIndex); final int nBitValue = m_nBuffer & nSelectorBit; m_nNextBitIndex++; return nBitValue == 0 ? CGlobal.BIT_NOT_SET : CGlobal.BIT_SET; }
Example 2
Source File: NonBlockingBitOutputStream.java From ph-commons with Apache License 2.0 | 6 votes |
/** * Write a single bit to the stream. It will only be flushed to the underlying * OutputStream when a byte has been completed or when flush() manually. * * @param aBit * 1 if the bit should be set, 0 if not * @throws IOException * In case writing to the output stream failed */ public void writeBit (final int aBit) throws IOException { if (m_aOS == null) throw new IllegalStateException ("BitOutputStream is already closed"); if (aBit != CGlobal.BIT_NOT_SET && aBit != CGlobal.BIT_SET) throw new IllegalArgumentException (aBit + " is not a bit"); if (aBit == CGlobal.BIT_SET) if (m_bHighOrderBitFirst) m_nBuffer |= (aBit << (7 - m_nBufferedBitCount)); else m_nBuffer |= (aBit << m_nBufferedBitCount); ++m_nBufferedBitCount; if (m_nBufferedBitCount == CGlobal.BITS_PER_BYTE) flush (); }
Example 3
Source File: AbstractPasswordHashCreatorPBKDF2.java From ph-commons with Apache License 2.0 | 6 votes |
/** * Computes the PBKDF2 hash of a password. * * @param aPassword * the password to hash. * @param aSalt * the salt * @param nIterations * the iteration count (slowness factor) * @param nBytes * the length of the hash to compute in bytes * @return the PBDKF2 hash of the password */ @Nonnull protected static final byte [] pbkdf2 (@Nonnull final char [] aPassword, @Nonnull final byte [] aSalt, @Nonnegative final int nIterations, @Nonnegative final int nBytes) { try { final PBEKeySpec spec = new PBEKeySpec (aPassword, aSalt, nIterations, nBytes * CGlobal.BITS_PER_BYTE); final SecretKeyFactory skf = SecretKeyFactory.getInstance (PBKDF2_ALGORITHM); return skf.generateSecret (spec).getEncoded (); } catch (final GeneralSecurityException ex) { throw new IllegalStateException ("Failed to apply PBKDF2 algorithm", ex); } }
Example 4
Source File: BitSetHelper.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Convert the passed byte value to an bit set of size 8. * * @param nValue * The value to be converted to a bit set. * @return The non-<code>null</code> bit set. */ @Nonnull public static BitSet createBitSet (final byte nValue) { final BitSet ret = new BitSet (CGlobal.BITS_PER_BYTE); for (int i = 0; i < CGlobal.BITS_PER_BYTE; ++i) ret.set (i, ((nValue >> i) & 1) == 1); return ret; }
Example 5
Source File: NonBlockingBitInputStream.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Create a new bit input stream based on an existing Java InputStream. * * @param aIS * the input stream this class should read the bits from. May not be * <code>null</code>. * @param aByteOrder * The non-<code>null</code> byte order to use. */ public NonBlockingBitInputStream (@Nonnull final InputStream aIS, @Nonnull final ByteOrder aByteOrder) { ValueEnforcer.notNull (aIS, "InputStream"); ValueEnforcer.notNull (aByteOrder, "ByteOrder"); m_aIS = StreamHelper.getBuffered (aIS); m_bHighOrderBitFirst = aByteOrder.equals (ByteOrder.LITTLE_ENDIAN); m_nNextBitIndex = CGlobal.BITS_PER_BYTE; }
Example 6
Source File: NonBlockingBitOutputStream.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Write the current cache to the stream and reset the buffer. * * @throws IOException * In case writing to the output stream failed */ public void flush () throws IOException { if (m_nBufferedBitCount > 0) { if (m_nBufferedBitCount != CGlobal.BITS_PER_BYTE) if (LOGGER.isDebugEnabled ()) LOGGER.debug ("Flushing BitOutputStream with only " + m_nBufferedBitCount + " bits"); m_aOS.write ((byte) m_nBuffer); m_nBufferedBitCount = 0; m_nBuffer = 0; } }