All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.xml.bind.v2.util.DataSourceSource Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
/*
 * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0, which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package com.sun.xml.bind.v2.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.MimeType;
import javax.activation.MimeTypeParseException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

/**
 * {@link Source} implementation backed by {@link DataHandler}.
 *
 * 

* This implementation allows the same {@link Source} to be used * mutliple times. * *

* {@link Source} isn't really pluggable. As a consequence, * this implementation is clunky --- weak against unexpected * usage of the class. * * @author Kohsuke Kawaguchi */ public final class DataSourceSource extends StreamSource { private final DataSource source; /** * If null, default to the encoding declaration */ private final String charset; // remember the value we returned so that the 2nd invocation // will return the same object, which is what's expeted out of // StreamSource private Reader r; private InputStream is; public DataSourceSource(DataHandler dh) throws MimeTypeParseException { this(dh.getDataSource()); } public DataSourceSource(DataSource source) throws MimeTypeParseException { this.source = source; String ct = source.getContentType(); if(ct==null) { charset = null; } else { MimeType mimeType = new MimeType(ct); this.charset = mimeType.getParameter("charset"); } } @Override public void setReader(Reader reader) { throw new UnsupportedOperationException(); } @Override public void setInputStream(InputStream inputStream) { throw new UnsupportedOperationException(); } @Override public Reader getReader() { try { if(charset==null) return null; if(r==null) r = new InputStreamReader(source.getInputStream(),charset); return r; } catch (IOException e) { // argh throw new RuntimeException(e); } } @Override public InputStream getInputStream() { try { if(charset!=null) return null; if(is==null) is = source.getInputStream(); return is; } catch (IOException e) { // argh throw new RuntimeException(e); } } public DataSource getDataSource() { return source; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy