You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2014/04/24 18:23:42 UTC

svn commit: r1589786 - in /sis/branches/JDK8: core/sis-utility/src/main/java/org/apache/sis/util/resources/ storage/sis-storage/src/main/java/org/apache/sis/internal/storage/

Author: desruisseaux
Date: Thu Apr 24 16:23:41 2014
New Revision: 1589786

URL: http://svn.apache.org/r1589786
Log:
Added 'writeBits' method.

Added:
    sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageOutputStream.java   (with props)
Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
    sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelData.java
    sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java
    sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataOutput.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java?rev=1589786&r1=1589785&r2=1589786&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java [UTF-8] Thu Apr 24 16:23:41 2014
@@ -215,6 +215,11 @@ public final class Errors extends Indexe
         public static final short ExcessiveListSize_2 = 25;
 
         /**
+         * The character string is too long.
+         */
+        public static final short ExcessiveStringSize = 153;
+
+        /**
          * Attribute “{0}” is not allowed for an object of type ‘{1}’.
          */
         public static final short ForbiddenAttribute_2 = 26;

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties?rev=1589786&r1=1589785&r2=1589786&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties [ISO-8859-1] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties [ISO-8859-1] Thu Apr 24 16:23:41 2014
@@ -54,6 +54,7 @@ EmptyEnvelope2D                   = Enve
 EmptyProperty_1                   = Property named \u201c{0}\u201d shall not be empty.
 ExcessiveArgumentSize_3           = Argument \u2018{0}\u2019 shall not contain more than {1} elements. A number of {2} is excessive.
 ExcessiveListSize_2               = A size of {1} elements is excessive for the \u201c{0}\u201d list.
+ExcessiveStringSize               = The character string is too long.
 ForbiddenAttribute_2              = Attribute \u201c{0}\u201d is not allowed for an object of type \u2018{1}\u2019.
 IllegalArgumentClass_2            = Argument \u2018{0}\u2019 can not be an instance of \u2018{1}\u2019.
 IllegalArgumentClass_3            = Argument \u2018{0}\u2019 can not be an instance of \u2018{2}\u2019. Expected an instance of \u2018{1}\u2019 or derived type.

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties?rev=1589786&r1=1589785&r2=1589786&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties [ISO-8859-1] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties [ISO-8859-1] Thu Apr 24 16:23:41 2014
@@ -44,6 +44,7 @@ EmptyEnvelope2D                   = L\u2
 EmptyProperty_1                   = La propri\u00e9t\u00e9 nomm\u00e9e \u00ab\u202f{0}\u202f\u00bb ne doit pas \u00eatre vide.
 ExcessiveArgumentSize_3           = L\u2019argument \u2018{0}\u2019 ne peut pas contenir plus de {1} \u00e9l\u00e9ments. Un nombre de {2} est excessif.
 ExcessiveListSize_2               = Une taille de {1} \u00e9l\u00e9ments est excessive pour la liste \u00ab\u202f{0}\u202f\u00bb.
+ExcessiveStringSize               = La cha\u00eene de caract\u00e8res est trop longue.
 ForbiddenAttribute_2              = L\u2019attribut \u00ab\u202f{0}\u202f\u00bb n\u2019est pas autoris\u00e9 pour un objet de type \u2018{1}\u2019.
 IllegalArgumentClass_2            = L\u2019argument \u2018{0}\u2019 ne peut pas \u00eatre de type \u2018{1}\u2019.
 IllegalArgumentClass_3            = L\u2019argument \u2018{0}\u2019 ne peut pas \u00eatre de type \u2018{2}\u2019. Une instance de \u2018{1}\u2019 ou d\u2019un type d\u00e9riv\u00e9 \u00e9tait attendue.

