Create an exception for the case when an unexpected byte value is read

@param expected
@param b
@return
@throws MessageFormatException
321245
private static MessagePackException unexpected(String expected, byte b)
    {
        MessageFormat format = MessageFormat.valueOf(b);
        if (format == MessageFormat.NEVER_USED) {
            return new MessageNeverUsedFormatException(String.format("Expected %s, but encountered 0xC1 \"NEVER_USED\" byte", expected));
        }
        else {
            String name = format.getValueType().name();
            String typeName = name.substring(0, 1) + name.substring(1).toLowerCase();
            return new MessageTypeException(String.format("Expected %s, but got %s (%02x)", expected, typeName, b));
        }
    }
586770
public int read(byte[] b) throws IOException
	{
		if (b == null)
			throw new NullPointerException();
		return p.readBytes(b, 0, b.length);
	}
700036
static public void assertEquals(String message, String expected, String actual) {
        if (expected == null && actual == null) {
            return;
        }
        if (expected != null && expected.equals(actual)) {
            return;
        }
        String cleanMessage = message == null ? "" : message;
        throw new ComparisonFailure(cleanMessage, expected, actual);
    }
238906
static String fromBytes(final byte[] b) {
    try {
      return (String) fromBytes.invoke(null, b);
    } catch (Exception e) {
      throw new RuntimeException("fromBytes=" + fromBytes, e);
    }
  }
464908
public static function create($value, $expectedType, $message = null)
    {
        if ($message === null) {
            $errorMessage = sprintf('Expected %s, got %s', $expectedType, self::getStringRepresentation($value));
        } elseif ($message === '') {
            $errorMessage = sprintf('Expected %s', $expectedType);
        } else {
            $errorMessage = sprintf('Expected %s. %s', $expectedType, $message);
        }
        return $errorMessage;
    }
879980
@Override
    public int read(byte[] b, int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return 0;
        }

        byte[] next = ensureDataAvailable(true);
        if (next == null) {
            return -1;
        }
        int numRead = Math.min(next.length - index, len);
        System.arraycopy(next, index, b, off, numRead);
        index += numRead;
        return numRead;
    }
1361883
public void expect(byte[] out) throws IOException {
        int i, off = 0;
        while (off < out.length && (i = in.read(out, off, out.length - off)) > 0) {
            off += i;
        }
        if (off < out.length) {
            throw new IOException("Not enough data available on stream: " + off + " < " + out.length);
        }
    }
474687
@Override
    public Byte fromBytes(Class targetClass, byte[] b)
    {
        try
        {
            if (b == null)
            {
                return null;
            }
            // return new Byte(new String(b, Constants.ENCODING));
            return (ByteBuffer.wrap(b).get());
        }
        catch (NumberFormatException e)
        {
            log.warn("Number format exception caught!,returning null!");
            return null;
        }
    }
228515
public byte[] getByteBuffer(int expected) {
		if (this.byteBuffer.length >= expected) {
			return byteBuffer;
		} else if (expected < MAX_SIZE) {
			// ?预先设置多一点
			byteBuffer = new byte[(int) (expected * 1.2)];
			return byteBuffer;
		} else {
			return EMPTY_BYTE_ARRAY;
		}

	}
427567
public static Matcher<Throwable> exceptionWithMessage(Class<? extends Throwable> type, String expectedMessage) {
        return new ExpectedException(type, expectedMessage);
    }
751845
public static <T extends Exception> org.hamcrest.Matcher<T> hasMessage(
            String expectedMessage) {
        return new ExceptionMessageMatcher<>(expectedMessage);
    }
59206
@Override
    public BytesMessage createBytesMessage() throws JMSRuntimeException {

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.entry(this, tc, "createBytesMessage");

        BytesMessage bytesMessage = null;

        try {
            bytesMessage = jmsSession.createBytesMessage();
        } catch (JMSException e) {
            throw (JMSRuntimeException) JmsErrorUtils.getJMS2Exception(e, JMSRuntimeException.class);
        } finally {
            if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
                SibTr.exit(this, tc, "createBytesMessage");
        }
        return bytesMessage;
    }
889212
@Override
	public int read(byte[] b, int off, int len) throws IOException {
		if (currentEntry != null) {
			if (currentFileSize == currentEntry.getSize()) {
				return -1;
			} else if ((currentEntry.getSize() - currentFileSize) < len) {
				len = (int) (currentEntry.getSize() - currentFileSize);
			}
		}

		int br = super.read(b, off, len);

		if (br != -1) {
			if (currentEntry != null) {
				currentFileSize += br;
			}

			bytesRead += br;
		}

		return br;
	}
200623
public static void assertCauseCauseMessage(final Throwable ex, final String expectedMessage) {
        assertThat(ex.getCause()).isNotNull();
        assertThat(ex.getCause().getCause()).isNotNull();
        assertThat(ex.getCause().getCause().getMessage()).isEqualTo(expectedMessage);
    }
796869
public long expectSigned(int bytes) throws IOException {
        switch (bytes) {
            case 8:
                return expectLong();
            case 4:
                return expectInt();
            case 2:
                return expectShort();
            case 1:
                return expectByte();
        }
        throw new IllegalArgumentException("Unsupported byte count for signed: " + bytes);
    }
729499
public byte[] expectBytes(final int bytes) throws IOException {
        if (bytes > (128 * 1024)) {  // more than 128 kB, be cautious.
            ByteArrayOutputStream tmp = new ByteArrayOutputStream(128 * 1024);
            int remaining = bytes;
            byte[] buffer = new byte[4 * 1024];  // 1 page in memory.
            int r;
            while (remaining > 0 &&
                   (r = in.read(buffer, 0, Math.min(remaining, buffer.length))) > 0) {
                tmp.write(buffer, 0, r);
                remaining -= r;
            }
            if (tmp.size() < bytes) {
                throw new IOException("Not enough data available on stream: " + tmp.size() + " < " + bytes);
            }
            return tmp.toByteArray();
        }
        byte[] out = new byte[bytes];
        expect(out);
        return out;
    }
498250
@Override
    @GwtIncompatible("incompatible method")
    public String getFormattedExceptionMessage(final String baseMessage){
        final StringBuilder buffer = new StringBuilder(256);
        if (baseMessage != null) {
            buffer.append(baseMessage);
        }

        if (contextValues.size() > 0) {
            if (buffer.length() > 0) {
                buffer.append('\n');
            }
            buffer.append("Exception Context:\n");

            int i = 0;
            for (final Pair<String, Object> pair : contextValues) {
                buffer.append("\t[");
                buffer.append(++i);
                buffer.append(':');
                buffer.append(pair.getKey());
                buffer.append("=");
                final Object value = pair.getValue();
                if (value == null) {
                    buffer.append("null");
                } else {
                    String valueStr;
                    try {
                        valueStr = value.toString();
                    } catch (final Exception e) {
                        valueStr = "Exception thrown on toString(): " + ExceptionUtils.getStackTrace(e);
                    }
                    buffer.append(valueStr);
                }
                buffer.append("]\n");
            }
            buffer.append("---------------------------------");
        }
        return buffer.toString();
    }