Validate the input source

@param in
a valid {@link InputStream} to a readable file
@return validation output {@link String}
@throws IllegalStateException
@throws IOException
@throws SAXException
357443
public String validate(InputStream in) throws IOException, SAXException {
        try (OneOffValidator validator = new OneOffValidator(asciiQuotes, detectLanguages, forceHTML, lineOffset, loadEntities, noStream, outputFormat, schemaUrl)) {
            return validator.validate(in);
        }
    }
1248073
public static void validateReservedDatastream(PID pid,
                                                  String dsId,
                                                  Datastream ds)
            throws ValidationException {

        // NB: only want to generate inputstream from .getContentStream() once
        // we know that this is a datastream to validate
        // to prevent reading of non-reserved datstream content when it is not needed
        InputStream content = null;

        try {
        if ("POLICY".equals(dsId)) {
            content = ds.getContentStream();
            validatePOLICY(content);
        } else if ("FESLPOLICY".equals(dsId)) {
            content = ds.getContentStream();
            validateFESLPOLICY(content);
        } else if ("RELS-EXT".equals(dsId) || "RELS-INT".equals(dsId)) {
                content = ds.getContentStream();
            validateRELS(pid, dsId, content);
            }
        } catch (StreamIOException e) {
            throw new ValidationException("Failed to get content stream for " + pid + "/" + dsId + ": " + e.getMessage(), e);
        }

        if (content != null) {
            try {
                content.close();
            } catch (IOException e) {
                throw new ValidationException("Error closing content stream for " + pid + "/" + dsId + ": " + e.getMessage(), e);
            }
        }
    }
279867
public static int copy(InputStream in, OutputStream out) throws IOException {
        BeanUtil.requireNonNull(in, "No InputStream specified");
        BeanUtil.requireNonNull(out, "No OutputStream specified");
        int byteCount = 0;
        byte[] buffer = new byte[4096];
        int bytesRead1;
        for (; (bytesRead1 = in.read(buffer)) != -1; byteCount += bytesRead1) {
            out.write(buffer, 0, bytesRead1);
        }
        out.flush();
        return byteCount;
    }
98394
@GwtIncompatible("InputStream")
  static InputStream asInputStream(final ByteInput input) {
    checkNotNull(input);
    return new InputStream() {
      @Override
      public int read() throws IOException {
        return input.read();
      }

      @Override
      public int read(byte[] b, int off, int len) throws IOException {
        checkNotNull(b);
        checkPositionIndexes(off, off + len, b.length);
        if (len == 0) {
          return 0;
        }
        int firstByte = read();
        if (firstByte == -1) {
          return -1;
        }
        b[off] = (byte) firstByte;
        for (int dst = 1; dst < len; dst++) {
          int readByte = read();
          if (readByte == -1) {
            return dst;
          }
          b[off + dst] = (byte) readByte;
        }
        return len;
      }

      @Override
      public void close() throws IOException {
        input.close();
      }
    };
  }
931361
public static boolean isXlsx(InputStream in) {
		if (false == in.markSupported()) {
			in = new BufferedInputStream(in);
		}
		try {
			return FileMagic.valueOf(in) == FileMagic.OOXML;
		} catch (IOException e) {
			throw new IORuntimeException(e);
		}
	}
72111
public static String inputStreamToString(InputStream in) {
    final int BUFFER_SIZE = 1024;
    BufferedReader bufferedReader;
    try {
      bufferedReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
      throw new DatasetException(
          "Platform doesn't support UTF-8. It must!", e);
    }
    char[] buffer = new char[BUFFER_SIZE];
    StringBuilder stringBuilder = new StringBuilder(BUFFER_SIZE);
    int bytesRead = 0;
    try {
      while ((bytesRead = bufferedReader.read(buffer, 0, BUFFER_SIZE)) > 0) {
        stringBuilder.append(buffer, 0, bytesRead);
      }
    } catch (IOException e) {
      throw new DatasetException("Error reading from input stream", e);
    }
    return stringBuilder.toString();
  }
