Bytes are decoded by reading each byte until we find two consecutive 0
bytes. A 0 byte followed by a 1 byte is translated into a 0 byte.

@return the decoded byte buffer.
196333
@Override
  public ByteBuffer readBytes(ByteBuffer old) throws IOException {
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    while (true) {
      int byteRead = in.read();
      if (byteRead < 0) {
        throw new EOFException();
      }
      if (byteRead == 0) {
        int secondByteRead = in.read();
        if (byteRead < 0) {
          throw new EOFException();
        }
        if (secondByteRead == 0) {
          break;
        } else if (secondByteRead == 1) {
          bytesOut.write(0);
        } else {
          String msg = "Illegal encoding. 0 byte cannot be followed by "
              + "anything other than 0 or 1. It was followed by "
              + Integer.toString(byteRead);
          throw new IOException(msg);
        }
      } else {
        bytesOut.write(byteRead);
      }
    }
    return ByteBuffer.wrap(bytesOut.toByteArray());
  }
837868
public byte[] getBytes() throws IOException {
        if(cachedData == null){
            if(getReader().available() > 0) {
                cachedData = getReader().read();
            }
            else{
                cachedData = new byte[0];
            }
        }
        return cachedData;
    }
1150266
@Override
    public int decode ( byte[] buffer, int bufferIndex, int len ) throws SMBProtocolDecodingException {
        this.indexNumber = SMBUtil.readInt8(buffer, bufferIndex);
        return 8;
    }
257769
private static byte[] decode4to3( byte[] fourBytes )
    {
        byte[] outBuff1 = new byte[3];
        int    count    = decode4to3( fourBytes, 0, outBuff1, 0 );
        byte[] outBuff2 = new byte[ count ];
        
        System.arraycopy( outBuff1, 0, outBuff2, 0, count );        
        return outBuff2;
    }
185449
public byte[] getBytes()
	{
		byte[] b = new byte[size];
		int bytesCopied = 0;

		System.arraycopy(Helpers.convertIntToDWord(size), 0, b, bytesCopied, 4);
		bytesCopied += 4;
		b[bytesCopied++] = (byte) numFlagBytes;
		System.arraycopy(getFlagBytes(), 0, b, bytesCopied, numFlagBytes);
		bytesCopied += numFlagBytes;

		return b;
	}
1326175
protected byte[] byteBufferToByteArray() {
        if (byteBuffer.hasArray()) {
            return byteBuffer.array();
        } else {
            final byte[] byteArray = new byte[byteBuffer.remaining()];
            byteBuffer.get(byteArray);
            return byteArray;
        }
    }
1170775
public byte[] getBytes() {
		final int len = getVInt();
		final byte[] bytes = new byte[len];
		System.arraycopy(buf, bufPosition, bytes, 0, bytes.length);
		bufPosition += bytes.length;
		return bytes;
	}
965069
public byte[] readToBytes() {
        try {
            try (InputStream in = body()) {
                return InputStreams.readAll(in);
            }
        } catch (IOException e) {
            throw new RequestsException(e);
        } finally {
            close();
        }
    }
1010362
public ByteBuffer getBuffer() {
        assert(isDirect == false);
        assert(buffer.b().hasArray());
        assert(!buffer.b().isDirect());
        buffer.b().flip();
        return buffer.b().asReadOnlyBuffer();
    }
415219
@Override
    public int decode ( byte[] buffer, int bufferIndex, int len ) throws SMBProtocolDecodingException {
        int start = bufferIndex;
        int nalgos = SMBUtil.readInt2(buffer, bufferIndex);
        int nsalt = SMBUtil.readInt2(buffer, bufferIndex + 2);
        bufferIndex += 4;

        this.hashAlgos = new int[nalgos];
        for ( int i = 0; i < nalgos; i++ ) {
            this.hashAlgos[ i ] = SMBUtil.readInt2(buffer, bufferIndex);
            bufferIndex += 2;
        }

        this.salt = new byte[nsalt];
        System.arraycopy(buffer, bufferIndex, this.salt, 0, nsalt);
        bufferIndex += nsalt;

        return bufferIndex - start;
    }
104256
public byte[] getBytesF() {
		final int len = getInt();
		final byte[] bytes = new byte[len];
		System.arraycopy(buf, bufPosition, bytes, 0, bytes.length);
		bufPosition += bytes.length;
		return bytes;
	}
470841
@Override
    public ByteBuffer getBuffer()
    {
        //  If we are expected to read large message, we'll opt for zero-
        //  copy, i.e. we'll ask caller to fill the data directly to the
        //  message. Note that subsequent read(s) are non-blocking, thus
        //  each single read reads at most SO_RCVBUF bytes at once not
        //  depending on how large is the chunk returned from here.
        //  As a consequence, large messages being received won't block
        //  other engines running in the same I/O thread for excessive
        //  amounts of time.
        if (toRead >= bufsize) {
            zeroCopy = true;
            return readPos.duplicate();
        }
        else {
            zeroCopy = false;
            buf.clear();
            return buf;
        }
    }
1326321
public byte[] decodeBinary() throws IOException {
		final int length = decodeUnsignedInteger();
		byte[] result = new byte[length];

		istream.read(result, 0, length);
		return result;
	}
