001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.core.io.marshallers.json;
021
022import java.io.ByteArrayInputStream;
023import java.io.IOException;
024import java.io.InputStream;
025
026import org.codehaus.jackson.JsonNode;
027
028/**
029 * This {@link InputStream} is a technical wrapper for {@link JsonNode}. It's used to broadcast a JsonNode between
030 * marshallers.
031 * <p>
032 * take a look at {@link AbstractJsonReader#getNode(InputStream, boolean)} to understand the mechanism.
033 * </p>
034 *
035 * @since 7.2
036 */
037public class InputStreamWithJsonNode extends InputStream {
038
039    private JsonNode jn;
040
041    private InputStream real = null;
042
043    public InputStreamWithJsonNode(JsonNode jn) {
044        super();
045        this.jn = jn;
046    }
047
048    public JsonNode getJsonNode() {
049        return jn;
050    }
051
052    public InputStream getRealInputStream() {
053        if (real == null) {
054            real = new ByteArrayInputStream(jn.toString().getBytes());
055        }
056        return real;
057    }
058
059    @Override
060    public int read() throws IOException {
061        return getRealInputStream().read();
062    }
063
064    @Override
065    public int read(byte[] b) throws IOException {
066        return getRealInputStream().read(b);
067    }
068
069    @Override
070    public int read(byte[] b, int off, int len) throws IOException {
071        return getRealInputStream().read(b, off, len);
072    }
073
074    @Override
075    public long skip(long n) throws IOException {
076        return getRealInputStream().skip(n);
077    }
078
079    @Override
080    public String toString() {
081        return getRealInputStream().toString();
082    }
083
084    @Override
085    public int available() throws IOException {
086        return getRealInputStream().available();
087    }
088
089    @Override
090    public void close() throws IOException {
091        if (real != null) {
092            getRealInputStream().close();
093        }
094    }
095
096    @Override
097    public void mark(int readlimit) {
098        getRealInputStream().mark(readlimit);
099    }
100
101    @Override
102    public void reset() throws IOException {
103        if (real != null) {
104            getRealInputStream().reset();
105        }
106    }
107
108    @Override
109    public boolean markSupported() {
110        return getRealInputStream().markSupported();
111    }
112
113}