1240809
@SuppressWarnings("rawtypes")
    protected void validateInputStreamData(Map isd) throws IllegalStateException {
        String message = null;
        if (isd != null) {
            if (isd.size() <= 3) {
                boolean type = isd.containsKey(INPUT_STREAM_CONTENT_TYPE);
                boolean length = isd.containsKey(INPUT_STREAM_CONTENT_DATA_LENGTH);
                boolean data = isd.containsKey(INPUT_STREAM_CONTENT_DATA);
                if (type && length && data) {
                    // valid.
                    return;
                } else {
                    message = "One of required values of InputStreamData is missing. type : " + type + " length : " + length + " data : " + data;
                }
            } else {
                message = "InputStreamData contains an unrecognized item.";
            }
        } else {
            message = "InputStreamData is null.";
        }
        if (com.ibm.ejs.ras.TraceComponent.isAnyTracingEnabled()&&logger.isLoggable (Level.FINE))
            logger.logp(Level.FINE, CLASS_NAME,"validateInputStreamData", message);
        throw new IllegalStateException(message);
    }
451065
@Override
    public String validate(DTDValidatorBase v, char[] cbuf, int start, int end, boolean normalize)
        throws XMLStreamException
    {
        //int origStart = start;

        /* First things first; let's ensure value is not empty (all
         * white space)...
         */
        while (start < end && WstxInputData.isSpaceChar(cbuf[start])) {
            ++start;
        }
        // Empty value?
        if (start >= end) {
            return reportValidationProblem(v, "Empty NMTOKENS value");
        }

        /* Then, let's have separate handling for normalizing and
         * non-normalizing case, since latter is trivially easy case:
         */
        if (!normalize) {
            for (; start < end; ++start) {
                char c = cbuf[start];
                if (!WstxInputData.isSpaceChar(c) 
                    && !WstxInputData.isNameChar(c, mCfgNsAware, mCfgXml11)) {
                    return reportInvalidChar(v, c, "not valid as NMTOKENS character");
                }
            }
            return null; // ok, all good
        }

        //boolean trimmed = (origStart != start);
        //origStart = start;

        --end; // so that it now points to the last char
        // Wouldn't absolutely have to trim trailing... but is easy to do
        while (end > start && WstxInputData.isSpaceChar(cbuf[end])) {
            --end;
            //trimmed = true;
        }

        /* Ok, now, need to check we only have valid chars, and maybe
         * also coalesce multiple spaces, if any.
         */
        StringBuilder sb = null;

        while (start <= end) {
            int i = start;
            for (; i <= end; ++i) {
                char c = cbuf[i];
                if (WstxInputData.isSpaceChar(c)) {
                    break;
                }
                if (!WstxInputData.isNameChar(c, mCfgNsAware, mCfgXml11)) {
                    return reportInvalidChar(v, c, "not valid as an NMTOKENS character");
                }
            }

            if (sb == null) {
                sb = new StringBuilder(end - start + 1);
            } else {
                sb.append(' ');
            }
            sb.append(cbuf, start, (i - start));

            start = i + 1;
            // Ok, any white space to skip?
            while (start <= end && WstxInputData.isSpaceChar(cbuf[start])) {
                ++start;
            }
        }

        /* 27-Nov-2005, TSa: Could actually optimize trimming, and often
         *   avoid using StringBuilder... but let's only do it if it turns
         *   out dealing with NMTOKENS normalization shows up on profiling...
         */
        return sb.toString();
    }
397908
public String validate(Path path) throws IOException, SAXException {
        try (OneOffValidator validator = new OneOffValidator(asciiQuotes, detectLanguages, forceHTML, lineOffset, loadEntities, noStream, outputFormat, schemaUrl)) {
            return validator.validate(path);
        }
    }
1091278
public static int copy(InputStream in, OutputStream out) throws IOException {
		Assert.notNull(in, "No InputStream specified");
		Assert.notNull(out, "No OutputStream specified");
		try {
			return StreamUtils.copy(in, out);
		}
		finally {
			try {
				in.close();
			}
			catch (IOException ex) {
			}
			try {
				out.close();
			}
			catch (IOException ex) {
			}
		}
	}