Modified: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelData.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelData.java?rev=1589786&r1=1589785&r2=1589786&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelData.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelData.java [UTF-8] Thu Apr 24 16:23:41 2014
@@ -124,14 +124,6 @@ public abstract class ChannelData {
     }
 
     /**
-     * Pushes back the last processed byte. This is used when a call to {@code readBit()} did not
-     * used every bits in a byte, or when {@code readLine()} checked for the Windows-style of EOL.
-     */
-    final void pushBack() {
-        buffer.position(buffer.position() - 1);
-    }
-
-    /**
      * Returns the current bit offset, as an integer between 0 and 7 inclusive.
      *
      * <p>According {@link javax.imageio.stream.ImageInputStream} contract, the bit offset shall be reset to 0

Modified: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java?rev=1589786&r1=1589785&r2=1589786&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataInput.java [UTF-8] Thu Apr 24 16:23:41 2014
@@ -195,6 +195,14 @@ public class ChannelDataInput extends Ch
     }
 
     /**
+     * Pushes back the last processed byte. This is used when a call to {@code readBit()} did not
+     * used every bits in a byte, or when {@code readLine()} checked for the Windows-style of EOL.
+     */
+    final void pushBack() {
+        buffer.position(buffer.position() - 1);
+    }
+
+    /**
      * Reads a single bit from the stream. The bit to be read depends on the
      * {@linkplain #getBitOffset() current bit offset}.
      *
@@ -202,7 +210,7 @@ public class ChannelDataInput extends Ch
      * @throws IOException If an error occurred while reading (including EOF).
      */
     public final int readBit() throws IOException {
-        return (int) (readBits(1) & 1);
+        return (int) readBits(1);
     }
 
     /**

Modified: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataOutput.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataOutput.java?rev=1589786&r1=1589785&r2=1589786&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataOutput.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelDataOutput.java [UTF-8] Thu Apr 24 16:23:41 2014
@@ -29,6 +29,8 @@ import java.nio.ShortBuffer;
 import java.nio.channels.WritableByteChannel;
 import org.apache.sis.util.resources.Errors;
 
+import static org.apache.sis.util.ArgumentChecks.ensureBetween;
+
 // Related to JDK7
 import java.nio.channels.SeekableByteChannel;
 
@@ -128,14 +130,66 @@ public class ChannelDataOutput extends C
     }
 
     /**
-     * Writes a single byte to the stream at the current position.
-     * The 24 high-order bits of {@code v} are ignored.
+     * Writes a single bit. This method uses only the rightmost bit of the given argument;
+     * the upper 31 bits are ignored.
      *
-     * @param  v an integer whose lower 8 bits are to be written.
-     * @throws IOException if some I/O exception occurs during writing.
+     * @param bit The bit to write (rightmost bit).
+     * @throws IOException If an error occurred while creating the data output.
      */
