You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/02/25 16:49:25 UTC

svn commit: r916331 - in /pivot/trunk/core/src/org/apache/pivot: io/FileSerializer.java util/MIMEType.java

Author: gbrown
Date: Thu Feb 25 15:49:24 2010
New Revision: 916331

URL: http://svn.apache.org/viewvc?rev=916331&view=rev
Log:
Add org.apache.pivot.io.FileSerializer class.


Added:
    pivot/trunk/core/src/org/apache/pivot/io/FileSerializer.java
Modified:
    pivot/trunk/core/src/org/apache/pivot/util/MIMEType.java

Added: pivot/trunk/core/src/org/apache/pivot/io/FileSerializer.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/io/FileSerializer.java?rev=916331&view=auto
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/io/FileSerializer.java (added)
+++ pivot/trunk/core/src/org/apache/pivot/io/FileSerializer.java Thu Feb 25 15:49:24 2010
@@ -0,0 +1,89 @@
+/*
+ * 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 org.apache.pivot.io;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.activation.MimetypesFileTypeMap;
+
+import org.apache.pivot.serialization.SerializationException;
+import org.apache.pivot.serialization.Serializer;
+
+/**
+ * Implementation of the {@link Serializer} interface that reads and
+ * writes {@link java.io.File} objects.
+ */
+public class FileSerializer implements Serializer<File> {
+    public static final int BUFFER_SIZE = 1024;
+
+    private static final MimetypesFileTypeMap MIME_TYPES_FILE_MAP = new MimetypesFileTypeMap();
+
+    /**
+     * Reads a file from an input stream. The returned file is a temporary file and must be
+     * deleted by the caller.
+     */
+    @Override
+    public File readObject(InputStream inputStream) throws IOException, SerializationException {
+        File file = File.createTempFile(getClass().getName(), null);
+        OutputStream outputStream = null;
+
+        try {
+            outputStream = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);
+            for (int data = inputStream.read(); data != -1; data = inputStream.read()) {
+                outputStream.write((byte)data);
+            }
+        } finally {
+            if (outputStream != null) {
+                outputStream.close();
+            }
+        }
+
+        return file;
+    }
+
+    /**
+     * Writes a file to an output stream.
+     */
+    @Override
+    public void writeObject(File file, OutputStream outputStream) throws IOException,
+        SerializationException {
+        InputStream inputStream = null;
+
+        try {
+            inputStream = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
+            for (int data = inputStream.read(); data != -1; data = inputStream.read()) {
+                outputStream.write((byte)data);
+            }
+        } finally {
+            if (inputStream != null) {
+                inputStream.close();
+            }
+        }
+    }
+
+    @Override
+    public String getMIMEType(File file) {
+        return MIME_TYPES_FILE_MAP.getContentType(file);
+    }
+}

Modified: pivot/trunk/core/src/org/apache/pivot/util/MIMEType.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/MIMEType.java?rev=916331&r1=916330&r2=916331&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/MIMEType.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/util/MIMEType.java Thu Feb 25 15:49:24 2010
@@ -21,7 +21,6 @@
 import org.apache.pivot.collections.Dictionary;
 import org.apache.pivot.collections.HashMap;
 
-
 /**
  * Utility class for introspecting a MIME type string.
  */