328261
@SuppressWarnings("cast")
    @Override
    public String validate(DTDValidatorBase v, char[] cbuf, int start, int end, boolean normalize)
        throws XMLStreamException
    {
        /* Let's skip leading/trailing white space, even if we are not
         * to normalize visible attribute value. This allows for better
         * round-trip handling (no changes for physical value caller
         * gets), but still allows succesful validation.
         */
        while (start < end && WstxInputData.isSpaceChar(cbuf[start])) {
            ++start;
        }

        // No id?
        if (start >= end) {
            return reportValidationProblem(v, "Empty IDREFS value");
        }

        --end; // so that it now points to the last char
        // We now the first char is not a space by now...
        while (end > start && WstxInputData.isSpaceChar(cbuf[end])) {
            --end;
        }

        // Ok; now start points to first, end to last char (both inclusive)
        ElementIdMap m = v.getIdMap();
        Location loc = v.getLocation();

        String idStr = null;
        StringBuilder sb = null;
        while (start <= end) {
            // Ok, need to check char validity, and also calc hash code:
            char c = cbuf[start];
            if (!WstxInputData.isNameStartChar(c, mCfgNsAware, mCfgXml11)) {
                return reportInvalidChar(v, c, "not valid as the first IDREFS character");
            }
            int hash = (int) c;
            int i = start+1;
            for (; i <= end; ++i) {
                c = cbuf[i];
                if (WstxInputData.isSpaceChar(c)) {
                    break;
                }
                if (!WstxInputData.isNameChar(c, mCfgNsAware, mCfgXml11)) {
                    return reportInvalidChar(v, c, "not valid as an IDREFS character");
                }
                hash = (hash * 31) + (int) c;
            }

            // Ok, got the next id ref...
            ElementId id = m.addReferenced(cbuf, start, i - start, hash,
                                           loc, v.getElemName(), mName);
            
            // Can skip the trailing space char (if there was one)
            start = i+1;

            /* When normalizing, we can possibly share id String, or
             * alternatively, compose normalized String if multiple
             */
            if (normalize) {
                if (idStr == null) { // first idref
                    idStr = id.getId();
                } else {
                    if (sb == null) {
                        sb = new StringBuilder(idStr);
                    }
                    idStr = id.getId();
                    sb.append(' ');
                    sb.append(idStr);
                }
            }

            // Ok, any white space to skip?
            while (start <= end && WstxInputData.isSpaceChar(cbuf[start])) {
                ++start;
            }
        }

        if (normalize) {
            if (sb != null) {
                idStr = sb.toString();
            }
            return idStr;
        }

        return null;
    }
901024
@Override
    public boolean isValid(Object input) {
        long epochMillisec;
        try {
            epochMillisec = Long.parseLong(input.toString());
        } catch (NumberFormatException e) {
            return false;
        }

        if (minValidTime != null && epochMillisec < minValidTime)
            return false;
        return !(maxValidTime != null && epochMillisec > maxValidTime);
    }
844625
public static byte[] bytes(InputStream in) throws IOException {
        if(in == null)
            throw new IllegalArgumentException("input stream cannot be null");

        ByteArrayOutputStream bout = new ByteArrayOutputStream(1024);
        byte[] bytes = new byte[1024];

        for (int x = in.read(bytes); x != -1; x = in.read(bytes))
            bout.write(bytes, 0, x);

        return bout.toByteArray();
    }
645860
private byte[] convertInputStreamToBytes(InputStream in) throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        int len;
        byte[] data = new byte[16384];

        while ((len = in.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, len);
        }

        buffer.flush();
        return buffer.toByteArray();
    }
258813
public static String readLine(InputStream in, byte[] buffer)
        throws IllegalArgumentException, IOException
    {
        return readLine(in, buffer, -1);
    }
465191
public static byte[] decode(InputStream in) {
        try {
            return IOUtils.toByteArray(decodeToStream(in));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
263656
public static InputStream toMarkSupportStream(InputStream in) {
		if (null == in) {
			return null;
		}
		if (false == in.markSupported()) {
			return new BufferedInputStream(in);
		}
		return in;
	}