001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
024     * @author Brian Wing Shun Chan
025     * @author Mika Koivisto
026     */
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    }