/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package backtype.storm.serialization;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.Map;
import java.util.zip.GZIPInputStream;

/**
 * Always writes gzip out, but tests incoming to see if it's gzipped. If it is, deserializes with gzip. If not, uses
 * {@link backtype.storm.serialization.DefaultSerializationDelegate} to deserialize. Any logic needing to be enabled
 * via {@link #prepare(java.util.Map)} is passed through to both delegates.
 * @author danehammer
 */
public class GzipBridgeSerializationDelegate implements SerializationDelegate {

    private DefaultSerializationDelegate defaultDelegate = new DefaultSerializationDelegate();
    private GzipSerializationDelegate gzipDelegate = new GzipSerializationDelegate();

    @Override
    public void prepare(Map stormConf) {
        defaultDelegate.prepare(stormConf);
        gzipDelegate.prepare(stormConf);
    }

    @Override
    public byte[] serialize(Object object) {
        return gzipDelegate.serialize(object);
    }

    @Override
    public Object deserialize(byte[] bytes) {
        if (isGzipped(bytes)) {
            return gzipDelegate.deserialize(bytes);
        } else {
            return defaultDelegate.deserialize(bytes);
        }
    }

    /**
     * Looks ahead to see if the GZIP magic constant is heading the input stream
     */
    private boolean isGzipped(byte[] bytes) {
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        PushbackInputStream pis = new PushbackInputStream(bis, 2);

        byte[] header = new byte[2];
        try {
            pis.read(header);
            // Push those two bytes back into the stream
            pis.unread(header);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // Split up GZIP_MAGIC into readable bytes
        byte magicFirst = (byte) GZIPInputStream.GZIP_MAGIC;
        byte magicSecond =(byte) (GZIPInputStream.GZIP_MAGIC >> 8);

        return (header[0] == magicFirst) && (header[1] == magicSecond);
    }
}

@@ -2197,24 +2197,21 @@
 ing 
-the input stream
+%7B@code bytes%7D
 %0A   
@@ -2265,402 +2265,8 @@
 ) %7B%0A
-        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);%0A        PushbackInputStream pis = new PushbackInputStream(bis, 2);%0A%0A        byte%5B%5D header = new byte%5B2%5D;%0A        try %7B%0A            pis.read(header);%0A            // Push those two bytes back into the stream%0A            pis.unread(header);%0A        %7D catch (IOException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A%0A
     
@@ -2458,22 +2458,21 @@
 return (
-header
+bytes
 %5B0%5D == m
@@ -2490,14 +2490,13 @@
 && (
-header
+bytes
 %5B1%5D 

Remove unncessary use of streams from GzipBridgeSerializationDelegate