-    public final void write(final int v) throws IOException {
-        writeByte(v);
+    public final void writeBit(final int bit) throws IOException {
+        writeBits(bit, 1);
+    }
+
+    /**
+     * Writes a sequence of bits. This method uses only the <var>numBits</code> rightmost bits;
+     * other bits are ignored.
+     *
+     * @param  bits The bits to write (rightmost bits).
+     * @param  numBits The number of bits to write.
+     * @throws IOException If an error occurred while creating the data output.
+     */
+    public final void writeBits(long bits, int numBits) throws IOException {
+        ensureBetween("numBits", 0, Long.SIZE, numBits);
+        if (numBits != 0) {
+            int bitOffset = getBitOffset();
+            if (bitOffset != 0) {
+                bits &= (1L << numBits) - 1; // Make sure that high-order bits are zero.
+                final int r = numBits - (Byte.SIZE - bitOffset);
+                /*
+                 * 'r' is the number of bits than we can not store in the current byte. This value may be negative,
+                 * which means that the current byte has space for more bits than what we have, in which case some
+                 * room will still exist after this method call (i.e. the 'bitOffset' will still non-zero).
+                 */
+                final long mask;
+                if (r >= 0) {
+                    mask = bits >>> r;
+                    bitOffset = 0;
+                } else {
+                    mask = bits << -r;
+                    bitOffset += numBits;
+                }
+                numBits = r;
+                assert (mask & ~0xFFL) == 0 : mask;
+                final int p = buffer.position() - 1;
+                buffer.put(p, (byte) (buffer.get(p) | mask));
+            }
+            /*
+             * At this point, we are going to write only whole bytes.
+             */
+            while (numBits > 0) {
+                numBits -= Byte.SIZE;
+                final long part;
+                if (numBits >= 0) {
+                    part = bits >>> numBits;
+                } else {
+                    part = bits << -numBits;
+                    bitOffset = (int) (Byte.SIZE + part);
+                }
+                assert (part & ~0xFFL) == 0 : part;
+                writeByte((int) part);
+            }
+            setBitOffset(bitOffset);
+        }
     }
 
     /**

Added: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageOutputStream.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageOutputStream.java?rev=1589786&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageOutputStream.java (added)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageOutputStream.java [UTF-8] Thu Apr 24 16:23:41 2014
@@ -0,0 +1,164 @@
+/*
+ * 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.sis.internal.storage;
+
+import java.io.Closeable;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.channels.WritableByteChannel;
+import javax.imageio.stream.ImageOutputStream;
+import org.apache.sis.util.resources.Errors;
+
+
+
+/**
+ * Adds the missing methods in {@code ChannelDataOutput} for implementing the {@code DataOutput} interface.
+ * Current implementation does not yet implements the {@code ImageOutputStream} sub-interface, but a future
+ * implementation may do so.
+ *
+ * <p>We do not implement {@link ImageOutputStream} yet because the later inherits all read operations from
+ * {@code ImageInputStream}, while the {@code org.apache.sis.internal.storage} package keeps the concerns
+ * separated. Despite that, the name of this {@code ChannelImageOutputStream} anticipates a future version
+ * which would implement the image I/O interface.</p>
+ *
+ * <p>{@code DataOutput} methods are defined in this separated class rather than in the parent class
+ * because some methods are Java 1.0 legacy and should be avoided (e.g. {@link #writeBytes(String)}).</p>
+ *
+ * @author  Rémi Maréchal (Geomatys)
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.5
+ * @version 0.5
+ * @module
+ */
+public class ChannelImageOutputStream extends ChannelDataOutput implements DataOutput, Closeable {
+    /**
+     * Creates a new output stream for the given channel and using the given buffer.
+     *
+     * @param  filename A file identifier used only for formatting error message.
+     * @param  channel  The channel where to write data.
+     * @param  buffer   The buffer from where to read the data.
+     * @throws IOException If an error occurred while writing into channel.
+     */
+    public ChannelImageOutputStream(final String filename, final WritableByteChannel channel, final ByteBuffer buffer)
+            throws IOException
+    {
+        super(filename, channel, buffer);
+    }
+
+    /**
+     * Creates a new output source from the given {@code ChannelDataOutput}.
+     * This constructor is invoked when we need to change the implementation class
+     * from {@code ChannelDataOutput} to {@code ChannelImageOutputStream}.
+     *
+     * @param  output The existing instance from which to takes the channel and buffer.
+     * @throws IOException If an error occurred while writing into channel.
+     */
+    public ChannelImageOutputStream(final ChannelDataOutput output) throws IOException {
+        super(output.filename, output.channel, output.buffer);
+    }
+
+    /**
+     * Writes a single byte to the stream at the current position.
+     * The 24 high-order bits of {@code v} are ignored.
+     *
+     * @param  v an integer whose lower 8 bits are to be written.
+     * @throws IOException if some I/O exception occurs during writing.
+     */
+    @Override
+    public final void write(final int v) throws IOException {
+        writeByte(v);
+    }
+
+    /**
+     * Writes boolean value (8 bits) into the steam. This method delegates to {@linkplain #writeByte(byte)}.
+     * If boolean {@code v} is {@code true} the byte value 1 is written whereas if boolean is {@code false}
+     * zero is written.
+     *
+     * @param  v boolean to be written.
+     * @throws IOException if some I/O exception occurs during writing.
+     */
+    @Override
+    public final void writeBoolean(final boolean v) throws IOException {
+        writeByte(v ? 1 : 0);
+    }
+
+    /**
+     * Writes all characters from the source into the stream.
+     *
+     * @param  s A String consider as an array of characters to be written into stream.
+     * @throws IOException If an error occurred while writing the stream.
+     */
+    @Override
+    public final void writeChars(final String s) throws IOException {
+        writeChars(s.toCharArray());
+    }
+
+    /**
+     * Writes two bytes of length information to the output stream, followed by the modified UTF-8 representation
+     * of every character in the {@code str} string. Each character is converted to a group of one, two, or three
+     * bytes, depending on the character code point value.
+     *
+     * @param  s the string to be written.
+     * @throws IOException If an error occurred while writing the stream.
+     */
+    @Override
+    public void writeUTF(final String s) throws IOException {
+        byte[] data = s.getBytes("UTF-8");
+        if (data.length > Short.MAX_VALUE) {
+            throw new IllegalArgumentException(Errors.format(Errors.Keys.ExcessiveStringSize));
+        }
+        final ByteOrder oldOrder = buffer.order();
+        buffer.order(ByteOrder.BIG_ENDIAN);
+        try {
+            writeShort(data.length);
+            write(data);
+        } finally {
+            buffer.order(oldOrder);
+        }
+    }
+
+    /**
+     * Writes the lower-order byte of each character. The high-order eight bits of each character
+     * in the string are ignored - this method does <strong>not</strong> applies any encoding.
+     *
+     * <p>This method is provided because required by the {@link DataOutput} interface, but its
+     * usage should generally be avoided.</p>
+     *
+     * @param  s the string to be written.
+     * @throws IOException If an error occurred while writing the stream.
+     */
+    @Override
+    public void writeBytes(final String s) throws IOException {
+        final byte[] data = new byte[s.length()];
+        for (int i=0; i<data.length; i++) {
+            data[i] = (byte) s.charAt(i);
+        }
+        write(data);
+    }
+
+    /**
+     * Closes the {@linkplain #channel}.
+     *
+     * @throws IOException If an error occurred while closing the channel.
+     */
+    @Override
+    public final void close() throws IOException {
+        channel.close();
+    }
+}

Propchange: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageOutputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/ChannelImageOutputStream.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8