001
014
015 package com.liferay.portal.kernel.flash;
016
017 import com.liferay.portal.kernel.util.ArrayUtil;
018
019 import java.io.IOException;
020 import java.io.InputStream;
021 import java.io.PushbackInputStream;
022
023
027 public class FlashMagicBytesUtil {
028
029 public static Result check(InputStream inputStream) throws IOException {
030 PushbackInputStream pushbackInputStream = new PushbackInputStream(
031 inputStream, 3);
032
033 byte[] magicBytes = new byte[3];
034
035 int length = pushbackInputStream.read(magicBytes);
036
037 if (length < 0) {
038 return new Result(false, inputStream);
039 }
040
041 pushbackInputStream.unread(magicBytes, 0, length);
042
043 inputStream = pushbackInputStream;
044
045 if (ArrayUtil.containsAll(_FLASH_MAGIC_BYTES[0], magicBytes) ||
046 ArrayUtil.containsAll(_FLASH_MAGIC_BYTES[1], magicBytes) ||
047 ArrayUtil.containsAll(_FLASH_MAGIC_BYTES[2], magicBytes)) {
048
049 return new Result(true, inputStream);
050 }
051
052 return new Result(false, inputStream);
053 }
054
055 public static class Result {
056
057 public InputStream getInputStream() {
058 return _inputStream;
059 }
060
061 public boolean isFlash() {
062 return _flash;
063 }
064
065 private Result(boolean flash, InputStream inputStream) {
066 _flash = flash;
067 _inputStream = inputStream;
068 }
069
070 private final boolean _flash;
071 private final InputStream _inputStream;
072
073 }
074
075 private static final byte[][] _FLASH_MAGIC_BYTES =
076 {{0x46, 0x57, 0x53}, {0x43, 0x57, 0x53}, {0x5a, 0x57, 0x53}};
077
078 }