Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed 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 org.jboss.marshalling;
 
Static utility methods for simplifying use of marshallers.

Apiviz.landmark:
 
 public final class Marshalling {
 
     private Marshalling() {
     }

    
Get a marshaller factory, by name. Uses the thread's current context classloader, if available, to locate the factory.

Deprecated:
It is recommended that you use getProvidedMarshallerFactory(java.lang.String) instead; using the context class loader to find a marshalling implementation is risky at best as the user may have just about anything on their class path.
Parameters:
name the name of the protocol to acquire
Returns:
the marshaller factory, or null if no matching factory was found
See also:
java.util.ServiceLoader
 
     @Deprecated
     public static MarshallerFactory getMarshallerFactory(String name) {
         return loadMarshallerFactory(ServiceLoader.load(ProviderDescriptor.class), name);
     }

    
Get a marshaller factory, by name. Uses the given classloader to locate the factory.

Parameters:
name the name of the protocol to acquire
classLoader the class loader to use
Returns:
the marshaller factory, or null if no matching factory was found
See also:
java.util.ServiceLoader
 
     public static MarshallerFactory getMarshallerFactory(String nameClassLoader classLoader) {
         return loadMarshallerFactory(ServiceLoader.load(ProviderDescriptor.classclassLoader), name);
     }

    
Get a marshaller factory which is visible to this implementation, by name. Uses the class loader of this API.

Parameters:
name the name of the protocol to acquire
Returns:
the marshaller factory, or null if no matching factory was found
 
     public static MarshallerFactory getProvidedMarshallerFactory(String name) {
         return loadMarshallerFactory(ServiceLoader.load(ProviderDescriptor.classMarshalling.class.getClassLoader()), name);
     }
 
     private static MarshallerFactory loadMarshallerFactory(ServiceLoader<ProviderDescriptorloaderString name) {
         for (ProviderDescriptor descriptor : loader) {
             if (name.equals(descriptor.getName())) {
                 return descriptor.getMarshallerFactory();
             }
         }
         return null;
     }
 
     private static final StreamHeader NULL_STREAM_HEADER = new StreamHeader() {
         public void readHeader(final ByteInput inputthrows IOException {
        }
        public void writeHeader(final ByteOutput outputthrows IOException {
        }
        public String toString() {
            return "Null StreamHeader";
        }
    };

    
Get the default stream header producer, which reads and writes no header at all.

Returns:
the default stream header producer
    public static StreamHeader nullStreamHeader() {
        return ;
    }

    
Create a stream header that uses the given bytes.

Parameters:
headerBytes the header bytes
Returns:
the stream header object
    public static StreamHeader streamHeader(final byte[] headerBytes) {
        return new StaticStreamHeader(headerBytes);
    }
    private static final class StaticStreamHeader implements StreamHeaderSerializable {
        private final byte[] headerBytes;
        private static final long serialVersionUID = 8465784729867667872L;
        public StaticStreamHeader(final byte[] bytes) {
             = bytes;
        }
        public void readHeader(final ByteInput inputthrows IOException {
            final byte[] buf = new byte[.];
            readFully(inputbuf);
            if (! Arrays.equals(buf)) {
                throw new StreamCorruptedException("Header is incorrect (expected " + Arrays.toString() + ", got " + Arrays.toString(buf) + ")");
            }
        }
        public void writeHeader(final ByteOutput outputthrows IOException {
            output.write();
        }
        public String toString() {
            return "static StreamHeader@" + Integer.toHexString(hashCode()) + " (" + . + " bytes)";
        }
    }

    
Read bytes from a ByteInput. Fully fills in the array.

Parameters:
input the input
dest the destination
Throws:
java.io.EOFException if the end of file is reached before the array is filled
java.io.IOException if an I/O error occurs
    public static void readFully(ByteInput inputbyte[] destthrows IOException {
        readFully(inputdest, 0, dest.length);
    }

    
Read bytes from a ByteInput. Fully fills in len bytes in the array.

Parameters:
input the input
dest the destination
offs the offset into the array
len the number of bytes
Throws:
java.io.EOFException if the end of file is reached before the array is filled
java.io.IOException if an I/O error occurs
    public static void readFully(ByteInput inputbyte[] destint offsint lenthrows IOException {
        while (len > 0) {
            final int r = input.read(destoffslen);
            if (r == -1) {
                throw new EOFException();
            }
            len -= r;
            offs += r;
        }
    }

    
Create a ByteInput wrapper for a ByteBuffer.

Parameters:
buffer the byte buffer
Returns:
the byte input wrapper
    public static ByteInput createByteInput(final ByteBuffer buffer) {
        return new ByteBufferInput(buffer);
    }

    
Create a ByteInput wrapper for an InputStream.

Parameters:
inputStream the input stream
Returns:
the byte input wrapper
    public static ByteInput createByteInput(final InputStream inputStream) {
        return inputStream instanceof ByteInput ? (ByteInputinputStream : new InputStreamByteInput(inputStream);
    }

    
Create an InputStream wrapper for a ByteInput.

Parameters:
byteInput the byte input
Returns:
the input stream wrapper
    public static InputStream createInputStream(final ByteInput byteInput) {
        return byteInput instanceof InputStream ? (InputStreambyteInput : new ByteInputStream(byteInput);
    }

    
Create a ByteOutput wrapper for a ByteBuffer.

Parameters:
buffer the byte buffer
Returns:
the byte output wrapper
    public static ByteOutput createByteOutput(final ByteBuffer buffer) {
        return new ByteBufferOutput(buffer);
    }

    
Create a ByteOutput wrapper for an OutputStream.

Parameters:
outputStream the output stream
Returns:
the byte output wrapper
    public static ByteOutput createByteOutput(final OutputStream outputStream) {
        return outputStream instanceof ByteOutput ? (ByteOutputoutputStream : new OutputStreamByteOutput(outputStream);
    }

    
Create a OutputStream wrapper for a ByteOutput.

Parameters:
byteOutput the byte output
Returns:
the output stream wrapper
    public static OutputStream createOutputStream(final ByteOutput byteOutput) {
        return byteOutput instanceof OutputStream ? (OutputStream)byteOutput : new ByteOutputStream(byteOutput);
    }
        public Externalizer getExternalizer(final Class<?> type) {
            return null;
        }
        public String toString() {
            return "Null class externalizer factory";
        }
    };

    
Return the null class externalizer factory. This instance does not externalize any classes.

Returns:
the null class externalizer factory
        return ;
    }
    private static final ObjectResolver NULL_OBJECT_RESOLVER = new ObjectResolver() {
        public Object readResolve(final Object replacement) {
            return replacement;
        }
        public Object writeReplace(final Object original) {
            return original;
        }
        public String toString() {
            return "Null object resolver";
        }
    };

    
Return the null object resolver. This instance does not translate objects in any way.

Returns:
the null object resolver
    public static ObjectResolver nullObjectResolver() {
        return ;
    }
    private static final ObjectTable NULL_OBJECT_TABLE = new ObjectTable() {
        public Writer getObjectWriter(final Object object) {
            return null;
        }
        public Object readObject(final Unmarshaller unmarshallerthrows IOExceptionClassNotFoundException {
            return null;
        }
        public String toString() {
            return "Null object table";
        }
    };

    
Return the null object instance table. This instance contains no predefined instances.

Returns:
the null instance table
    public static ObjectTable nullObjectTable() {
        return ;
    }
    private static final ClassTable NULL_CLASS_TABLE = new ClassTable() {
        public Writer getClassWriter(final Class<?> clazz) {
            return null;
        }
        public Class<?> readClass(final Unmarshaller unmarshallerthrows IOExceptionClassNotFoundException {
            return null;
        }
        public String toString() {
            return "Null class table";
        }
    };

    
Return the null class table instance. This instance contains no predefined classes.

Returns:
the null class table
    public static ClassTable nullClassTable() {
        return ;
    }

    
Construct a new java.io.OptionalDataException. This method is necssary because there are no public constructors in the API.

Parameters:
eof true if there is no more data in the buffered part of the stream
Returns:
a new OptionalDataException
    public static OptionalDataException createOptionalDataException(boolean eof) {
        final OptionalDataException optionalDataException = createOptionalDataException();
        final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        final StackTraceElement[] copyStackTrace = new StackTraceElement[stackTrace.length - 1];
        System.arraycopy(stackTrace, 1, copyStackTrace, 0, copyStackTrace.length);
        optionalDataException.setStackTrace(copyStackTrace);
        optionalDataException.eof = eof;
        return optionalDataException;
    }

    
Construct a new java.io.OptionalDataException. This method is necssary because there are no public constructors in the API.

Parameters:
length the number of bytes of primitive data available to be read in the current buffer
Returns:
a new OptionalDataException
    public static OptionalDataException createOptionalDataException(int length) {
        final OptionalDataException optionalDataException = createOptionalDataException();
        optionalDataException.length = length;
        return optionalDataException;
    }
        return AccessController.doPrivileged();
    }
    private static final class OptionalDataExceptionCreateAction implements PrivilegedAction<OptionalDataException> {
        private final Constructor<OptionalDataExceptionconstructor;
        private OptionalDataExceptionCreateAction() {
             = AccessController.doPrivileged(new PrivilegedAction<Constructor<OptionalDataException>>() {
                public Constructor<OptionalDataExceptionrun() {
                    try {
                        final Constructor<OptionalDataExceptionconstructor = OptionalDataException.class.getDeclaredConstructor(boolean.class);
                        constructor.setAccessible(true);
                        return constructor;
                    } catch (NoSuchMethodException e) {
                        throw new NoSuchMethodError(e.getMessage());
                    }
                }
            });
        }
        public OptionalDataException run() {
            try {
                return .newInstance(.);
            } catch (InstantiationException e) {
                throw new InstantiationError(e.getMessage());
            } catch (IllegalAccessException e) {
                throw new IllegalAccessError(e.getMessage());
            } catch (InvocationTargetException e) {
                throw new RuntimeException("Error invoking constructor"e);
            }
        }
    }
    private static final Externalizer NULL_EXTERNALIZER = new AbstractExternalizer() {
        private static final long serialVersionUID = 1L;
        public void writeExternal(final Object subjectfinal ObjectOutput outputthrows IOException {
        }
        public void readExternal(final Object subjectfinal ObjectInput inputthrows IOExceptionClassNotFoundException {
        }
        public String toString() {
            return "Null externalizer";
        }
    };

    
Get a null externalizer. Useful in conjunction with ObjectTable entries. This externalizer reads and writes no data.

Returns:
the null externalizer
    public static Externalizer nullExternalizer() {
        return ;
    }
New to GrepCode? Check out our FAQ X