754515
public byte[] decodeBinary() throws IOException {
		final int length = decodeUnsignedInteger();
		byte[] result = new byte[length];

		int readBytes = 0;
		while (readBytes < length) {
			int len = is.read(result, readBytes, length - readBytes);
			if (len == -1) {
				throw new EOFException(
						"Premature EOS found while reading data.");
			}
			readBytes += len;
		}

		// int readBytes = is.read(result);
		// if (readBytes < length) {
		// // special case: not all bytes are read
		// while ((readBytes += is.read(result, readBytes, length - readBytes))
		// < length) {
		// }
		// }

		return result;
	}
961075
@Override
	protected int decode(ByteBuffer b, char[] chars, int pos, int len, MutableBoolean interrupt, int min) {
		if (b == null) {
			// end
			if (state == 0)
				return -1;
			chars[pos] = (char)val;
			state = 0;
			return 1;
		}
		int nb = 0;
		while ((state == 7 || b.hasRemaining()) && nb < len && (nb < min || interrupt == null || !interrupt.get())) {
			int a = state == 7 ? 0 : b.get() & 0xFF;
			switch (state) {
			case 0:
				if (a < 0x80) {
					// 0aaa aaaa
					chars[pos + (nb++)] = (char)a;
				} else if ((a & 0xE0) == 0xC0) {
					// 110a aaaa 10bb bbbb
					val = (a & 0x1F) << 6;
					state = 1;
				} else if ((a & 0xF0) == 0xE0) {
					// 1110 aaaa 10bb bbbb 10cc cccc
					val = (a & 0x0F) << 12;
					state = 2;
				} else if ((a & 0xF8) == 0xF0) {
					// 1111 0aaa 10bb bbbb 10cc cccc 10dd dddd
					val = (a & 0x07) << 12;
					state = 4;
				} else {
					chars[pos + (nb++)] = invalidChar;
				}
				break;
			case 1:
				// second byte of 110a aaaa 10bb bbbb
				if ((a & 0xC0) == 0x80) {
					chars[pos + (nb++)] = (char)(val | (a & 0x3F));
				} else {
					chars[pos + (nb++)] = invalidChar;
				}
				state = 0;
				break;
			case 2:
				// second byte of 1110 aaaa 10bb bbbb 10cc cccc
				if ((a & 0xC0) == 0x80) {
					val |= (a & 0x3F) << 6;
					state = 3;
				} else {
					chars[pos + (nb++)] = invalidChar;
					state = 0;
				}
				break;
			case 3:
				// third byte of 1110 aaaa 10bb bbbb 10cc cccc
				if ((a & 0xC0) == 0x80) {
					chars[pos + (nb++)] = (char)(val | (a & 0x3F));
				} else {
					chars[pos + (nb++)] = invalidChar;
				}
				state = 0;
				break;
			case 4:
				// second byte of 1111 0aaa 10bb bbbb 10cc cccc 10dd dddd
				if ((a & 0xC0) == 0x80) {
					val |= (a & 0x3F) << 6;
					state = 5;
				} else {
					chars[pos + (nb++)] = invalidChar;
					state = 0;
				}
				break;
			case 5:
				// third byte of 1111 0aaa 10bb bbbb 10cc cccc 10dd dddd
				if ((a & 0xC0) == 0x80) {
					val |= (a & 0x3F);
					state = 6;
				} else {
					chars[pos + (nb++)] = invalidChar;
					state = 0;
				}
				break;
			case 6:
				// fourth byte of 1111 0aaa 10bb bbbb 10cc cccc 10dd dddd
				if ((a & 0xC0) == 0x80) {
					// UTF-8:   [1111 0uuu] [10uu zzzz] [10yy yyyy] [10xx xxxx]*
					// Unicode: [1101 10ww] [wwzz zzyy] (high surrogate)
					//          [1101 11yy] [yyxx xxxx] (low surrogate)
					//          * uuuuu = wwww + 1
					int uuuuu = (val & 0x7C00) >> 10;
					// TODO if > 0x10 invalid Surrogate
					int wwww = uuuuu - 1;
					chars[pos + (nb++)] = (char)(0xD800 | (wwww << 6) | ((val & 0x3C0) >> 4) | ((val & 0x30) >> 4));
					val = (0xDC00 | ((val & 0xF) << 6) | (a & 0x3F));
					state = 7;
				} else {
					chars[pos + (nb++)] = invalidChar;
					state = 0;
				}
				break;
			case 7:
				chars[pos + (nb++)] = (char)val;
				state = 0;
				break;
			}
		}
		return nb;
	}
1309979
@Override
    public int decode ( byte[] buffer, int bufferIndex ) throws SMBProtocolDecodingException {
        int len = super.decode(buffer, bufferIndex);
        if ( this.byteCount == 0 ) {
            // otherwise hasMore may not be correctly set
            readBytesWireFormat(buffer, len + bufferIndex);
        }
        nextElement();
        return len;
    }
40640
final byte[] decodeChecked(CharSequence chars)
      throws DecodingException {
    chars = padding().trimTrailingFrom(chars);
    byte[] tmp = new byte[maxDecodedSize(chars.length())];
    int len = decodeTo(tmp, chars);
    return extract(tmp, len);
  }