001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nicolas Chapurlat <nchapurlat@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.core.io.marshallers.json;
019
020import java.io.ByteArrayInputStream;
021import java.io.IOException;
022import java.io.InputStream;
023
024import org.codehaus.jackson.JsonNode;
025
026/**
027 * This {@link InputStream} is a technical wrapper for {@link JsonNode}. It's used to broadcast a JsonNode between
028 * marshallers.
029 * <p>
030 * take a look at {@link AbstractJsonReader#getNode(InputStream, boolean)} to understand the mechanism.
031 * </p>
032 *
033 * @since 7.2
034 */
035public class InputStreamWithJsonNode extends InputStream {
036
037    private JsonNode jn;
038
039    private InputStream real = null;
040
041    public InputStreamWithJsonNode(JsonNode jn) {
042        super();
043        this.jn = jn;
044    }
045
046    public JsonNode getJsonNode() {
047        return jn;
048    }
049
050    public InputStream getRealInputStream() {
051        if (real == null) {
052            real = new ByteArrayInputStream(jn.toString().getBytes());
053        }
054        return real;
055    }
056
057    @Override
058    public int read() throws IOException {
059        return getRealInputStream().read();
060    }
061
062    @Override
063    public int read(byte[] b) throws IOException {
064        return getRealInputStream().read(b);
065    }
066
067    @Override
068    public int read(byte[] b, int off, int len) throws IOException {
069        return getRealInputStream().read(b, off, len);
070    }
071
072    @Override
073    public long skip(long n) throws IOException {
074        return getRealInputStream().skip(n);
075    }
076
077    @Override
078    public String toString() {
079        return getRealInputStream().toString();
080    }
081
082    @Override
083    public int available() throws IOException {
084        return getRealInputStream().available();
085    }
086
087    @Override
088    public void close() throws IOException {
089        if (real != null) {
090            getRealInputStream().close();
091        }
092    }
093
094    @Override
095    public void mark(int readlimit) {
096        getRealInputStream().mark(readlimit);
097    }
098
099    @Override
100    public void reset() throws IOException {
101        if (real != null) {
102            getRealInputStream().reset();
103        }
104    }
105
106    @Override
107    public boolean markSupported() {
108        return getRealInputStream().markSupported();
109    }
110
111}